-
-
Notifications
You must be signed in to change notification settings - Fork 137
Add support for sentinels (PEP 661) #594
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
Changes from 4 commits
7626068
2be531e
eb0e089
6333253
fe206a4
fa321dc
a724058
03c18eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| # pyright: ignore | ||
| import abc | ||
| import builtins | ||
| import collections | ||
|
|
@@ -89,6 +90,7 @@ | |
| 'overload', | ||
| 'override', | ||
| 'Protocol', | ||
| 'Sentinel', | ||
| 'reveal_type', | ||
| 'runtime', | ||
| 'runtime_checkable', | ||
|
|
@@ -4222,6 +4224,44 @@ def evaluate_forward_ref( | |
| ) | ||
|
|
||
|
|
||
| class Sentinel: | ||
| """Create a unique sentinel object. | ||
|
|
||
| *name* should be the fully-qualified name of the variable to which the | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully-qualified? That doesn't match the PEP which does
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was copied verbatim from https://github.com/taleinat/python-stdlib-sentinels/, updated.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually the repr construction by splitting dots don't make sense anymore, so I also updated the logic in the last logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd make sense to be fully qualified inside the module, so
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This will have to be decided in the PEP discussion. |
||
| return value shall be assigned. | ||
|
|
||
| *repr*, if supplied, will be used for the repr of the sentinel object. | ||
| If not provided, "<name>" will be used (with any leading class names | ||
| removed). | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| repr: typing.Optional[str] = None, | ||
| ): | ||
| self._name = name | ||
| self._repr = repr if repr is not None else f'<{name.split(".")[-1]}>' | ||
|
|
||
| def __repr__(self): | ||
| return self._repr | ||
|
|
||
| if sys.version_info < (3, 11): | ||
| # The presence of this method convinces typing._type_check | ||
| # that Sentinels are types. | ||
| def __call__(self, *args, **kwargs): | ||
| raise TypeError(f"{type(self).__name__!r} object is not callable") | ||
|
|
||
| def __or__(self, other): | ||
| return Union[self, other] | ||
|
|
||
| def __ror__(self, other): | ||
| return Union[other, self] | ||
|
|
||
| def __getstate__(self): | ||
| raise TypeError(f"Cannot pickle {type(self).__name__!r} object") | ||
|
|
||
|
|
||
| # Aliases for items that are in typing in all supported versions. | ||
| # Explicitly assign these (rather than using `from typing import *` at the top), | ||
| # so that we get a CI error if one of these is deleted from typing.py | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.