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_attribs=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
Description
When using recursive types (see example below),
structurewithGenConverterraises aStructureHandlerNotFoundErrorwith 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']withy: typing.Optional[Y]everything works as expected.What I Did