39
39
40
40
41
41
def make_mapper (converters : t .List [Converter ]) -> ObjectMapper [t .Any , t .Any ]:
42
- mapper = ObjectMapper ()
42
+ mapper = ObjectMapper [ t . Any , t . Any ] ()
43
43
for converter in converters :
44
44
mapper .module .register (converter )
45
45
return mapper
46
46
47
47
48
- def test_any_converter ():
48
+ def test_any_converter () -> None :
49
49
mapper = make_mapper ([AnyConverter ()])
50
50
assert mapper .convert (Direction .SERIALIZE , "foobar" , t .Any ) == "foobar"
51
51
assert mapper .convert (Direction .SERIALIZE , 42 , t .Any ) == 42
52
52
assert mapper .convert (Direction .SERIALIZE , t .Any , t .Any ) == t .Any
53
53
54
54
55
55
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
56
- def test_plain_datatype_converter (direction : Direction ):
57
- mapper = make_mapper ([PlainDatatypeConverter (direction )])
56
+ def test_plain_datatype_converter (direction : Direction ) -> None :
57
+ mapper = make_mapper ([PlainDatatypeConverter ()])
58
58
59
59
# test strict
60
60
@@ -77,8 +77,8 @@ def test_plain_datatype_converter(direction: Direction):
77
77
78
78
79
79
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
80
- def test_decimal_converter (direction : Direction ):
81
- mapper = make_mapper ([DecimalConverter (direction )])
80
+ def test_decimal_converter (direction : Direction ) -> None :
81
+ mapper = make_mapper ([DecimalConverter ()])
82
82
83
83
pi = decimal .Decimal ("3.141592653589793" )
84
84
if direction == Direction .SERIALIZE :
@@ -93,7 +93,7 @@ def test_decimal_converter(direction: Direction):
93
93
94
94
@pytest .mark .parametrize ("direction" , (Direction .DESERIALIZE , Direction .SERIALIZE ))
95
95
# @pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
96
- def test_enum_converter (direction : Direction ):
96
+ def test_enum_converter (direction : Direction ) -> None :
97
97
mapper = make_mapper ([EnumConverter ()])
98
98
99
99
class Pet (enum .Enum ):
@@ -126,7 +126,7 @@ class Flags(enum.IntEnum):
126
126
assert mapper .convert (direction , 3 , Flags )
127
127
128
128
129
- def test_optional_converter ():
129
+ def test_optional_converter () -> None :
130
130
mapper = make_mapper ([OptionalConverter (), PlainDatatypeConverter ()])
131
131
assert mapper .convert (Direction .SERIALIZE , 42 , t .Optional [int ]) == 42
132
132
assert mapper .convert (Direction .SERIALIZE , None , t .Optional [int ]) is None
@@ -136,7 +136,7 @@ def test_optional_converter():
136
136
137
137
138
138
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
139
- def test_datetime_converter (direction : Direction ):
139
+ def test_datetime_converter (direction : Direction ) -> None :
140
140
mapper = make_mapper ([DatetimeConverter ()])
141
141
142
142
tests = [
@@ -153,40 +153,40 @@ def test_datetime_converter(direction: Direction):
153
153
154
154
155
155
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
156
- def test_duration_converter (direction : Direction ):
156
+ def test_duration_converter (direction : Direction ) -> None :
157
157
mapper = make_mapper ([StringifyConverter (duration , duration .parse ), SchemaConverter (), PlainDatatypeConverter ()])
158
158
159
159
# Test parsing duration from strings.
160
160
161
- tests = [
161
+ test_from_strings = [
162
162
(duration (2 , 1 , 4 , 0 , 3 ), "P2Y1M4WT3H" ),
163
163
(duration (seconds = 10 ), "PT10S" ),
164
164
(duration (days = 1 , minutes = 5 ), "P1DT5M" ),
165
165
]
166
166
167
- for py_value , str_value in tests :
167
+ for py_value , str_value in test_from_strings :
168
168
if direction == Direction .SERIALIZE :
169
169
assert mapper .convert (direction , py_value , duration ) == str_value
170
170
else :
171
171
assert mapper .convert (direction , str_value , duration ) == py_value
172
172
173
- # Test parsing duraiton from objects (it is also a dataclass).
173
+ # Test parsing duration from objects (it is also a dataclass).
174
174
175
- tests = [
175
+ test_from_dicts = [
176
176
(duration (2 , 1 , 4 , 0 , 3 ), {"years" : 2 , "months" : 1 , "weeks" : 4 , "hours" : 3 }),
177
177
(duration (seconds = 10 ), {"seconds" : 10 }),
178
178
(duration (days = 1 , minutes = 5 ), {"days" : 1 , "minutes" : 5 }),
179
179
]
180
180
181
- for py_value , obj_value in tests :
181
+ for py_value , obj_value in test_from_dicts :
182
182
if direction == Direction .SERIALIZE :
183
183
assert mapper .convert (direction , py_value , duration ) == str (py_value ) # obj_value
184
184
else :
185
185
assert mapper .convert (direction , obj_value , duration ) == py_value
186
186
187
187
188
188
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
189
- def test_stringify_converter (direction : Direction ):
189
+ def test_stringify_converter (direction : Direction ) -> None :
190
190
mapper = make_mapper ([StringifyConverter (uuid .UUID )])
191
191
192
192
uid = uuid .uuid4 ()
@@ -197,7 +197,7 @@ def test_stringify_converter(direction: Direction):
197
197
198
198
199
199
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
200
- def test_mapping_converter (direction ) :
200
+ def test_mapping_converter (direction : Direction ) -> None :
201
201
mapper = make_mapper ([AnyConverter (), MappingConverter (), PlainDatatypeConverter ()])
202
202
assert mapper .convert (direction , {"a" : 1 }, t .Mapping ) == {"a" : 1 }
203
203
assert mapper .convert (direction , {"a" : 1 }, t .Mapping [str , int ]) == {"a" : 1 }
@@ -206,7 +206,8 @@ def test_mapping_converter(direction):
206
206
with pytest .raises (ConversionError ):
207
207
assert mapper .convert (direction , 1 , t .Mapping [int , str ])
208
208
209
- K , V = t .TypeVar ("K" ), t .TypeVar ("V" )
209
+ K = t .TypeVar ("K" )
210
+ V = t .TypeVar ("V" )
210
211
211
212
class CustomDict (t .Dict [K , V ]):
212
213
pass
@@ -225,7 +226,7 @@ class CustomDict(t.Dict[K, V]):
225
226
226
227
227
228
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
228
- def test_collection_converter (direction ) :
229
+ def test_collection_converter (direction : Direction ) -> None :
229
230
mapper = make_mapper ([AnyConverter (), CollectionConverter (), PlainDatatypeConverter ()])
230
231
assert mapper .convert (direction , [1 , 2 , 3 ], t .Collection ) == [1 , 2 , 3 ]
231
232
assert mapper .convert (direction , [1 , 2 , 3 ], t .Collection [int ]) == [1 , 2 , 3 ]
@@ -253,7 +254,7 @@ class CustomList(t.List[T]):
253
254
254
255
255
256
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
256
- def test_union_converter_nested (direction ) :
257
+ def test_union_converter_nested (direction : Direction ) -> None :
257
258
mapper = make_mapper ([UnionConverter (), PlainDatatypeConverter ()])
258
259
259
260
hint = te .Annotated [t .Union [int , str ], Union ({"int" : int , "str" : str })]
@@ -264,7 +265,7 @@ def test_union_converter_nested(direction):
264
265
265
266
266
267
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
267
- def test_union_converter_best_match (direction ) :
268
+ def test_union_converter_best_match (direction : Direction ) -> None :
268
269
mapper = make_mapper ([UnionConverter (), PlainDatatypeConverter ()])
269
270
270
271
if direction == Direction .DESERIALIZE :
@@ -274,7 +275,7 @@ def test_union_converter_best_match(direction):
274
275
275
276
276
277
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
277
- def test_union_converter_keyed (direction ) :
278
+ def test_union_converter_keyed (direction : Direction ) -> None :
278
279
mapper = make_mapper ([UnionConverter (), PlainDatatypeConverter ()])
279
280
280
281
th = te .Annotated [t .Union [int , str ], Union ({"int" : int , "str" : str }, style = Union .KEYED )]
@@ -285,7 +286,7 @@ def test_union_converter_keyed(direction):
285
286
286
287
287
288
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
288
- def test_union_converter_flat_plain_types_not_supported (direction ) :
289
+ def test_union_converter_flat_plain_types_not_supported (direction : Direction ) -> None :
289
290
mapper = make_mapper ([UnionConverter (), PlainDatatypeConverter ()])
290
291
291
292
th = te .Annotated [t .Union [int , str ], Union ({"int" : int , "str" : str }, style = Union .FLAT )]
@@ -302,7 +303,7 @@ def test_union_converter_flat_plain_types_not_supported(direction):
302
303
# TODO(NiklasRosenstein): Bring back dataclass definitions in function bodies.
303
304
304
305
# @pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
305
- # def test_schema_converter(direction):
306
+ # def test_schema_converter(direction: Direction ):
306
307
# mapper = make_mapper([SchemaConverter(), PlainDatatypeConverter()])
307
308
308
309
# class Dict1(te.TypedDict):
@@ -351,7 +352,7 @@ def test_union_converter_flat_plain_types_not_supported(direction):
351
352
352
353
353
354
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
354
- def test_schema_converter_with_dict_member (direction ) :
355
+ def test_schema_converter_with_dict_member (direction : Direction ) -> None :
355
356
mapper = make_mapper ([SchemaConverter (), MappingConverter (), PlainDatatypeConverter ()])
356
357
357
358
@dataclasses .dataclass
@@ -366,7 +367,7 @@ class A:
366
367
367
368
368
369
@pytest .mark .parametrize ("direction" , (Direction .SERIALIZE , Direction .DESERIALIZE ))
369
- def test_schema_converter_remainder_field (direction ) :
370
+ def test_schema_converter_remainder_field (direction : Direction ) -> None :
370
371
mapper = make_mapper ([SchemaConverter (), MappingConverter (), PlainDatatypeConverter ()])
371
372
372
373
@dataclasses .dataclass
@@ -413,7 +414,7 @@ class MyClass:
413
414
assert mapper .deserialize ({"a" : {}}, MyClass ) == MyClass (B (42 ))
414
415
415
416
416
- def test_deserialize_union_dataclass_subclass ():
417
+ def test_deserialize_union_dataclass_subclass () -> None :
417
418
"""Tests that a subclass of a dataclass marked as a union is deserialized as a dataclass."""
418
419
419
420
@dataclasses .dataclass
@@ -432,7 +433,7 @@ class B(A):
432
433
assert load ({"id" : 0 , "name" : "spam" }, B ) == B (0 , "spam" )
433
434
434
435
435
- def test_deserialize_and_serialize_literal_union ():
436
+ def test_deserialize_and_serialize_literal_union () -> None :
436
437
from databind .json import dump , load
437
438
438
439
@dataclasses .dataclass
@@ -482,8 +483,8 @@ def convert(self, ctx: Context) -> t.Any:
482
483
class MyClass1 :
483
484
a : int
484
485
485
- assert load ({"a" : 42 }, MyClass1 ) == "Oh HELLO"
486
- assert load ({}, MyClass1 ) == "Oh HELLO"
486
+ assert load ({"a" : 42 }, MyClass1 ) == "Oh HELLO" # type: ignore[comparison-overlap] # Non-overlapping equality check # noqa: E501
487
+ assert load ({}, MyClass1 ) == "Oh HELLO" # type: ignore[comparison-overlap] # Non-overlapping equality check # noqa: E501
487
488
assert load ({}, te .Annotated [MyClass1 , JsonConverter (MyConverter ("I am better" ))]) == "I am better"
488
489
489
490
class AnotherClass :
@@ -498,7 +499,7 @@ class AnotherClass:
498
499
class MyClass2 :
499
500
a : int
500
501
501
- assert load ({"a" : 42 }, MyClass1 ) == "Oh HELLO"
502
+ assert load ({"a" : 42 }, MyClass1 ) == "Oh HELLO" # type: ignore[comparison-overlap] # Non-overlapping equality check # noqa: E501
502
503
assert load ({"a" : 42 }, MyClass2 ) == MyClass2 (42 )
503
504
assert load ({"a" : 42 }, te .Annotated [MyClass2 , JsonConverter (MyConverter ("foo" ))]) == "foo"
504
505
assert load ({"a" : 42 }, te .Annotated [MyClass2 , JsonConverter (MyConverter (None , skip = True ))]) == MyClass2 (42 )
0 commit comments