konradhalas / dacite

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

Dictionary matching errors with object type key #252

Open ali-raza-zaidi opened 7 months ago

ali-raza-zaidi commented 7 months ago

Describe the bug When trying to deserialize a dictionary into a dataclass, if the class structure includes a dictionary with its key being an object, then a WrongTypeError exception is raised.

To Reproduce

from dataclasses import dataclass
from dacite import Config, from_dict
from bson import ObjectId

@dataclass
class NiceObj:
    a: str
    b: str

@dataclass
class Foo:
    d: dict[ObjectId, NiceObj]

tc = from_dict(Foo, {'d': {'4142434445464748494a4b4c': {"a": "1", "b": "2"}}}, config=Config(cast=[ObjectId]))

print(tc)

Expected behavior In Dacite 1.7.0, this correctly gives the output

Foo(d={ObjectId('4142434445464748494a4b4c'): NiceObj(a='1', b='2')})

but in v1.8.0+ it outputs the following error

dacite.exceptions.WrongTypeError: wrong value type for field "d" - should be "dict" instead of value "{'4142434445464748494a4b4c': NiceObj(a='1', b='2')}" of type "dict"

Environment

Additional context