Skip to content

Commit

Permalink
Merge pull request #345 from yukinarit/fix-recursive-union
Browse files Browse the repository at this point in the history
Fix recursive union
  • Loading branch information
yukinarit committed May 20, 2023
2 parents e2c8740 + f4aab6e commit 07f0cdc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
31 changes: 31 additions & 0 deletions examples/recursive_union.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List, Union
from serde import serde, to_dict, InternalTagging, from_dict
from dataclasses import dataclass


@serde(tagging=InternalTagging("type"))
@dataclass
class Leaf:
value: int


@dataclass
class Node:
name: str
children: List[Union[Leaf, "Node"]]


serde(Node, tagging=InternalTagging("type"))


def main() -> None:
node1 = Node("node1", [Leaf(10)])
node2 = Node("node2", [node1])
d = to_dict(node2)
print(f"Into dict: {d}")
node = from_dict(Node, d)
print(f"From dict: {node}")


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions examples/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import plain_dataclass_class_attribute
import recursive
import recursive_list
import recursive_union
import rename
import rename_all
import simple
Expand Down Expand Up @@ -80,6 +81,7 @@ def run_all():
run(alias)
run(recursive)
run(recursive_list)
run(recursive_union)
run(class_var)
run(plain_dataclass)
run(plain_dataclass_class_attribute)
Expand Down
2 changes: 1 addition & 1 deletion serde/de.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def wrap(cls: Type):
# We call deserialize and not wrap to make sure that we will use the default serde
# configuration for generating the deserialization function.
deserialize(typ)
if typ is cls or (is_primitive(typ) and not is_enum(typ)):
if is_primitive(typ) and not is_enum(typ):
continue
if is_generic(typ):
g[typename(typ)] = get_origin(typ)
Expand Down
2 changes: 1 addition & 1 deletion serde/se.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def wrap(cls: Type[Any]):
# configuration for generating the serialization function.
serialize(typ)

if typ is cls or (is_primitive(typ) and not is_enum(typ)):
if is_primitive(typ) and not is_enum(typ):
continue
g[typename(typ)] = typ

Expand Down

0 comments on commit 07f0cdc

Please sign in to comment.