-
-
Notifications
You must be signed in to change notification settings - Fork 133
Support for pickling sentinel objects as singletons #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
All commit authors signed the Contributor License Agreement.
Viicos
commented
Jun 5, 2025
Replacing
reprwithmodule_nameis technically a breaking change.
I think breaking changes are expected for draft PEPs, so imo no deprecation process should be used. Users should be aware that changes are expected. However, I think we can:
- Improve the documentation about draft PEPs, stating that implementation can change.
- Explicitly state that Sentinels are from a draft PEP in the
Sentinelclass documentation, and isn't stable yet.
Viicos
commented
Jun 5, 2025
cc @taleinat
JelleZijlstra
commented
Jun 5, 2025
I'm not willing to remove the repr parameter from typing_extensions.Sentinel without a deprecation. However, I'm OK to re-export the builtin Sentinel class in 3.15 (if it makes it in), even if it doesn't have this parameter.
HexDecimal
commented
Jun 5, 2025
I'm not willing to remove the
reprparameter fromtyping_extensions.Sentinelwithout a deprecation. However, I'm OK to re-export the builtin Sentinel class in 3.15 (if it makes it in), even if it doesn't have this parameter.
Then I will change repr into a keyword parameter.
If compatibility is important enough then I can also have the code assume module_name is actually repr if it does not refer to a existing module. Would this be necessary?
Configuration like repr would be defined only with the initial Sentinel object. It wouldn't get stored in the reduce function. This allows changes in repr to be reflected in unpickled sentinels. This means that removing repr later will not break serialization.
2c509de to
e29210a
Compare
I've replaced the __reduce__ method with a version using pickle's singleton support. This is the most conservative and inoffensive option for a new reduce function since it doesn't add a custom unpickle function, is forward compatible with any future method of pickling, is the standard method of handling singletons via pickle, and is generally strict. This does not support pickling anonymous sentinels which will now raise pickle.PicklingError instead of TypeError. I can revert this if the previous behavior was more desired, but I personally only need to pickle sentinels which have a top-level definition, and the more I work with them the more that anonymous sentinels feel counter intuitive.
I've attempted to add to the discussion of PEP 661. Anonymous sentinels bring a lot of issues which need to be addressed.
https://discuss.python.org/t/pep-661-sentinel-values/9126/251
(削除) Before this is merged I'd like to add a strict keyword parameter to Sentinel which prevents sentinels from being accidentally named after existing top-level objects which are not Sentinel as well as ensuring that repr is never given conflicting values. I'll need feedback on if this is appropriate. (削除ここまで)
Viicos
commented
Jul 2, 2025
Thanks @HexDecimal. I just tested your implementation in Pydantic to implement an UNSET sentinel, and pickling seems to work as expected.
This comment was marked as outdated.
This comment was marked as outdated.
JelleZijlstra
commented
Jul 4, 2025
Which of these makes the most sense for the above code?
I feel strongly that 2 is the right behavior. Constructing a class shouldn't look around in the globals for other stuff that might be using the same name.
HexDecimal
commented
Jul 4, 2025
Constructing a class shouldn't look around in the globals for other stuff that might be using the same name.
That was necessary to handle unpickling until I finally implemented the correct reduce function, but at this point I can remove that code now and revert to the old behavior. I'd accept that the other options are unnecessarily handholdy.
This comment was marked as outdated.
This comment was marked as outdated.
93d7b65 to
de582a1
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. @@ Coverage Diff @@ ## main #617 +/- ## ======================================= Coverage 97.38% 97.38% ======================================= Files 3 3 Lines 7689 7701 +12 ======================================= + Hits 7488 7500 +12 Misses 201 201
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
HexDecimal
commented
Dec 2, 2025
I've narrowed the scope of this PR down to only what's needed to support pickling sentinels. Unfortunately this still means that repr needs to be touched to allow for the module_name parameter. Thankfully a lot of other changes were no longer needed after this latest rebase.
Has anyone mentioned that _marker is a terribly obfuscated name for a sentinel?
de582a1 to
e7ac070
Compare
e7ac070 to
678b73b
Compare
678b73b to
9d18f86
Compare
src/typing_extensions.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a new argument is problematic because it might (again) conflict with what PEP 661 proposes. I'd be OK with adding it but I'd make it keyword-only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make module_name keyword only, but what happens to repr? Do I revert repr back to a positional parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module_name is now keyword-only.
The `repr` parameters position was replaced by `module_name` to conform to PEP 661. Added copy and pickle tests. Updated documentation for Sentinel. `_marker` was defined before `caller` which causes minor issues, resolved by setting its module name manually.
9d18f86 to
2238608
Compare
Uh oh!
There was an error while loading. Please reload this page.
My attempt at implementing PEP 661 since I was unhappy with #594. I'm hoping that this is a PR of decent quality and not just me desperately wanting pickle support.
Changes made to Sentinel:
module_nameparameter following PEP implementation and tweaking that to use the local_callerhelper function. Breaking change due toreprbecoming a keyword parameter.namerequires qualified names to support pickle singletons, this is tested and documented.__reduce__to track Sentinel by name and module_name.For a while I still supported the
reprparameter and after following the PEP 661 discussion I ended up implementing the truthiness tweaks mentioned there, but then I ended up scrapping all of that so that I could follow the PEP more closely, but they would be easy to reintroduce later if desired.I'm unsure of how to mention version changes in the docs. Replacing
reprwithmodule_nameis technically a breaking change.Closes #720