Skip to content

fix: make Requirements/Markers pickle-safe#1171

Merged
henryiii merged 4 commits intopypa:mainfrom
henryiii:henryiii/fix/picklenode
Apr 24, 2026
Merged

fix: make Requirements/Markers pickle-safe#1171
henryiii merged 4 commits intopypa:mainfrom
henryiii:henryiii/fix/picklenode

Conversation

@henryiii
Copy link
Copy Markdown
Contributor

Similar to #1168, but for Requirements/Markers.

🤖 Using Copilot (auto model)

@henryiii henryiii marked this pull request as draft April 20, 2026 14:39
@henryiii
Copy link
Copy Markdown
Contributor Author

Needs #1168, since Specifiers are contained inside.

@henryiii henryiii force-pushed the henryiii/fix/picklenode branch from 2824080 to 6cd4665 Compare April 23, 2026 03:35
@henryiii henryiii marked this pull request as ready for review April 23, 2026 05:52
@henryiii henryiii requested a review from Copilot April 23, 2026 19:40
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds pickle stability/backwards-compatibility support for Requirement, Marker, and marker parser Node objects (similar to prior Specifier/Version work), along with tests that ensure both new and legacy pickle formats round-trip correctly.

Changes:

  • Implement __getstate__/__setstate__ for Requirement, Marker, and _parser.Node to support compact, stable pickling plus legacy state formats.
  • Add pytest coverage for pickle round-trips and loading pickles generated by older packaging versions.
  • Update typos configuration to silence false positives from embedded pickle byte streams.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/packaging/requirements.py Pickle state now serializes as a requirement string; legacy __dict__ restore supported.
src/packaging/markers.py Pickle state now serializes as a marker expression string; legacy dict/slots restore supported.
src/packaging/_parser.py Adds pickle support for marker AST node types via string state + legacy restore.
tests/test_requirements.py Adds round-trip + legacy pickle fixture tests for Requirement.
tests/test_markers.py Adds round-trip + legacy pickle fixture tests for Marker and Node types.
pyproject.toml Extends typos allowlist to accommodate pickle-byte literals in tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/packaging/markers.py Outdated
Comment thread src/packaging/_parser.py Outdated
Comment thread pyproject.toml Outdated
Comment thread src/packaging/requirements.py
Comment thread src/packaging/_parser.py Outdated
self._restore_value(slot_dict["value"])
return
if isinstance(state, dict) and "value" in state:
# Old format (packaging <= 26.0, no __slots__): plain __dict__.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be < 26.0, slots were added in 26.0, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Marker is the only one we missed and added in 26.1. (#1147)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, this is Node, not Marker, sorry. Yes, I think you are right (#1032).

Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Assisted-by: OpenCode:Qwen3.6-27B
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Assisted-by: Copilot:Kimi-K2.6
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
Signed-off-by: Henry Schreiner <henryfs@princeton.edu>
@henryiii henryiii force-pushed the henryiii/fix/picklenode branch from 06f9df6 to b880d50 Compare April 24, 2026 13:26
@henryiii henryiii merged commit 48a8a06 into pypa:main Apr 24, 2026
57 checks passed
@henryiii henryiii deleted the henryiii/fix/picklenode branch April 24, 2026 13:44
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.

3 participants