Skip to content

Handle dicts with unsortable keys in serialize_object - #1058

Open
thelastbackspace wants to merge 1 commit into
ipython:mainfrom
thelastbackspace:fix/1057-serialize-mixed-dict-keys
Open

thelastbackspace wants to merge 1 commit into
ipython:mainfrom
thelastbackspace:fix/1057-serialize-mixed-dict-keys

Conversation

@thelastbackspace

Copy link
Copy Markdown

Fixes #1057

serialize_object and deserialize_object walk small dicts (fewer than MAX_ITEMS entries) with sorted(), so something like {'A': 1, 2: 2} raises TypeError: '<' not supported between instances of 'int' and 'str' and can't be sent to engines at all.

On the serialize side I kept sorted() and only fall back to insertion order when sorting raises, so anything that worked before produces exactly the same message. On the deserialize side I just walk the unpickled dict in its own order instead of re-sorting. That dict is the cobj that serialize_object built, so its order is the order the buffers were pulled out in. For messages from older clients that's still sorted order, so mixed versions keep working.

Testing:

python -m pytest ipyparallel/tests/test_serialize.py

Added two tests: a plain mixed-key dict, and a mixed-key dict holding two numpy arrays big enough to go out as separate buffers, to check each one comes back under the right key. Both fail on main with the TypeError and pass with this change.

The full test suite passes apart from a couple of cluster tests that fail intermittently here with or without this change.

Thanks to @raisingchris2026 for the report and the suggested diff.

serialize_object/deserialize_object walked small dicts with sorted(),
which raises TypeError for mixed key types like {'A': 1, 2: 2}.
Fall back to insertion order when sorting fails, and walk the unpickled
dict in its own order on the way back, which is the order the buffers
were extracted in.

Closes ipython#1057

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sending a dict whose keys can't be sorted together fails with TypeError

1 participant