seandstewart / typical

Typical: Fast, simple, & correct data-validation using Python 3 typing.
https://python-typical.org
MIT License
182 stars 9 forks source link

Recursive type with List[] causing RecursionError #136

Closed gkaemmer closed 3 years ago

gkaemmer commented 3 years ago

Description

Trying to use typical to serialize/deserialize a type that has a recursive field of type List:

@dataclasses.dataclass
class Node:
    children: List["Node"]

typic.transmute(Node, {"children": []})
# => raises RecursionError: maximum recursion depth exceeded

What I Did

Tried using "List[Node]" as the type, and from __future__ import annotations:

@dataclasses.dataclass
class Node:
    children: "List[Node]"

typic.transmute(Node, {"children": []})
# => raises RecursionError: maximum recursion depth exceeded

from __future__ import annotations
@dataclasses.dataclass
class Node:
    children: List[Node]

typic.transmute(Node, {"children": []})
# => raises RecursionError: maximum recursion depth exceeded

With Optional instead of List, it seems to work:

@dataclasses.dataclass
class Node:
    child: Optional["Node"]

typic.transmute(Node, {"children": Node})
# => Node(child=None)