konradhalas / dacite

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

Ability to customize from_dict per data class #122

Open trojkat opened 3 years ago

trojkat commented 3 years ago

In some cases when loading a nested structure of data classes you might need to do a customization for some of them. This change allows customizing a from_dict method for each data class.

Code sample from the test

@dataclass
class X:
    d: date
    t: str

    def from_dict(data_class, data, config):
        data["t"] = "prefix {}".format(data["t"])
        return from_dict(
            data_class=data_class,
            data=data,
            config=Config(type_hooks={date: date.fromtimestamp}),
        )

@dataclass
class Y:
    d: date
    x: X
MeTaNoV commented 3 years ago

would be nice indeed!

mwerlberger commented 3 years ago

Is there any plan to land these changes? I would very much like this feature to be landed. Or is there a good workaround to achieve the same behavior?

trojkat commented 3 years ago

@mwerlberger you can use my fork for now https://pypi.org/project/tonalite/

trojkat commented 1 year ago

@konradhalas I've updated the MR as I can see some life in this repository again.