Add pickle support for Sentinel via singleton registry#732
Open
r266-tech wants to merge 1 commit intopython:mainfrom
Open
Add pickle support for Sentinel via singleton registry#732r266-tech wants to merge 1 commit intopython:mainfrom
r266-tech wants to merge 1 commit intopython:mainfrom
Conversation
Sentinel objects now support pickling/unpickling using a module-level registry keyed by (module_name, name). Unpickled sentinels preserve object identity (is checks pass). Changes: - Add _sentinel_registry dict for singleton tracking - Convert __init__ to __new__ with registry-based singleton pattern - Replace __getstate__ (which raised TypeError) with __reduce__ - __reduce__ returns (cls, (name, None, module_name)) for reconstruction - Auto-detect calling module via inspect.currentframe() - Update tests: verify pickle roundtrip, identity preservation, singleton Implements the approach discussed in issue python#720, following the PEP 661 reference implementation's singleton registry pattern. Fixes python#720
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds pickle support for
typing_extensions.Sentinelobjects, fixing #720.Sentinel objects now support pickling/unpickling using a module-level registry keyed by
(module_name, name). Unpickled sentinels preserve object identity (ischecks pass).Changes
_sentinel_registrydict for singleton tracking__init__to__new__with registry-based singleton pattern__getstate__(which raisedTypeError) with__reduce____reduce__returns(cls, (name, None, module_name))for reconstructioninspect.currentframe()Approach
This implements the singleton registry pattern discussed in the issue and consistent with the PEP 661 reference implementation. When a sentinel is created, it is registered in
_sentinel_registrykeyed by(module_name, name). On unpickling,Sentinel(name, module_name=module_name)is called, which looks up the existing singleton from the registry rather than creating a new object.Motivation
As noted in #720, pickle support is expressed as a motivation in PEP 661. The current explicit rejection (
__getstate__raises TypeError) blocks legitimate use cases where sentinel values need to be serialized. Singletons are inherently forward-compatible — they simply reference an object by its qualified name and do not care what that object is later replaced with.Tests
All 554 existing tests pass (13 skipped for version-specific features). New tests added:
test_sentinel_picklable— verifies pickle roundtrip across all protocolstest_sentinel_pickle_preserves_identity— verifiesischecks pass after unpicklingtest_sentinel_singleton— verifies same-name sentinels return the same object