fix: make Specifier / SpecifierSet pickle-safe#1168
fix: make Specifier / SpecifierSet pickle-safe#1168
Conversation
8e44fef to
0a5742b
Compare
|
Looks good! 👍 |
|
Yes, I usually start the tuple with an "format version" integer. But I don't think this will be likely to change much, and simply adding a value is safe. Longer term, we could probably use pattern matching here if it's not too much slower. |
|
Indeed, sounds good |
3954b39 to
3c2603d
Compare
a56da7e to
3799a8a
Compare
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Assisted-by: Copilot:claude-sonnet-4.6 Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Assisted-by: OpenCode:Kimi-K2.5 Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Assisted-by: Copilot:claude-sonnet-4.6 Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
3799a8a to
4cca7ec
Compare
There was a problem hiding this comment.
Pull request overview
This PR extends the pickle-safety/backward-compatibility work from #1163 to Specifier and SpecifierSet, aiming to ensure pickles remain loadable across packaging versions and don’t persist cache/internal state.
Changes:
- Add
__getstate__/__setstate__toSpecifierandSpecifierSet, using compact tuple state and discarding caches on restore while supporting older pickle formats. - Add pickle roundtrip tests for
Specifier/SpecifierSet. - Add fixture-based tests to load historical pickle payloads (packaging 25.0, 26.0, 26.1, 26.2-style).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/packaging/specifiers.py |
Implements compact pickle state + legacy restore logic for Specifier and SpecifierSet, clearing caches on restore. |
tests/test_specifiers.py |
Adds roundtrip and historical-pickle loading tests for specifiers/specifiersets across multiple packaging versions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The copilot review boils down to one thing: protect against malformed pickles. I've added it (generated tests, but threw away the model-implemented solution and added my own), but I'm not sure it's really needed to always check this (auto-generated pickle code doesn't have such checks), but I think it's probably a good idea for the "old" pickles. I've added it for everything, but I'd also be fine to drop it for new-style pickles (outer tuple). It's not too expensive, though, and it does make typing happy. Pattern matching would be so nice here. :) |
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
47b00d5 to
cab5c2c
Compare
|
Also, I've followed #1163 in generating old pickles and storing them, but we could also just store the intermediate state, which wouldn't be a somewhat opaque binary string, and would unit test the same. I think the current one is a bit better, but if there's a desire to avoid binary strings, we could make something work. |
This is a followup to #1163. I've had an model apply the same changes/logic in that PR to Specifier/SpecifierSet. I think this is likely in better shape, due to not referncing a missing module, but should be better long term.
I also had it generate similar tests for several packaging versions.
🤖 Assisted by both OpenCode w/ Kimi-K2.5 & Copilot CLI w/ (new for CLI) auto model selection (claude-sonnet-4.6).
CC @eachimei.