konradhalas / dacite

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

dict cannot be matched if keys are specified as enums in dataclass #239

Open gryznar opened 1 year ago

gryznar commented 1 year ago

Describe the bug

dict is not matched if its keys are enums

To Reproduce

from enum import Enum
from dacite import Config, from_dict

class Bar(Enum):
    BAR_1 = 1
    BAR_2 = 2

@dataclass
class Foo:
    d: dict[Bar, str]

from_dict(Foo, {'d': {1: "1"}}, config=Config(cast=[Enum]))

Expected behavior

dataclass is created

Actual behavior

dacite.exceptions.WrongTypeError: wrong value type for field "d" - should be "dict" instead of value "{1: '1'}" of type "dict"

Environment

vrmaroli commented 11 months ago

Is there a workaround for this?

ali-raza-zaidi commented 7 months ago

Facing similar issues. Was hoping to get this fixed if possible

clintlombard commented 7 months ago

@ali-raza-zaidi I had a similar issue, and ended up switching to msgspec to solve it.