Ticket #278 (new defect)

Opened 21 months ago

Cannot load __hash__able objects in mapping keys

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


This apparently reasonable example will fail:

import yaml

class HashableThing(yaml.YAMLObject):
    yaml_tag = u'HashableThing'

    def __init__(self, name): = name

    def __eq__(self, other):
        return isinstance(other, type(self)) and ==

    def __hash__(self):
        return hash(

y = yaml.dump({HashableThing('thing'): True})
print y
x = yaml.load(y)
print x

because the mapping constructor will try to hash() the object when that object has not yet been fully constructed.

The obvious thing to do would be to set deep=True on the construct_object call for mapping keys, but I'm not sure whether that's always the right thing to do. The current behaviour appears to only allow builtin types to be used as mapping keys.



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.