Modify ↓
Ticket #42 (closed defect: wontfix)
List objects are unhashable
| Reported by: | dfnord@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | major | Keywords: | |
| Cc: |
Description
Even though yaml specification allows lists ("flow collections on yaml jargon") to be keys, pyaml fails to use them, as lists are unhashable.
For instance, loading "[1, 2]: something" gives the error "found unacceptable key (list objects are unhashable)"
As a away around this, flow collections could be translated into a simple hasheable subclass of list, such as:
class HasheableList(list):
#############################
## Public Methods
#############################
def __hash__(self):
return hash(tuple(self))
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

There are many ways to create a YAML document that could not be directly translated to a Python object, "lists as keys" is the only one of them. PyYAML creates Python objects when there exists a reasonable and straightforward way to do it, otherwise it complains. The correct way to fix this issue is to provide custom constructors using add_*_resolver and add_*_constructor API; PyYAML is configurable enough to allow you to change the way YAML nodes are converted to Python objects. Since there are many way to fix it, it's your responsibility to specify the way you want it to be done.