Skip to content

Commit f4aab6e

Browse files
committed
Fix recursive union
1 parent e2c8740 commit f4aab6e

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

examples/recursive_union.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List, Union
2+
from serde import serde, to_dict, InternalTagging, from_dict
3+
from dataclasses import dataclass
4+
5+
6+
@serde(tagging=InternalTagging("type"))
7+
@dataclass
8+
class Leaf:
9+
value: int
10+
11+
12+
@dataclass
13+
class Node:
14+
name: str
15+
children: List[Union[Leaf, "Node"]]
16+
17+
18+
serde(Node, tagging=InternalTagging("type"))
19+
20+
21+
def main() -> None:
22+
node1 = Node("node1", [Leaf(10)])
23+
node2 = Node("node2", [node1])
24+
d = to_dict(node2)
25+
print(f"Into dict: {d}")
26+
node = from_dict(Node, d)
27+
print(f"From dict: {node}")
28+
29+
30+
if __name__ == "__main__":
31+
main()

examples/runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import plain_dataclass_class_attribute
2626
import recursive
2727
import recursive_list
28+
import recursive_union
2829
import rename
2930
import rename_all
3031
import simple
@@ -80,6 +81,7 @@ def run_all():
8081
run(alias)
8182
run(recursive)
8283
run(recursive_list)
84+
run(recursive_union)
8385
run(class_var)
8486
run(plain_dataclass)
8587
run(plain_dataclass_class_attribute)

serde/de.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def wrap(cls: Type):
275275
# We call deserialize and not wrap to make sure that we will use the default serde
276276
# configuration for generating the deserialization function.
277277
deserialize(typ)
278-
if typ is cls or (is_primitive(typ) and not is_enum(typ)):
278+
if is_primitive(typ) and not is_enum(typ):
279279
continue
280280
if is_generic(typ):
281281
g[typename(typ)] = get_origin(typ)

serde/se.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def wrap(cls: Type[Any]):
275275
# configuration for generating the serialization function.
276276
serialize(typ)
277277

278-
if typ is cls or (is_primitive(typ) and not is_enum(typ)):
278+
if is_primitive(typ) and not is_enum(typ):
279279
continue
280280
g[typename(typ)] = typ
281281

0 commit comments

Comments
 (0)