You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
can't structure dataclass with PEP563 postponed annotations or forward references #195
I would like to use cattrs with dataclasses (for this project I can't use attrs classes). I get the following errors when I try to structure a dataclass with circular, nested definitions that contain string literals to be post-evaluated (e.g. via typing.get_type_hints).
It seems that this currently only works with attrs classes, but not dataclasses.
What I Did
from __future__ importannotationsfromtypingimportListfromdataclassesimportdataclass, fieldfromcattrimportConverter, GenConverter@dataclassclassNode:
children: List[Node] =field(default_factory=list)
tree=Node([Node([Node(), Node()]), Node()])
c=GenConverter()
data=c.unstructure(tree)
# Raises: NotAnAttrsClassError: <class '__main__.Node'> is not an attrs-decorated class.c.structure(data, Node)
c=Converter()
data=c.unstructure(tree)
# Raises: StructureHandlerNotFoundError: Unsupported type: List[Node]. Register a structure hook for it.c.structure(data, Node)
If I remove from __future__ import annotations and type the nested children: List["Node"] I get this other error:
StructureHandlerNotFoundError: Unsupported type: ForwardRef('Node'). Register a structure hook for it.
Hm, for attrs classes I use attr.resolve_types, and that doesn't exist for dataclasses. I can try using typing.get_type_hints directly, but that won't cover some of the use cases (like when you need to resolve them yourself, in a special way).
yeah, I suppose that's the way to go. You can have a resolve_types in _compat that delegates to attrs.resolve_types for attrs classes or does the same thing for dataclasses.
won't cover some of the use cases (like when you need to resolve them yourself, in a special way)
So for example cattrs doing resolve_types on a class can fail. That's why resolve_types takes some arguments so you can help it before you actually use the class.
Also resolve_types is a stateful change - it updates the fields. get_type_hints just returns the annotations. It'll probably be enough to start though.
Activity
anthrotype commentedon Dec 7, 2021
Note the same happens with the newly released cattrs 1.9.0
Tinche commentedon Dec 7, 2021
Hm, for attrs classes I use
attr.resolve_types, and that doesn't exist for dataclasses. I can try usingtyping.get_type_hintsdirectly, but that won't cover some of the use cases (like when you need to resolve them yourself, in a special way).anthrotype commentedon Dec 7, 2021
yeah, I suppose that's the way to go. You can have a
resolve_typesin _compat that delegates to attrs.resolve_types for attrs classes or does the same thing for dataclasses.not sure what you mean by that
Tinche commentedon Dec 7, 2021
So for example cattrs doing
resolve_typeson a class can fail. That's whyresolve_typestakes some arguments so you can help it before you actually use the class.Also
resolve_typesis a stateful change - it updates the fields.get_type_hintsjust returns the annotations. It'll probably be enough to start though.Tinche commentedon Dec 12, 2021
I just pushed support for this to the main branch, feel free to test it out.
anthrotype commentedon Dec 13, 2021
Thanks, it works now!
chore(deps): update cattrs requirement from <1.10,>=1.8 to >=1.8,<1.1…
chore(deps): Update cattrs requirement from <1.11,>=1.8 to >=1.8,<22.…