konradhalas / dacite

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

Fails to initialize IntEnum from int #182

Closed TBS1996 closed 1 year ago

TBS1996 commented 2 years ago

an IntEnum will be saved in json as a simple int, when loading a dataclass with an IntEnum it should be able to recognize the int as an intenum, but instead i get an error.

gmonacho commented 2 years ago

did you try to use dacite.Config to automatically cast the integer to Enum ?

class FooEnum(IntEnum):
   value = 1

@dataclass
class Foo:
   foo_enum: FooEnum

dacite.from_dict(data_class=Foo, data={'foo_enum': 1}, config=dacite.Config(cast=[FooEnum])

Dacite will then automatically cast json field foo_enum to FooEnum type.

konradhalas commented 1 year ago

@gmonacho thank you for the good answer :)