-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #345 from yukinarit/fix-recursive-union
Fix recursive union
- Loading branch information
Showing
4 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters