Skip to content

Commit 03c0c1f

Browse files
authored
Merge pull request #465 from yukinarit/fix-more-type-errors
Fix more mypy type errors
2 parents d5114cb + 6acedba commit 03c0c1f

File tree

8 files changed

+75
-70
lines changed

8 files changed

+75
-70
lines changed

pyproject.toml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,20 @@ include = [
101101
"serde/yaml.py",
102102
"serde/pickle.py",
103103
"tests/test_custom.py",
104+
"tests/test_kwonly.py",
105+
"tests/test_flatten.py",
106+
"tests/test_lazy_type_evaluation.py",
107+
"tests/data.py",
104108
]
105109
exclude = [
106110
"serde/numpy.py",
107111
"bench",
108112
"tests/common.py",
109-
"tests/data.py",
110113
"tests/imported.py",
111114
"tests/test_basics.py",
112115
"tests/test_compat.py",
113116
"tests/test_core.py",
114117
"tests/test_de.py",
115-
"tests/test_flatten.py",
116-
"tests/test_kwonly.py",
117-
"tests/test_lazy_type_evaluation.py",
118118
"tests/test_legacy_custom.py",
119119
"tests/test_literal.py",
120120
"tests/test_numpy.py",
@@ -134,15 +134,11 @@ exclude = [
134134
"examples/field_order.py",
135135
"bench",
136136
"tests/common.py",
137-
"tests/data.py",
138137
"tests/imported.py",
139138
"tests/test_basics.py",
140139
"tests/test_compat.py",
141140
"tests/test_core.py",
142141
"tests/test_de.py",
143-
"tests/test_flatten.py",
144-
"tests/test_kwonly.py",
145-
"tests/test_lazy_type_evaluation.py",
146142
"tests/test_legacy_custom.py",
147143
"tests/test_literal.py",
148144
"tests/test_numpy.py",

serde/compat.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from collections import defaultdict
1717
from dataclasses import is_dataclass
1818
from typing import (
19+
NewType,
1920
Any,
2021
ClassVar,
2122
DefaultDict,
@@ -595,7 +596,7 @@ def is_bare_list(typ: Type[Any]) -> bool:
595596
return typ in (List, list)
596597

597598

598-
def is_tuple(typ: Type[Any]) -> bool:
599+
def is_tuple(typ: Any) -> bool:
599600
"""
600601
Test if the type is `typing.Tuple`.
601602
"""
@@ -768,7 +769,7 @@ def is_primitive_subclass(typ: Type[Any]) -> bool:
768769
return is_primitive(typ) and typ not in PRIMITIVES and not is_new_type_primitive(typ)
769770

770771

771-
def is_primitive(typ: Type[Any]) -> bool:
772+
def is_primitive(typ: Union[Type[Any], NewType]) -> bool:
772773
"""
773774
Test if the type is primitive.
774775
@@ -780,12 +781,12 @@ def is_primitive(typ: Type[Any]) -> bool:
780781
True
781782
"""
782783
try:
783-
return any(issubclass(typ, ty) for ty in PRIMITIVES)
784+
return any(issubclass(typ, ty) for ty in PRIMITIVES) # type: ignore
784785
except TypeError:
785786
return is_new_type_primitive(typ)
786787

787788

788-
def is_new_type_primitive(typ: Type[Any]) -> bool:
789+
def is_new_type_primitive(typ: Union[Type[Any], NewType]) -> bool:
789790
"""
790791
Test if the type is a NewType of primitives.
791792
"""

tests/common.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
Set,
2020
Tuple,
2121
TypeVar,
22+
Union,
2223
)
24+
from typing_extensions import TypeAlias
2325

2426
import more_itertools
2527

@@ -31,21 +33,23 @@
3133
from serde.yaml import from_yaml, to_yaml
3234
from tests import data
3335

34-
format_dict: List = [(to_dict, from_dict)]
36+
FormatFuncs: TypeAlias = List[Tuple[Callable[..., Any], Callable[..., Any]]]
3537

36-
format_tuple: List = [(to_tuple, from_tuple)]
38+
format_dict: FormatFuncs = [(to_dict, from_dict)]
3739

38-
format_json: List = [(to_json, from_json)]
40+
format_tuple: FormatFuncs = [(to_tuple, from_tuple)]
3941

40-
format_msgpack: List = [(to_msgpack, from_msgpack)]
42+
format_json: FormatFuncs = [(to_json, from_json)]
4143

42-
format_yaml: List = [(to_yaml, from_yaml)]
44+
format_msgpack: FormatFuncs = [(to_msgpack, from_msgpack)]
4345

44-
format_toml: List = [(to_toml, from_toml)]
46+
format_yaml: FormatFuncs = [(to_yaml, from_yaml)]
4547

46-
format_pickle: List = [(to_pickle, from_pickle)]
48+
format_toml: FormatFuncs = [(to_toml, from_toml)]
4749

48-
all_formats: List = (
50+
format_pickle: FormatFuncs = [(to_pickle, from_pickle)]
51+
52+
all_formats: FormatFuncs = (
4953
format_dict
5054
+ format_tuple
5155
+ format_json
@@ -79,7 +83,10 @@ class NestedGenericClass(Generic[U, V]):
7983
b: Inner[V]
8084

8185

82-
def param(val, typ, filter: Optional[Callable] = None):
86+
Filter: TypeAlias = Callable[..., bool]
87+
88+
89+
def param(val: T, typ: U, filter: Optional[Filter] = None) -> Tuple[T, U, Filter]:
8390
"""
8491
Create a test parameter
8592
@@ -98,7 +105,7 @@ def yaml_not_supported(se: Any, de: Any, opt: Any) -> bool:
98105
return se is to_yaml
99106

100107

101-
types: List = [
108+
types: List[Tuple[Any, Any, Optional[Filter]]] = [
102109
param(10, int), # Primitive
103110
param("foo", str),
104111
param(100.0, float),
@@ -179,14 +186,14 @@ def yaml_not_supported(se: Any, de: Any, opt: Any) -> bool:
179186
list(more_itertools.flatten(c)) for c in itertools.combinations(types, 2)
180187
]
181188

182-
opt_case: List = [
189+
opt_case: List[Dict[str, Union[bool, str]]] = [
183190
{"reuse_instances_default": False},
184191
{"reuse_instances_default": False, "rename_all": "camelcase"},
185192
{"reuse_instances_default": False, "rename_all": "snakecase"},
186193
]
187194

188195

189-
def make_id_from_dict(d: Dict) -> str:
196+
def make_id_from_dict(d: Dict[str, str]) -> str:
190197
if not d:
191198
return "none"
192199
else:

tests/test_compat.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
U = TypeVar("U")
3131

3232

33-
def test_types():
33+
def test_types() -> None:
3434
assert is_list(List[int])
3535
assert is_list(List)
3636
assert is_set(Set[int])
@@ -68,7 +68,7 @@ def test_types():
6868
assert is_opt(str | None)
6969

7070

71-
def test_typename():
71+
def test_typename() -> None:
7272
@serde.serde
7373
class Bar(Generic[T]):
7474
v: T
@@ -87,13 +87,13 @@ class Foo(Generic[T]):
8787
assert typename(Tuple[int, str]) == "Tuple[int, str]"
8888
assert typename(Tuple[int, ...]) == "Tuple[int, ...]"
8989
assert typename(Dict) == "Dict"
90-
assert typename(Dict[str, Foo]) == "Dict[str, Foo]"
90+
assert typename(Dict[str, Foo]) == "Dict[str, Foo]" # type: ignore
9191
assert typename(Set) == "Set"
9292
assert typename(Set[int]) == "Set[int]"
93-
assert typename(Literal[1, 1.0, "Hey"]) == "Literal[1, 1.0, Hey]"
93+
assert typename(Literal[1, True, "Hey"]) == "Literal[1, True, Hey]"
9494

9595

96-
def test_iter_types():
96+
def test_iter_types() -> None:
9797
assert {Pri, int, str, float, bool} == set(iter_types(Pri))
9898
assert {Dict, str, Pri, int, float, bool} == set(iter_types(Dict[str, Pri]))
9999
assert {List, str} == set(iter_types(List[str]))
@@ -115,7 +115,7 @@ class Foo:
115115
assert {Foo, datetime, Optional, str, Union, List, Set, int} == set(iter_types(Foo))
116116

117117

118-
def test_iter_unions():
118+
def test_iter_unions() -> None:
119119
assert [Union[str, int]] == list(iter_unions(Union[str, int]))
120120
assert [Union[str, int]] == list(iter_unions(Dict[str, Union[str, int]]))
121121
assert [Union[str, int]] == list(iter_unions(Tuple[Union[str, int]]))
@@ -134,7 +134,7 @@ class A:
134134
)
135135

136136

137-
def test_type_args():
137+
def test_type_args() -> None:
138138
assert (int,) == type_args(List[int])
139139
assert (int, str) == type_args(Dict[int, str])
140140
assert (int, type(None)) == type_args(Optional[int])
@@ -151,13 +151,13 @@ def test_type_args():
151151
assert (int, Ellipsis) == type_args(tuple[int, ...])
152152

153153

154-
def test_union_args():
154+
def test_union_args() -> None:
155155
assert (int, str) == union_args(Union[int, str])
156156
assert (List[int], Dict[int, str]) == union_args(Union[List[int], Dict[int, str]])
157157
assert (Optional[int], str) == union_args(Union[Optional[int], str])
158158

159159

160-
def test_is_instance():
160+
def test_is_instance() -> None:
161161
# Primitive
162162
assert is_instance(10, int)
163163
assert is_instance("str", str)
@@ -257,7 +257,7 @@ class GenericFoo(Generic[T]):
257257
t: T
258258

259259

260-
def test_is_generic():
260+
def test_is_generic() -> None:
261261
assert not is_generic(int)
262262
assert not is_generic(List[int])
263263
assert not is_generic(List)
@@ -272,7 +272,7 @@ def test_is_generic():
272272
assert not serde.is_deserializable(GenericFoo[List[int]])
273273

274274

275-
def test_get_generic_arg():
275+
def test_get_generic_arg() -> None:
276276
class GenericFoo(Generic[T, U]):
277277
pass
278278

tests/test_flatten.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Tests for flatten attribute.
33
"""
44

5-
from typing import Dict, List
5+
from typing import Dict, List, Any
66

77
import pytest
88

@@ -12,7 +12,7 @@
1212
from .common import all_formats
1313

1414

15-
def test_flatten_simple():
15+
def test_flatten_simple() -> None:
1616
@serde
1717
class Bar:
1818
c: float
@@ -31,7 +31,7 @@ class Foo:
3131

3232

3333
@pytest.mark.parametrize("se,de", all_formats)
34-
def test_flatten(se, de):
34+
def test_flatten(se: Any, de: Any) -> None:
3535
@serde
3636
class Baz:
3737
e: List[int]

tests/test_kwonly.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@pytest.mark.skipif(
1111
sys.version_info < (3, 10), reason="dataclasses `kw_only` requires python3.10 or higher"
1212
)
13-
def test_simple():
13+
def test_simple() -> None:
1414
@deserialize
1515
@dataclass(kw_only=True)
1616
class Hello:
@@ -22,7 +22,7 @@ class Hello:
2222
@pytest.mark.skipif(
2323
sys.version_info < (3, 10), reason="dataclasses `kw_only` requires python3.10 or higher"
2424
)
25-
def test_inheritance():
25+
def test_inheritance() -> None:
2626
@dataclass(kw_only=True)
2727
class Friend:
2828
name: str = "MyFriend"

tests/test_lazy_type_evaluation.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class B:
3030

3131

3232
# only works with global classes
33-
def test_serde_with_lazy_type_annotations():
33+
def test_serde_with_lazy_type_annotations() -> None:
3434
a = A(1, Status.ERR, ["foo"])
3535
a_dict = {"a": 1, "b": "err", "c": ["foo"]}
3636

@@ -57,7 +57,8 @@ class ForwardReferenceBar:
5757

5858

5959
# assert type is str
60-
assert "ForwardReferenceBar" == dataclasses.fields(ForwardReferenceFoo)[0].type
60+
typ = dataclasses.fields(ForwardReferenceFoo)[0].type
61+
assert isinstance(typ, str) and "ForwardReferenceBar" == typ
6162

6263
# setup pyserde for Foo after Bar becomes visible to global scope
6364
deserialize(ForwardReferenceFoo)
@@ -69,7 +70,7 @@ class ForwardReferenceBar:
6970

7071

7172
# verify usage works
72-
def test_forward_reference_works():
73+
def test_forward_reference_works() -> None:
7374
h = ForwardReferenceFoo(bar=ForwardReferenceBar(i=10))
7475
h_dict = {"bar": {"i": 10}}
7576

@@ -78,7 +79,7 @@ def test_forward_reference_works():
7879

7980

8081
# trying to use forward reference normally will throw
81-
def test_unresolved_forward_reference_throws():
82+
def test_unresolved_forward_reference_throws() -> None:
8283
with pytest.raises(SerdeError) as e:
8384

8485
@serde
@@ -93,7 +94,7 @@ class UnresolvedForwardBar:
9394

9495

9596
# trying to use string forward reference will throw
96-
def test_string_forward_reference_throws():
97+
def test_string_forward_reference_throws() -> None:
9798
with pytest.raises(SerdeError) as e:
9899

99100
@serde

0 commit comments

Comments
 (0)