Modify

Ticket #42 (closed defect: wontfix)

Opened 7 years ago

Last modified 7 years ago

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

comment:1 Changed 7 years ago by xi

  • Status changed from new to closed
  • Resolution set to wontfix

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.

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


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

 
Note: See TracTickets for help on using tickets.