@@ -57,13 +57,34 @@ def test_len(self, d: ImmutableMap[str, Any]) -> None:
5757
5858 def test_hash (self , d : ImmutableMap [str , Any ]) -> None :
5959 """Test `__hash__`."""
60- assert hash (d ) == hash (tuple (d .items ()))
60+ assert hash (d ) == hash (frozenset (d .items ()))
6161
6262 # Not hashable if values aren't hashable.
6363 d = ImmutableMap (a = 1 , b = {"c" })
6464 with pytest .raises (TypeError , match = "unhashable type: 'set'" ):
6565 hash (d )
6666
67+ def test_eq_with_other_mappings (self ) -> None :
68+ """Test mapping interoperability for `__eq__`."""
69+ d = ImmutableMap (a = 1 , b = 2 )
70+ other_dict = {"a" : 1 , "b" : 2 }
71+ other_ordered_dict = OrderedDict ([("a" , 1 ), ("b" , 2 )])
72+ other_proxy = MappingProxyType ({"a" : 1 , "b" : 2 })
73+
74+ assert d == {"a" : 1 , "b" : 2 }
75+ assert d == OrderedDict ([("a" , 1 ), ("b" , 2 )])
76+ assert d == MappingProxyType ({"a" : 1 , "b" : 2 })
77+ assert other_dict == d
78+ assert other_ordered_dict == d
79+ assert other_proxy == d
80+
81+ def test_eq_and_hash_ignore_insertion_order (self ) -> None :
82+ """Test equality/hash contract for same items in different orders."""
83+ d1 = ImmutableMap (a = 1 , b = 2 )
84+ d2 = ImmutableMap (b = 2 , a = 1 )
85+ assert d1 == d2
86+ assert hash (d1 ) == hash (d2 )
87+
6788 def test_keys (self , d : ImmutableMap [str , Any ]) -> None :
6889 """Test `keys`."""
6990 assert list (d .keys ()) == ["a" , "b" ]
0 commit comments