Skip to content

Commit 0a372a0

Browse files
Use PEP 661 Sentinel for internal sentinel (#657)
1 parent 9d1637e commit 0a372a0

File tree

1 file changed

+36
-43
lines changed

1 file changed

+36
-43
lines changed

src/typing_extensions.py

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,48 @@
160160
# Added with bpo-45166 to 3.10.1+ and some 3.9 versions
161161
_FORWARD_REF_HAS_CLASS = "__forward_is_class__" in typing.ForwardRef.__slots__
162162

163-
# The functions below are modified copies of typing internal helpers.
164-
# They are needed by _ProtocolMeta and they provide support for PEP 646.
163+
class Sentinel:
164+
"""Create a unique sentinel object.
165+
166+
*name* should be the name of the variable to which the return value shall be assigned.
165167
168+
*repr*, if supplied, will be used for the repr of the sentinel object.
169+
If not provided, "<name>" will be used.
170+
"""
171+
172+
def __init__(
173+
self,
174+
name: str,
175+
repr: typing.Optional[str] = None,
176+
):
177+
self._name = name
178+
self._repr = repr if repr is not None else f'<{name}>'
166179

167-
class _Sentinel:
168180
def __repr__(self):
169-
return "<sentinel>"
181+
return self._repr
182+
183+
if sys.version_info < (3, 11):
184+
# The presence of this method convinces typing._type_check
185+
# that Sentinels are types.
186+
def __call__(self, *args, **kwargs):
187+
raise TypeError(f"{type(self).__name__!r} object is not callable")
170188

189+
# Breakpoint: https://github.com/python/cpython/pull/21515
190+
if sys.version_info >= (3, 10):
191+
def __or__(self, other):
192+
return typing.Union[self, other]
193+
194+
def __ror__(self, other):
195+
return typing.Union[other, self]
196+
197+
def __getstate__(self):
198+
raise TypeError(f"Cannot pickle {type(self).__name__!r} object")
171199

172-
_marker = _Sentinel()
173200

201+
_marker = Sentinel("sentinel")
202+
203+
# The functions below are modified copies of typing internal helpers.
204+
# They are needed by _ProtocolMeta and they provide support for PEP 646.
174205

175206
# Breakpoint: https://github.com/python/cpython/pull/27342
176207
if sys.version_info >= (3, 10):
@@ -4207,44 +4238,6 @@ def evaluate_forward_ref(
42074238
)
42084239

42094240

4210-
class Sentinel:
4211-
"""Create a unique sentinel object.
4212-
4213-
*name* should be the name of the variable to which the return value shall be assigned.
4214-
4215-
*repr*, if supplied, will be used for the repr of the sentinel object.
4216-
If not provided, "<name>" will be used.
4217-
"""
4218-
4219-
def __init__(
4220-
self,
4221-
name: str,
4222-
repr: typing.Optional[str] = None,
4223-
):
4224-
self._name = name
4225-
self._repr = repr if repr is not None else f'<{name}>'
4226-
4227-
def __repr__(self):
4228-
return self._repr
4229-
4230-
if sys.version_info < (3, 11):
4231-
# The presence of this method convinces typing._type_check
4232-
# that Sentinels are types.
4233-
def __call__(self, *args, **kwargs):
4234-
raise TypeError(f"{type(self).__name__!r} object is not callable")
4235-
4236-
# Breakpoint: https://github.com/python/cpython/pull/21515
4237-
if sys.version_info >= (3, 10):
4238-
def __or__(self, other):
4239-
return typing.Union[self, other]
4240-
4241-
def __ror__(self, other):
4242-
return typing.Union[other, self]
4243-
4244-
def __getstate__(self):
4245-
raise TypeError(f"Cannot pickle {type(self).__name__!r} object")
4246-
4247-
42484241
if sys.version_info >= (3, 14, 0, "beta"):
42494242
type_repr = annotationlib.type_repr
42504243
else:

0 commit comments

Comments
 (0)