Ticket #169 (closed defect: wontfix)

Opened 4 years ago

Last modified 2 years ago

PyYAML fails to load mapping keys of sequence type

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

Description (last modified by xi) (diff)

The following YAML code is taken directly from an example given in the YAML 1.1 specification and fails to load with PyYAML, giving the error message: found unacceptable key (unhashable type: 'list').

? - Detroit Tigers
  - Chicago cubs
  - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]

The problem most likely arises because Python lists are not hashable and cannot be used as dict keys. A possible solution may be to detect this special case and coerce YAML !!seq types to Python tuples before constructing the dictionaries that they are a part of.


Change History

comment:1 Changed 4 years ago by xi

  • Status changed from new to closed
  • Resolution set to wontfix
  • Description modified (diff)

Using PyYAML API, you could provide a custom loader for !!dict nodes that handles this case in an application-specific way. However I think raising an error is a sane default behavior.

comment:2 Changed 2 years ago by anonymous

As it's a bit tricky at first to get this stuff working, I figured I'd share my solution. I'm assuming you occasionally have keys with sequences and want the keys to convert to tuples, here's  one way to do it.

Most of what I'm really working at is figuring out how the tags work. For a lot of applications, the right way to handle mappings is through custom ADTs where you specify exactly what it means for A to map to B in a reproducible manner.

For a lot of the YAML use cases, you might even go so far as to simply make all values map to immutable types. (A standard 'frozendict' would be a big help here.)


Add a comment

Modify Ticket

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

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

Note: See TracTickets for help on using tickets.