Ticket #252 (new defect)

Opened 22 months ago

Last modified 7 months ago

Sub-classing yaml.YAMLObject bypasses __init__

Reported by: ijmcfadyen@… Owned by: xi
Priority: normal Component: pyyaml
Severity: normal Keywords:



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?



Change History

comment:1 Changed 7 months ago by koukopoulos@…

Did this issue get any attention? I agree with ijmcfadyen above. The documentation doesn't fit reality and reality could be improved.


Add a comment

Modify Ticket

Change Properties
<Author field>
as new
as The resolution will be set. Next status will be 'closed'
to The owner will be changed from xi. Next status will be 'new'
The owner will be changed from xi to anonymous. Next status will be 'assigned'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.