Enhancement: Add support for Python's dataclass
ruamel.yaml is a YAML 1.2 parser/emitter for Python
Brought to you by:
anthon
According to this StackOverflow question: https://stackoverflow.com/questions/51529458/when-ruamel-yaml-loads-dataclass-from-string-post-init-is-not-called ruamel.yaml doesn't fully support Python's dataclasses. The call to __post_init__ is missing.
I also checked the change logs of this projects, which is very details, but adding support for dataclasses is not mentioned. Therefore I request to add this support :).
Support can be implemented in 2 steps:
dataclasses.is_dataclass(obj)) and __post_init__ is defined, then call it.InitVar, then pass these parameters to __post_init__Kind regards
Patrick
Is this being considered for implementation?
Probably not in the base packages, but it could be added as a plugin. I would have to review (and update) the answer on the SO issue, but since that answer has not been accepted, I am not even sure it solves the OP's issue as is. Did you use that?
I think the suggestion in the top SO answer is a reasonable one:
That would enable users of dataclasses to use @yaml_object
The initial support for this is there, post_init() is called on registered dataclasses that get loaded.
InitVar is now specifically supported
Last edit: Anthon van der Neut 2023-10-04
Awesome, thanks for addressing this