Ticket #169 (closed defect: wontfix)
PyYAML fails to load mapping keys of sequence type
| Reported by: | sedie@… | Owned by: | xi |
|---|---|---|---|
| Priority: | normal | Component: | pyyaml |
| Severity: | normal | Keywords: | |
| Cc: |
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.
Attachments
Change History
comment:1 Changed 3 years ago by xi
- Status changed from new to closed
- Resolution set to wontfix
- Description modified (diff)
comment:2 Changed 14 months 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.)

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.