Ticket #252 (new defect)
Opened 11 months ago
Sub-classing yaml.YAMLObject bypasses __init__
|Reported by:||ijmcfadyen@…||Owned by:||xi|
I really like the idea of the YAML constructor that allows you to auto-magically create class instances by simply loading YAML.
In the example shown in the documentation for constructors, the "Monster" class sub-classes yaml.YAMLObject and includes an init() method.
However, in practice the init() is never called. YAMLObject.from_yaml() calls the loader.construct_yaml_object() method, which directly inserts the mapping into the instance's dict.
So why does the example code show an init() method setting the attributes of the instance?
More importantly, this means that there is no way to use init to define the full set of fields that are expected, and supply default values if they are missing from the YAML. So if I have a dozen YAML files, every one of them has to have the exact same set of fields, even if some or most of the files have either no value or a simple default value for one or more fields.
In my opinion, this greatly reduces the utility of this approach - but perhaps I am missing something here?