-
-
Notifications
You must be signed in to change notification settings - Fork 120
Open
Description
- cattrs version: 1.8.0 (attrs 21.2.0)
- Python version: 3.9.7
- Operating System: Windows 10
Description
When using recursive types (see example below), structure
with GenConverter
raises a StructureHandlerNotFoundError
with the message "Unsupported type: ForwardRef('Y'). Register a structure hook for it"
.
The error does not happen with PEP 563 enabled. That means when we add 'from future import annotations' to the program and
replace y: typing.Optional['Y']
with y: typing.Optional[Y]
everything works as expected.
What I Did
import typing
import cattr
import attr
@attr.define(auto_attribs=True)
class X:
a: int
b: float
y: typing.Optional['Y'] #with PEP563 enabled: y: typing.Optional[Y] (that does not raise an exception)
@attr.define(auto_a
5744
ttribs=True)
class Y:
a: typing.List[int]
x: X
converter = cattr.GenConverter()
data = { 'a': [1,2,3], 'x': {'a':1, 'b':2.000001, 'y': {'a': [], 'x': {'a': 5, 'b': 3.1, 'y': None}}} }
d = converter.structure( data, Y ) # <---- raises StructureHandlerNotFoundError Unsupported type: ForwardRef('Y'). Register a structure hook for it
Metadata
Metadata
Assignees
Labels
No labels