-
-
Notifications
You must be signed in to change notification settings - Fork 119
Refactor Sentinel to conform to PEP 661 #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
base: main
Are you sure you want to change the base?
Conversation
`repr` parameter removed, explicit repr tests removed `__repr__` modified to match PEP implementation (removed angle brackets) Added `module_name` parameter following PEP implementation and tweaking to use `_caller` helper function. `name` required support for qualified names to follow PEP implementation. Added `__reduce__` to track Sentinel by name and module_name. Added a Sentinel registry to preserve Sentinel identity across multiple calls to the class. Added tests for this. Added an import step to allow forward compatibility with other sentinel libraries. Import step is tested. This was not required by the PEP but it is required for typing_extensions to have a forward compatible type. Added copy and pickle tests. Updated documentation for Sentinel.
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:
|
cc @taleinat |
I'm not willing to remove the |
Then I will change If compatibility is important enough then I can also have the code assume Configuration like |
Adds tests for both the keyword and old positional repr parameters
Changes in Sentinel such as swapping it with a theoretical typing.Sentinel will affect private methods, so the callable used with `__reduce__` must be at the top-level to be stable.
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:
repr
parameter removed, explicit repr tests removed,repr
was rejected by the PEP__repr__
modified to match PEP implementation (removed angle brackets)module_name
parameter following PEP implementation and tweaking that to use the local_caller
helper function.name
required support for qualified names to follow PEP and ensure forward compatibility, this is tested. Unless sentinels in class scopes are forbidden then qualified names are not optional.__reduce__
to track Sentinel by name and module_name.This is not from the PEP but it is required for typing_extensions to be forward compatible with any official sentinel type.
For a while I still supported the
repr
parameter 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.If you don't like the current
__reduce__
function relying on the class directly then I can modify it to rely on a private classmethod instead which will ensure that any later changes to__new__
or__reduce__
won't cause issues. That said it seems thename, module_name
parameters are one of the few things most have agreed on.The
_import_sentinel
staticmethod can be moved if desired.I'm unsure of how to mention version changes in the docs. Replacing
repr
withmodule_name
is technically a breaking change._sentinel_registry.setdefault
is potentially supposed to be protected with a lock but I'm unsure of if I can import thethreading
module without causing platform issues._sentinel_registry
could be replaced with a weak values dictionary but I'm unsure if that's necessary due to how permanent most sentinels are.