Skip to content

Add pickle support for Sentinel via singleton registry#732

Open
r266-tech wants to merge 1 commit intopython:mainfrom
r266-tech:sentinel-pickle-support
Open

Add pickle support for Sentinel via singleton registry#732
r266-tech wants to merge 1 commit intopython:mainfrom
r266-tech:sentinel-pickle-support

Conversation

@r266-tech
Copy link

Summary

This PR adds pickle support for typing_extensions.Sentinel objects, fixing #720.

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 behavior

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_registry keyed 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 protocols
  • test_sentinel_pickle_preserves_identity — verifies is checks pass after unpickling
  • test_sentinel_singleton — verifies same-name sentinels return the same object

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
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.

1 participant