msgpack / msgpack-python

MessagePack serializer implementation for Python msgpack.org[Python]
https://msgpack.org/
Other
1.92k stars 230 forks source link

msgpack will pack tuples as keys, but seems not able to unpack them #418

Closed cdgriffith closed 4 years ago

cdgriffith commented 4 years ago

Ran into this issue while trying to pack up dictionaries that used tuples as keys.

import msgpack
packed = msgpack.packb({(1,2): 'value'})
# b'\x81\x92\x01\x02\xa5value'

msgpack.unpackb(packed, strict_map_key=False)
# TypeError: unhashable type: 'list'

I understand that it might not be to spec to be able to unpack them like this, but then it seems necessary to raise an error on packing if you know it will be unpackable.

While it is possible to use use_list=False to be able to unpack them, it will then change the types of all values that were lists into tuples, which is undesirable. I imagine it would be better to automatically assume any keys that were packed as arrays must have been tuples to begin with as all dictionary keys must be hashable?

Thanks!

jfolz commented 4 years ago

The MessagePack spec has only one sequence type: array. If use_list=False doesn't work for you, you can also define object_pairs_hook to make only the keys hashable.

methane commented 4 years ago

I understand your inconvenience, but I don't want to do anything, because:

but then it seems necessary to raise an error on packing if you know it will be unpackable.

It is backward incompatible change.

it would be better to automatically assume any keys that were packed as arrays must have been tuples to begin with as all dictionary keys must be hashable?

I don't want to add more complexity to the implementation only for very limited usage.

cdgriffith commented 4 years ago

@methane That is very understandable, thank you for replying so fast @jfolz Tahnks, I will look into doing that for my use case!