Skip to content

Commit eebdc06

Browse files
tests: Type-check unit tests
1 parent 80c4b50 commit eebdc06

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

databind.json/.changelog/_unreleased.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ id = "e8a41ce4-27c1-4706-a154-fb2488c342dd"
99
type = "improvement"
1010
description = "Expose public API in `databind.json` root module."
1111
author = "@NiklasRosenstein"
12+
13+
[[entries]]
14+
id = "5bbbae6a-fb76-4657-b16d-27358ae9f22d"
15+
type = "tests"
16+
description = "Type-check unit tests"
17+
author = "@NiklasRosenstein"

databind.json/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ isort = "isort --check-only src/"
4343
flake8 = "flake8 src/"
4444

4545
[tool.slap.run]
46-
fmt = "black src/ tests/ && isort src/ tests/"
46+
fmt = "black src/ && isort src/"
4747

4848
[tool.mypy]
4949
python_version = "3.6"

databind.json/src/databind/json/tests/converters_test.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@
3939

4040

4141
def make_mapper(converters: t.List[Converter]) -> ObjectMapper[t.Any, t.Any]:
42-
mapper = ObjectMapper()
42+
mapper = ObjectMapper[t.Any, t.Any]()
4343
for converter in converters:
4444
mapper.module.register(converter)
4545
return mapper
4646

4747

48-
def test_any_converter():
48+
def test_any_converter() -> None:
4949
mapper = make_mapper([AnyConverter()])
5050
assert mapper.convert(Direction.SERIALIZE, "foobar", t.Any) == "foobar"
5151
assert mapper.convert(Direction.SERIALIZE, 42, t.Any) == 42
5252
assert mapper.convert(Direction.SERIALIZE, t.Any, t.Any) == t.Any
5353

5454

5555
@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()])
5858

5959
# test strict
6060

@@ -77,8 +77,8 @@ def test_plain_datatype_converter(direction: Direction):
7777

7878

7979
@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()])
8282

8383
pi = decimal.Decimal("3.141592653589793")
8484
if direction == Direction.SERIALIZE:
@@ -93,7 +93,7 @@ def test_decimal_converter(direction: Direction):
9393

9494
@pytest.mark.parametrize("direction", (Direction.DESERIALIZE, Direction.SERIALIZE))
9595
# @pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
96-
def test_enum_converter(direction: Direction):
96+
def test_enum_converter(direction: Direction) -> None:
9797
mapper = make_mapper([EnumConverter()])
9898

9999
class Pet(enum.Enum):
@@ -126,7 +126,7 @@ class Flags(enum.IntEnum):
126126
assert mapper.convert(direction, 3, Flags)
127127

128128

129-
def test_optional_converter():
129+
def test_optional_converter() -> None:
130130
mapper = make_mapper([OptionalConverter(), PlainDatatypeConverter()])
131131
assert mapper.convert(Direction.SERIALIZE, 42, t.Optional[int]) == 42
132132
assert mapper.convert(Direction.SERIALIZE, None, t.Optional[int]) is None
@@ -136,7 +136,7 @@ def test_optional_converter():
136136

137137

138138
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
139-
def test_datetime_converter(direction: Direction):
139+
def test_datetime_converter(direction: Direction) -> None:
140140
mapper = make_mapper([DatetimeConverter()])
141141

142142
tests = [
@@ -153,40 +153,40 @@ def test_datetime_converter(direction: Direction):
153153

154154

155155
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
156-
def test_duration_converter(direction: Direction):
156+
def test_duration_converter(direction: Direction) -> None:
157157
mapper = make_mapper([StringifyConverter(duration, duration.parse), SchemaConverter(), PlainDatatypeConverter()])
158158

159159
# Test parsing duration from strings.
160160

161-
tests = [
161+
test_from_strings = [
162162
(duration(2, 1, 4, 0, 3), "P2Y1M4WT3H"),
163163
(duration(seconds=10), "PT10S"),
164164
(duration(days=1, minutes=5), "P1DT5M"),
165165
]
166166

167-
for py_value, str_value in tests:
167+
for py_value, str_value in test_from_strings:
168168
if direction == Direction.SERIALIZE:
169169
assert mapper.convert(direction, py_value, duration) == str_value
170170
else:
171171
assert mapper.convert(direction, str_value, duration) == py_value
172172

173-
# Test parsing duraiton from objects (it is also a dataclass).
173+
# Test parsing duration from objects (it is also a dataclass).
174174

175-
tests = [
175+
test_from_dicts = [
176176
(duration(2, 1, 4, 0, 3), {"years": 2, "months": 1, "weeks": 4, "hours": 3}),
177177
(duration(seconds=10), {"seconds": 10}),
178178
(duration(days=1, minutes=5), {"days": 1, "minutes": 5}),
179179
]
180180

181-
for py_value, obj_value in tests:
181+
for py_value, obj_value in test_from_dicts:
182182
if direction == Direction.SERIALIZE:
183183
assert mapper.convert(direction, py_value, duration) == str(py_value) # obj_value
184184
else:
185185
assert mapper.convert(direction, obj_value, duration) == py_value
186186

187187

188188
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
189-
def test_stringify_converter(direction: Direction):
189+
def test_stringify_converter(direction: Direction) -> None:
190190
mapper = make_mapper([StringifyConverter(uuid.UUID)])
191191

192192
uid = uuid.uuid4()
@@ -197,7 +197,7 @@ def test_stringify_converter(direction: Direction):
197197

198198

199199
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
200-
def test_mapping_converter(direction):
200+
def test_mapping_converter(direction: Direction) -> None:
201201
mapper = make_mapper([AnyConverter(), MappingConverter(), PlainDatatypeConverter()])
202202
assert mapper.convert(direction, {"a": 1}, t.Mapping) == {"a": 1}
203203
assert mapper.convert(direction, {"a": 1}, t.Mapping[str, int]) == {"a": 1}
@@ -206,7 +206,8 @@ def test_mapping_converter(direction):
206206
with pytest.raises(ConversionError):
207207
assert mapper.convert(direction, 1, t.Mapping[int, str])
208208

209-
K, V = t.TypeVar("K"), t.TypeVar("V")
209+
K = t.TypeVar("K")
210+
V = t.TypeVar("V")
210211

211212
class CustomDict(t.Dict[K, V]):
212213
pass
@@ -225,7 +226,7 @@ class CustomDict(t.Dict[K, V]):
225226

226227

227228
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
228-
def test_collection_converter(direction):
229+
def test_collection_converter(direction: Direction) -> None:
229230
mapper = make_mapper([AnyConverter(), CollectionConverter(), PlainDatatypeConverter()])
230231
assert mapper.convert(direction, [1, 2, 3], t.Collection) == [1, 2, 3]
231232
assert mapper.convert(direction, [1, 2, 3], t.Collection[int]) == [1, 2, 3]
@@ -253,7 +254,7 @@ class CustomList(t.List[T]):
253254

254255

255256
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
256-
def test_union_converter_nested(direction):
257+
def test_union_converter_nested(direction: Direction) -> None:
257258
mapper = make_mapper([UnionConverter(), PlainDatatypeConverter()])
258259

259260
hint = te.Annotated[t.Union[int, str], Union({"int": int, "str": str})]
@@ -264,7 +265,7 @@ def test_union_converter_nested(direction):
264265

265266

266267
@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:
268269
mapper = make_mapper([UnionConverter(), PlainDatatypeConverter()])
269270

270271
if direction == Direction.DESERIALIZE:
@@ -274,7 +275,7 @@ def test_union_converter_best_match(direction):
274275

275276

276277
@pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
277-
def test_union_converter_keyed(direction):
278+
def test_union_converter_keyed(direction: Direction) -> None:
278279
mapper = make_mapper([UnionConverter(), PlainDatatypeConverter()])
279280

280281
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):
285286

286287

287288
@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:
289290
mapper = make_mapper([UnionConverter(), PlainDatatypeConverter()])
290291

291292
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):
302303
# TODO(NiklasRosenstein): Bring back dataclass definitions in function bodies.
303304

304305
# @pytest.mark.parametrize("direction", (Direction.SERIALIZE, Direction.DESERIALIZE))
305-
# def test_schema_converter(direction):
306+
# def test_schema_converter(direction: Direction):
306307
# mapper = make_mapper([SchemaConverter(), PlainDatatypeConverter()])
307308

308309
# class Dict1(te.TypedDict):
@@ -351,7 +352,7 @@ def test_union_converter_flat_plain_types_not_supported(direction):
351352

352353

353354
@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:
355356
mapper = make_mapper([SchemaConverter(), MappingConverter(), PlainDatatypeConverter()])
356357

357358
@dataclasses.dataclass
@@ -366,7 +367,7 @@ class A:
366367

367368

368369
@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:
370371
mapper = make_mapper([SchemaConverter(), MappingConverter(), PlainDatatypeConverter()])
371372

372373
@dataclasses.dataclass
@@ -413,7 +414,7 @@ class MyClass:
413414
assert mapper.deserialize({"a": {}}, MyClass) == MyClass(B(42))
414415

415416

416-
def test_deserialize_union_dataclass_subclass():
417+
def test_deserialize_union_dataclass_subclass() -> None:
417418
"""Tests that a subclass of a dataclass marked as a union is deserialized as a dataclass."""
418419

419420
@dataclasses.dataclass
@@ -432,7 +433,7 @@ class B(A):
432433
assert load({"id": 0, "name": "spam"}, B) == B(0, "spam")
433434

434435

435-
def test_deserialize_and_serialize_literal_union():
436+
def test_deserialize_and_serialize_literal_union() -> None:
436437
from databind.json import dump, load
437438

438439
@dataclasses.dataclass
@@ -482,8 +483,8 @@ def convert(self, ctx: Context) -> t.Any:
482483
class MyClass1:
483484
a: int
484485

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
487488
assert load({}, te.Annotated[MyClass1, JsonConverter(MyConverter("I am better"))]) == "I am better"
488489

489490
class AnotherClass:
@@ -498,7 +499,7 @@ class AnotherClass:
498499
class MyClass2:
499500
a: int
500501

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
502503
assert load({"a": 42}, MyClass2) == MyClass2(42)
503504
assert load({"a": 42}, te.Annotated[MyClass2, JsonConverter(MyConverter("foo"))]) == "foo"
504505
assert load({"a": 42}, te.Annotated[MyClass2, JsonConverter(MyConverter(None, skip=True))]) == MyClass2(42)

0 commit comments

Comments
 (0)