fix: make Tags pickle-safe#1170
Conversation
a1daaed to
5d91233
Compare
There was a problem hiding this comment.
Pull request overview
Adds explicit pickle serialization support for packaging.tags.Tag, aligning it with the project’s recent “pickle-safe” work so that cached fields (like _hash) don’t break equality/hash behavior after unpickling and older pickles remain loadable.
Changes:
- Implement
Tag.__getstate__to serialize only the core(interpreter, abi, platform)fields. - Extend
Tag.__setstate__to accept the new tuple format and the older(None, {slot: value})slot-dict format. - Add tests covering round-trip behavior, invalid state rejection, and loading embedded “old-format” pickle bytes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/packaging/tags.py |
Adds __getstate__ and expands __setstate__ to support new + legacy pickle state formats while recomputing _hash. |
tests/test_tags.py |
Adds round-trip, invalid-state, and embedded-old-pickle compatibility tests for Tag. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
|
|
||
| # Pickle bytes generated with packaging==25.0, Python 3.13.1, pickle protocol 2. | ||
| # Format: plain __dict__ (no __slots__). |
There was a problem hiding this comment.
This comment says the packaging==25.0 pickle uses a plain __dict__ (no __slots__), but the embedded pickle bytes build a 2-tuple state (None, {...}) (e.g., N}...\x86), which corresponds to the __slots__-style state rather than a plain dict state. Please either regenerate the bytes from an actual 25.0 Tag pickle (and adjust Tag.__setstate__ accordingly if needed) or fix the comment/label so the test reflects the real format being exercised.
| # Format: plain __dict__ (no __slots__). | |
| # Format exercised by these bytes: __slots__-style state (None, {slot: value}). |
There was a problem hiding this comment.
This was an amazing catch, by the way. It was copied from the wrong version, I regenerated it from 25.0. (I believe this was the one I added by hand)
513d663 to
570567d
Compare
Signed-off-by: Henry Schreiner <henryfs@princeton.edu> Assisted-by: Copilot:claude-hauku-4.5
Assisted-by: Copilot:Kimi-K2.6 Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
570567d to
d099466
Compare
Similar to #1168, but for Tags.
🤖 Using Copilot (auto model)
CC @eachimei.