konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 106 forks source link

Python 3.9+ style collections with forward references fails to resolve types #256

Open mwoods-familiaris opened 3 months ago

mwoods-familiaris commented 3 months ago

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

Additional context Explicitly passing in the forward references with config=Config(forward_references={"A": A}) has no impact.

mwoods-familiaris commented 3 months ago

Just to add...possibly relates to this:

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].

Ref: https://docs.python.org/3.9/library/typing.html#typing.ForwardRef