26
26
import python_test .containers .thrift_mutable_types as mutable_containers_types
27
27
import python_test .containers .thrift_types as immutable_containers_types
28
28
29
- import python_test .maps .thrift_mutable_types as immutable_maps_types
30
- import python_test .maps .thrift_types as mutable_maps_types
29
+ import python_test .maps .thrift_mutable_types as mutable_maps_types
30
+ import python_test .maps .thrift_types as immutable_maps_types
31
31
32
32
from folly .iobuf import IOBuf
33
33
@@ -53,6 +53,15 @@ class MyStringEnum(str, Enum):
53
53
test = "test"
54
54
55
55
56
+ class ImmutableMapTests (unittest .TestCase ):
57
+ def test_hashability (self ) -> None :
58
+ hash (immutable_maps_types .StrI32ListMap ())
59
+ x = immutable_maps_types .StrStrIntListMapMap (
60
+ {"foo" : immutable_maps_types .StrI32ListMap ()}
61
+ )
62
+ hash (x ["foo" ])
63
+
64
+
56
65
@parameterized_class (
57
66
("containers_types" , "maps_types" ),
58
67
[
@@ -84,6 +93,9 @@ def setUp(self) -> None:
84
93
self .Foo : Type [FooType ] = self .containers_types .Foo
85
94
self .Maps : Type [MapsType ] = self .containers_types .Maps
86
95
self .Color : Type [ColorType ] = self .containers_types .Color
96
+ self .is_mutable_run : bool = self .containers_types .__name__ .endswith (
97
+ "thrift_mutable_types"
98
+ )
87
99
88
100
def test_recursive_const_map (self ) -> None :
89
101
self .assertEqual (self .LocationMap [1 ][1 ], 1 )
@@ -163,14 +175,6 @@ def test_mixed_construction(self) -> None:
163
175
self .StrStrIntListMapMap (px )
164
176
self .assertNotIn ("bar" , cx )
165
177
166
- def test_hashability (self ) -> None :
167
- # Mutable types do not support hashing
168
- # pyre-ignore[16]: has no attribute `lists_types`
169
- if self .maps_types .__name__ .endswith ("immutable_types" ):
170
- hash (self .StrI32ListMap ())
171
- x = self .StrStrIntListMapMap ({"foo" : self .StrI32ListMap ()})
172
- hash (x ["foo" ])
173
-
174
178
def test_equality (self ) -> None :
175
179
x = self .StrIntMap ({"foo" : 5 , "bar" : 4 })
176
180
y = self .StrIntMap ({"foo" : 4 , "bar" : 5 })
@@ -198,9 +202,6 @@ def test_struct_in_map(self) -> None:
198
202
self .assertNotEqual (a , c )
199
203
200
204
def test_struct_with_map_fields (self ) -> None :
201
- # pyre-ignore[16]: has no attribute `lists_types`
202
- is_immutable = self .maps_types .__name__ .endswith ("immutable_types" )
203
-
204
205
s = self .Maps (
205
206
boolMap = {True : True , False : False },
206
207
byteMap = {1 : 1 , 2 : 2 , 3 : 3 },
@@ -212,12 +213,12 @@ def test_struct_with_map_fields(self) -> None:
212
213
binaryMap = {b"foo" : b"foo" , b"bar" : b"bar" },
213
214
iobufMap = {IOBuf (b"foo" ): IOBuf (b"foo" ), IOBuf (b"bar" ): IOBuf (b"bar" )},
214
215
structMap = (
215
- {
216
+ {}
217
+ if self .is_mutable_run
218
+ else {
216
219
self .Foo (value = 1 ): self .Foo (value = 1 ),
217
220
self .Foo (value = 2 ): self .Foo (value = 2 ),
218
221
}
219
- if is_immutable
220
- else {}
221
222
),
222
223
)
223
224
self .assertEqual (s .boolMap , {True : True , False : False })
@@ -231,7 +232,7 @@ def test_struct_with_map_fields(self) -> None:
231
232
self .assertEqual (
232
233
s .iobufMap , {IOBuf (b"foo" ): IOBuf (b"foo" ), IOBuf (b"bar" ): IOBuf (b"bar" )}
233
234
)
234
- if is_immutable :
235
+ if not self . is_mutable_run :
235
236
self .assertEqual (
236
237
s .structMap ,
237
238
{
0 commit comments