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
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.
The text was updated successfully, but these errors were encountered:
Note PEP 585 generic types such as list["SomeClass"] will not be implicitly transformed into list[ForwardRef("SomeClass")] and thus will not automatically resolve to list[SomeClass].
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":
results in:
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:
typing.List with forward reference:
python 3.9+ list with no forward reference:
Environment
dacite
version: 1.8.1Additional context
Explicitly passing in the forward references with
config=Config(forward_references={"A": A})
has no impact.The text was updated successfully, but these errors were encountered: