Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1075,12 +1075,12 @@ Capsule objects
Sentinel objects
~~~~~~~~~~~~~~~~

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

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.

Assigning attributes to a sentinel is deprecated.
Assigning attributes to a sentinel is deprecated (except for __module__).
Comment thread
zedzhen marked this conversation as resolved.

Example::

Expand All @@ -1106,7 +1106,7 @@ Sentinel objects
Now supports pickle and will be reduced as a singleton.
Renamed from `Sentinel` to `sentinel`, `Sentinel` is deprecated.
Automatic `repr` string no longer has angle brackets.
`repr` parameter was deprecated.
`repr` as a positional argument is deprecated.
`name` as a keyword is deprecated.
Subclassing and attribute assignment are deprecated.

Expand Down
13 changes: 9 additions & 4 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9599,11 +9599,16 @@ def test_sentinel_no_repr(self):
self.assertEqual(sentinel_no_repr.__name__, 'sentinel_no_repr')
self.assertEqual(repr(sentinel_no_repr), 'sentinel_no_repr')

def test_sentinel_deprecated_explicit_repr(self):
with self.assertWarnsRegex(DeprecationWarning, r"'repr' parameter is deprecated and will be removed"):
sentinel_explicit_repr = sentinel('sentinel_explicit_repr', repr='explicit_repr')
def test_sentinel_deprecated_argument_repr(self):
with self.assertWarnsRegex(DeprecationWarning, r"Passing 'repr' as a position argument is deprecated; pass it by keyword instead."):
sentinel_argument_repr = sentinel('sentinel_argument_repr', 'argument_repr')

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

def test_sentinel_deprecated_keyword_repr(self):
Comment thread
zedzhen marked this conversation as resolved.
Outdated
sentinel_keyword_repr = sentinel('sentinel_keyword_repr', repr='keyword_repr')

self.assertEqual(repr(sentinel_keyword_repr), 'keyword_repr')

@skipIf(sys.version_info < (3, 10), reason='New unions not available in 3.9')
def test_sentinel_type_expression_union(self):
Expand Down
10 changes: 6 additions & 4 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,10 @@ class sentinel:
def __init__(
self,
__name: str = _sentinel_placeholder,
__repr: typing.Optional[str] = _sentinel_placeholder,
/,
repr: typing.Optional[str] = None,
*,
repr: typing.Optional[str] = None,
name: str = _sentinel_placeholder,
) -> None:
if name is not _sentinel_placeholder:
Expand All @@ -204,13 +205,14 @@ def __init__(
__name = name
if __name is _sentinel_placeholder:
raise TypeError("First parameter 'name' is required")
if repr is not None:
if __repr is not _sentinel_placeholder:
warnings.warn(
"The 'repr' parameter is deprecated "
"and will be removed in Python 3.15.",
"Passing 'repr' as a position argument is deprecated; "
Comment thread
zedzhen marked this conversation as resolved.
Outdated
"pass it by keyword instead.",
DeprecationWarning,
stacklevel=2,
)
repr = __repr

self.__name__ = __name
self._repr = repr if repr is not None else __name
Expand Down
Loading