konradhalas / dacite

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

Add missing value factory for from_dict() #238

Open evan0greenup opened 1 year ago

evan0greenup commented 1 year ago

Is your feature request related to a problem? Please describe. When there is missing value in dict, it will raise Error, this is good for majority scenario but sometimes user want to give the attribute with default value for the specific type via default factory callable.

It would be nice to add this support in Config.

Describe the solution you'd like Add default_factory kwarg in Config which is type dict[type, Callable[[], Any]. the return value of callable is the instance of the key type.

The default value of default_factory is None, which will raise Error when the key is missing.

When the missing value is the type beyond the keys in default_factory, it will also raise Error.

Describe alternatives you've considered ...

Additional context