Skip to content

Commit 4d66e94

Browse files
authoredMar 3, 2023
Merge pull request #325 from yukinarit/fix-typing
Add type annotations to from_dict, from_tuple and from_msgpack
2 parents 8cd7e26 + e4cc087 commit 4d66e94

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed
 

‎examples/msg_pack.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from dataclasses import dataclass
2+
3+
from serde import serde
4+
from serde.msgpack import from_msgpack, to_msgpack
5+
6+
7+
@serde
8+
@dataclass
9+
class Foo:
10+
i: int
11+
s: str
12+
f: float
13+
b: bool
14+
15+
16+
def main():
17+
f = Foo(i=10, s='foo', f=100.0, b=True)
18+
p = to_msgpack(f)
19+
print(f"Into MsgPack: {p}")
20+
print(f"From MsgPack: {from_msgpack(Foo, p)}")
21+
22+
23+
if __name__ == '__main__':
24+
main()

‎examples/runner.py

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import jsonfile
1919
import lazy_type_evaluation
2020
import literal
21+
import msg_pack
2122
import newtype
2223
import pep681
2324
import plain_dataclass
@@ -80,6 +81,7 @@ def run_all():
8081
run(class_var)
8182
run(plain_dataclass)
8283
run(plain_dataclass_class_attribute)
84+
run(msg_pack)
8385
if PY310:
8486
import union_operator
8587

‎serde/de.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import functools
1010
import typing
1111
from dataclasses import dataclass, is_dataclass
12-
from typing import Any, Callable, Dict, Iterator, List, Optional, TypeVar
12+
from typing import Any, Callable, Dict, List, Optional, TypeVar
1313

1414
import jinja2
1515
from typing_extensions import Type, dataclass_transform
@@ -18,6 +18,7 @@
1818
Literal,
1919
SerdeError,
2020
SerdeSkip,
21+
T,
2122
UserError,
2223
find_generic_arg,
2324
get_args,
@@ -367,7 +368,7 @@ def deserialize(cls, data, **opts):
367368
raise NotImplementedError
368369

369370

370-
def from_obj(c: Type, o: Any, named: bool, reuse_instances: bool):
371+
def from_obj(c: Type[T], o: Any, named: bool, reuse_instances: bool) -> T:
371372
"""
372373
Deserialize from an object into an instance of the type specified as arg `c`.
373374
`c` can be either primitive type, `List`, `Tuple`, `Dict` or `deserialize` class.
@@ -462,7 +463,7 @@ def deserializable_to_obj(cls):
462463
raise SerdeError(e)
463464

464465

465-
def from_dict(cls, o, reuse_instances: bool = ...):
466+
def from_dict(cls: Type[T], o, reuse_instances: bool = ...) -> T:
466467
"""
467468
Deserialize dictionary into object.
468469
@@ -485,7 +486,7 @@ def from_dict(cls, o, reuse_instances: bool = ...):
485486
return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
486487

487488

488-
def from_tuple(cls, o, reuse_instances: bool = ...):
489+
def from_tuple(cls: Type[T], o: Any, reuse_instances: bool = ...) -> T:
489490
"""
490491
Deserialize tuple into object.
491492

‎serde/msgpack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def from_msgpack(
7575
named: bool = True,
7676
ext_dict: Dict[int, Type] = None,
7777
**opts,
78-
) -> Type[T]:
78+
) -> T:
7979
"""
8080
Deserialize from MsgPack into the object.
8181

0 commit comments

Comments
 (0)
Please sign in to comment.