Modify ↓
Ticket #278 (new defect)
Opened 11 months ago
Cannot load __hash__able objects in mapping keys
| Reported by: | asuffiel@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: |
Description
This apparently reasonable example will fail:
import yaml
class HashableThing(yaml.YAMLObject):
yaml_tag = u'HashableThing'
def __init__(self, name):
self.name = name
def __eq__(self, other):
return isinstance(other, type(self)) and self.name == other.name
def __hash__(self):
return hash(self.name)
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.
Attachments
Note: See
TracTickets for help on using
tickets.
