dacite
dacite copied to clipboard
Python 3.9+ style collections with forward references fails to resolve types
Describe the bug Collections and forward references seem to work in isolation, or when used together providing the typing.List style collection type annotation is used. When using python 3.9 style collections, dacite fails to resolve type mapping.
To Reproduce Taking the example from the docs and amending it to use a python 3.9 style list type annotation, with a forward reference to "A":
@dataclass
class A:
x: str
y: int
@dataclass
class B:
a_list: list["A"]
data = {
'a_list': [
{
'x': 'test1',
'y': 1,
},
{
'x': 'test2',
'y': 2,
}
],
}
result = from_dict(data_class=B, data=data)
assert result == B(a_list=[A(x='test1', y=1), A(x='test2', y=2)])
results in:
dacite.exceptions.WrongTypeError: wrong value type for field "a_list" - should be "list" instead of value "[{'x': 'test1', 'y': 1}, {'x': 'test2', 'y': 2}]" of type "list"
Expected behavior
To successfully resolve list["A"]
, in the same manner as with the following alternatives (e.g. all these other combinations of annotations work as expected, where typing.List is used, or forward references are avoided):
typing.List with no forward reference:
@dataclass
class B:
a_list: List[A]
typing.List with forward reference:
@dataclass
class B:
a_list: List["A"]
python 3.9+ list with no forward reference:
@dataclass
class B:
a_list: list[A]
Environment
- Python version: 3.9.13
-
dacite
version: 1.8.1
Additional context
Explicitly passing in the forward references with config=Config(forward_references={"A": A})
has no impact.