Skip to content

Commit

Permalink
Merge pull request #445 from maallaire/fix-iter_types-set
Browse files Browse the repository at this point in the history
Fix name 'Set' is not defined error
  • Loading branch information
yukinarit authored Nov 28, 2023
2 parents 1cba1da + 63b55e4 commit ee7edae
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 6 additions & 1 deletion serde/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,16 @@ def recursive(cls: TypeLike) -> None:
lst.add(Union)
for arg in type_args(cls):
recursive(arg)
elif is_list(cls) or is_set(cls):
elif is_list(cls):
lst.add(List)
args = type_args(cls)
if args:
recursive(args[0])
elif is_set(cls):
lst.add(Set)
args = type_args(cls)
if args:
recursive(args[0])
elif is_tuple(cls):
lst.add(Tuple)
for arg in type_args(cls):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def test_iter_types():
assert {Pri, int, str, float, bool} == set(iter_types(Pri))
assert {Dict, str, Pri, int, float, bool} == set(iter_types(Dict[str, Pri]))
assert {List, str} == set(iter_types(List[str]))
assert {Set, str} == set(iter_types(Set[str]))
assert {Tuple, int, str, bool, float} == set(iter_types(Tuple[int, str, bool, float]))
assert {Tuple, int, Ellipsis} == set(iter_types(Tuple[int, ...]))
assert {PriOpt, Optional, int, str, float, bool} == set(iter_types(PriOpt))
Expand All @@ -110,8 +111,9 @@ class Foo:
d: Optional[str] = None
e: Union[str, int] = 10
f: List[int] = serde.field(default_factory=list)
g: Set[int] = serde.field(default_factory=set)

assert {Foo, datetime, Optional, str, Union, List, int} == set(iter_types(Foo))
assert {Foo, datetime, Optional, str, Union, List, Set, int} == set(iter_types(Foo))


def test_iter_unions():
Expand Down
12 changes: 8 additions & 4 deletions tests/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Tests for custom serializer/deserializer.
"""
from datetime import datetime
from typing import List, Optional, Union
from typing import List, Optional, Union, Set

import pytest

Expand Down Expand Up @@ -92,14 +92,18 @@ class Foo:
d: Optional[str] = None
e: Union[str, int] = 10
f: List[int] = field(default_factory=list)
g: Set[int] = field(default_factory=set)

dt = datetime(2021, 1, 1, 0, 0, 0)
f = Foo(10, dt, dt, f=[1, 2, 3])
f = Foo(10, dt, dt, f=[1, 2, 3], g={4, 5, 6})

assert to_json(f) == '{"a":10,"b":"01/01/21","c":"01/01/21","d":null,"e":10,"f":[1,2,3]}'
assert (
to_json(f)
== '{"a":10,"b":"01/01/21","c":"01/01/21","d":null,"e":10,"f":[1,2,3],"g":[4,5,6]}'
)
assert f == from_json(Foo, to_json(f))

assert to_tuple(f) == (10, "01/01/21", "01/01/21", None, 10, [1, 2, 3])
assert to_tuple(f) == (10, "01/01/21", "01/01/21", None, 10, [1, 2, 3], {4, 5, 6})
assert f == from_tuple(Foo, to_tuple(f))

def fallback(_, __):
Expand Down

0 comments on commit ee7edae

Please sign in to comment.