File tree Expand file tree Collapse file tree 3 files changed +31
-11
lines changed Expand file tree Collapse file tree 3 files changed +31
-11
lines changed Original file line number Diff line number Diff line change @@ -1017,6 +1017,34 @@ Capsule objects
10171017 .. versionadded :: 4.12.0
10181018
10191019
1020+ Sentinel objects
1021+ ~~~~~~~~~~~~~~~~
1022+
1023+ .. class :: Sentinel(name, repr=None)
1024+
1025+ A type used to define sentinel values. The *name * argument should be the
1026+ name of the variable to which the return value shall be assigned.
1027+
1028+ If *repr * is provided, it will be used for the :meth: `~object.__repr__ `
1029+ of the sentinel object. If not provided, ``"<name>" `` will be used.
1030+
1031+ Example::
1032+
1033+ >>> from typing_extensions import Sentinel, assert_type
1034+ >>> MISSING = Sentinel('MISSING')
1035+ >>> def func(arg: int | MISSING = MISSING) -> None:
1036+ ... if arg is MISSING:
1037+ ... assert_type(arg, MISSING)
1038+ ... else:
1039+ ... assert_type(arg, int)
1040+ ...
1041+ >>> func(MISSING)
1042+
1043+ .. versionadded :: 4.14.0
1044+
1045+ See :pep: `661 `
1046+
1047+
10201048Pure aliases
10211049~~~~~~~~~~~~
10221050
Original file line number Diff line number Diff line change @@ -9096,11 +9096,6 @@ def test_sentinel_no_repr(self):
90969096 self .assertEqual (sentinel_no_repr ._name , 'sentinel_no_repr' )
90979097 self .assertEqual (repr (sentinel_no_repr ), '<sentinel_no_repr>' )
90989098
9099- sentinel_no_repr_dots = Sentinel ('Test.sentinel_no_repr' )
9100-
9101- self .assertEqual (sentinel_no_repr_dots ._name , 'Test.sentinel_no_repr' )
9102- self .assertEqual (repr (sentinel_no_repr ), '<sentinel_no_repr>' )
9103-
91049099 def test_sentinel_explicit_repr (self ):
91059100 sentinel_explicit_repr = Sentinel ('sentinel_explicit_repr' , repr = 'explicit_repr' )
91069101
Original file line number Diff line number Diff line change 1- # pyright: ignore
21import abc
32import builtins
43import collections
@@ -4227,12 +4226,10 @@ def evaluate_forward_ref(
42274226class Sentinel :
42284227 """Create a unique sentinel object.
42294228
4230- *name* should be the fully-qualified name of the variable to which the
4231- return value shall be assigned.
4229+ *name* should be the name of the variable to which the return value shall be assigned.
42324230
42334231 *repr*, if supplied, will be used for the repr of the sentinel object.
4234- If not provided, "<name>" will be used (with any leading class names
4235- removed).
4232+ If not provided, "<name>" will be used.
42364233 """
42374234
42384235 def __init__ (
@@ -4241,7 +4238,7 @@ def __init__(
42414238 repr : typing .Optional [str ] = None ,
42424239 ):
42434240 self ._name = name
4244- self ._repr = repr if repr is not None else f'<{ name . split ( "." )[ - 1 ] } >'
4241+ self ._repr = repr if repr is not None else f'<{ name } >'
42454242
42464243 def __repr__ (self ):
42474244 return self ._repr
You can’t perform that action at this time.
0 commit comments