Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1071,18 +1071,20 @@ Capsule objects
Sentinel objects
~~~~~~~~~~~~~~~~

.. class:: Sentinel(name, repr=None)
.. class:: sentinel(name, /)

A type used to define sentinel values. The *name* argument should be the
name of the variable to which the return value shall be assigned.

If *repr* is provided, it will be used for the :meth:`~object.__repr__`
of the sentinel object. If not provided, ``"<name>"`` will be used.
A sentinel is bound to the module it is created within,
sentinels are not equal to similar named sentinels from other modules.

Assigning attributes to a sentinel including `__weakref__` is forbidden.

Example::

>>> from typing_extensions import Sentinel, assert_type
>>> MISSING = Sentinel('MISSING')
>>> from typing_extensions import sentinel, assert_type
>>> MISSING = sentinel('MISSING')
>>> def func(arg: int | MISSING = MISSING) -> None:
... if arg is MISSING:
... assert_type(arg, MISSING)
Expand All @@ -1091,6 +1093,13 @@ Sentinel objects
...
>>> func(MISSING)

Sentinels defined inside a class scope should use a :term:`qualified name`.

Example::

>>> class MyClass:
... MISSING = sentinel('MyClass.MISSING')

.. versionadded:: 4.14.0

See :pep:`661`
Expand Down
66 changes: 46 additions & 20 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
reveal_type,
runtime,
runtime_checkable,
sentinel,
type_repr,
)

Expand Down Expand Up @@ -9541,42 +9542,67 @@ def test_invalid_special_forms(self):


class TestSentinels(BaseTestCase):
SENTINEL = sentinel("TestSentinels.SENTINEL")

def test_sentinel_no_repr(self):
sentinel_no_repr = Sentinel('sentinel_no_repr')
sentinel_no_repr = sentinel('sentinel_no_repr')

self.assertEqual(sentinel_no_repr._name, 'sentinel_no_repr')
self.assertEqual(repr(sentinel_no_repr), '<sentinel_no_repr>')
self.assertEqual(sentinel_no_repr.__name__, 'sentinel_no_repr')
self.assertEqual(repr(sentinel_no_repr), 'sentinel_no_repr')

def test_sentinel_explicit_repr(self):
sentinel_explicit_repr = Sentinel('sentinel_explicit_repr', repr='explicit_repr')
def test_sentinel_deprecated_explicit_repr(self):
sentinel_explicit_repr = sentinel('sentinel_explicit_repr', repr='explicit_repr')

self.assertEqual(repr(sentinel_explicit_repr), 'explicit_repr')

@skipIf(sys.version_info < (3, 10), reason='New unions not available in 3.9')
def test_sentinel_type_expression_union(self):
sentinel = Sentinel('sentinel')
sentinel_type = sentinel('sentinel')

def func1(a: int | sentinel = sentinel): pass
def func2(a: sentinel | int = sentinel): pass
def func1(a: int | sentinel_type = sentinel_type): pass
def func2(a: sentinel_type | int = sentinel_type): pass

self.assertEqual(func1.__annotations__['a'], Union[int, sentinel])
self.assertEqual(func2.__annotations__['a'], Union[sentinel, int])
self.assertEqual(func1.__annotations__['a'], Union[int, sentinel_type])
self.assertEqual(func2.__annotations__['a'], Union[sentinel_type, int])

def test_sentinel_not_callable(self):
sentinel = Sentinel('sentinel')
sentinel_ = sentinel('sentinel')
with self.assertRaisesRegex(
TypeError,
"'Sentinel' object is not callable"
"'sentinel' object is not callable"
):
sentinel()
sentinel_()

def test_sentinel_copy_identity(self):
self.assertIs(self.SENTINEL, copy.copy(self.SENTINEL))
self.assertIs(self.SENTINEL, copy.deepcopy(self.SENTINEL))

anonymous_sentinel = sentinel("anonymous_sentinel")
self.assertIs(anonymous_sentinel, copy.copy(anonymous_sentinel))
self.assertIs(anonymous_sentinel, copy.deepcopy(anonymous_sentinel))

def test_sentinel_picklable_qualified(self):
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
self.assertIs(self.SENTINEL, pickle.loads(pickle.dumps(self.SENTINEL, protocol=proto)))

def test_sentinel_picklable_anonymous(self):
anonymous_sentinel = sentinel("anonymous_sentinel") # Anonymous sentinel can not be pickled
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.assertRaisesRegex(
pickle.PicklingError,
r"attribute lookup anonymous_sentinel on \w+ failed|not found as \w+.anonymous_sentinel"
):
self.assertIs(anonymous_sentinel, pickle.loads(pickle.dumps(anonymous_sentinel, protocol=proto)))

def test_sentinel_deprecated(self):
with self.assertWarnsRegex(DeprecationWarning, r"Subclassing sentinel is forbidden by PEP 661"):
class SentinelSubclass(Sentinel):
pass

my_sentinel = Sentinel(name="my_sentinel")
with self.assertWarnsRegex(DeprecationWarning, r"Setting attributes on sentinel is deprecated"):
my_sentinel.foo = "bar"

def test_sentinel_not_picklable(self):
sentinel = Sentinel('sentinel')
with self.assertRaisesRegex(
TypeError,
"Cannot pickle 'Sentinel' object"
):
pickle.dumps(sentinel)

def load_tests(loader, tests, pattern):
import doctest
Expand Down
Loading
Loading