|
| 1 | +# Sentinels |
| 2 | + |
| 3 | +## `typing_extensions.Sentinel` |
| 4 | + |
| 5 | +Sentinels constructed with `typing_extensions.Sentinel` can be used directly in type expressions: |
| 6 | + |
| 7 | +```py |
| 8 | +from typing_extensions import Sentinel, assert_type |
| 9 | + |
| 10 | +MISSING = Sentinel("MISSING") |
| 11 | +OTHER = Sentinel("OTHER") |
| 12 | +WITH_REPR = Sentinel("WITH_REPR", "<with repr>") |
| 13 | +WITH_REPR_KEYWORD = Sentinel("WITH_REPR_KEYWORD", repr="<with repr keyword>") |
| 14 | + |
| 15 | +reveal_type(MISSING) # revealed: MISSING |
| 16 | +reveal_type(OTHER) # revealed: OTHER |
| 17 | +reveal_type(WITH_REPR) # revealed: WITH_REPR |
| 18 | +reveal_type(WITH_REPR_KEYWORD) # revealed: WITH_REPR_KEYWORD |
| 19 | + |
| 20 | +def accepts_missing(x: MISSING) -> None: ... |
| 21 | +def accepts_other(x: OTHER) -> None: ... |
| 22 | + |
| 23 | +accepts_missing(MISSING) |
| 24 | +accepts_missing(OTHER) # error: [invalid-argument-type] |
| 25 | +accepts_other(OTHER) |
| 26 | +accepts_other(MISSING) # error: [invalid-argument-type] |
| 27 | + |
| 28 | +def bad_default(x: int = MISSING) -> None: # error: [invalid-parameter-default] |
| 29 | + pass |
| 30 | + |
| 31 | +def good_default(x: int | MISSING | OTHER = MISSING) -> None: |
| 32 | + if x is MISSING: |
| 33 | + assert_type(x, MISSING) |
| 34 | + reveal_type(x) # revealed: MISSING |
| 35 | + else: |
| 36 | + assert_type(x, int | OTHER) |
| 37 | + reveal_type(x) # revealed: int | OTHER |
| 38 | + |
| 39 | +good_default(1) |
| 40 | +good_default(MISSING) |
| 41 | +good_default(OTHER) |
| 42 | + |
| 43 | +def reverse_check(x: int | MISSING | OTHER) -> None: |
| 44 | + if MISSING is x: |
| 45 | + assert_type(x, MISSING) |
| 46 | + reveal_type(x) # revealed: MISSING |
| 47 | + else: |
| 48 | + assert_type(x, int | OTHER) |
| 49 | + reveal_type(x) # revealed: int | OTHER |
| 50 | + |
| 51 | +def negative_check(x: int | MISSING | OTHER) -> None: |
| 52 | + if x is not MISSING: |
| 53 | + assert_type(x, int | OTHER) |
| 54 | + reveal_type(x) # revealed: int | OTHER |
| 55 | + else: |
| 56 | + assert_type(x, MISSING) |
| 57 | + reveal_type(x) # revealed: MISSING |
| 58 | + |
| 59 | +def reverse_negative_check(x: int | MISSING | OTHER) -> None: |
| 60 | + if MISSING is not x: |
| 61 | + assert_type(x, int | OTHER) |
| 62 | + reveal_type(x) # revealed: int | OTHER |
| 63 | + else: |
| 64 | + assert_type(x, MISSING) |
| 65 | + reveal_type(x) # revealed: MISSING |
| 66 | +``` |
| 67 | + |
| 68 | +Sentinel objects are always truthy, expose the standard sentinel metadata attributes, and are |
| 69 | +rejected as class bases: |
| 70 | + |
| 71 | +```py |
| 72 | +from typing_extensions import Sentinel |
| 73 | + |
| 74 | +MISSING = Sentinel("MISSING") |
| 75 | + |
| 76 | +reveal_type(bool(MISSING)) # revealed: Literal[True] |
| 77 | +reveal_type(MISSING.__module__) # revealed: str |
| 78 | + |
| 79 | +class MissingSubclass(MISSING): # error: [invalid-base] |
| 80 | + pass |
| 81 | +``` |
| 82 | + |
| 83 | +Sentinels declared in class scope can also be used in type expressions: |
| 84 | + |
| 85 | +```py |
| 86 | +from typing_extensions import Sentinel, assert_type |
| 87 | + |
| 88 | +class C: |
| 89 | + MARKER = Sentinel("C.MARKER") |
| 90 | + |
| 91 | +def accepts_marker(x: C.MARKER) -> None: ... |
| 92 | + |
| 93 | +accepts_marker(C.MARKER) |
| 94 | + |
| 95 | +def class_default(x: int | C.MARKER = C.MARKER) -> None: |
| 96 | + if x is C.MARKER: |
| 97 | + assert_type(x, C.MARKER) |
| 98 | + reveal_type(x) # revealed: MARKER |
| 99 | + else: |
| 100 | + assert_type(x, int) |
| 101 | + reveal_type(x) # revealed: int |
| 102 | + |
| 103 | +def class_reverse_negative(x: int | C.MARKER) -> None: |
| 104 | + if C.MARKER is not x: |
| 105 | + assert_type(x, int) |
| 106 | + reveal_type(x) # revealed: int |
| 107 | + else: |
| 108 | + assert_type(x, C.MARKER) |
| 109 | + reveal_type(x) # revealed: MARKER |
| 110 | +``` |
| 111 | + |
| 112 | +Sentinel declarations are recognized only in module and class scope: |
| 113 | + |
| 114 | +```py |
| 115 | +from typing_extensions import Sentinel |
| 116 | + |
| 117 | +def outer(): |
| 118 | + LOCAL = Sentinel("LOCAL") |
| 119 | + |
| 120 | + def inner(x: LOCAL) -> None: ... # error: [invalid-type-form] |
| 121 | +``` |
| 122 | + |
| 123 | +Sentinels are not generic: |
| 124 | + |
| 125 | +```py |
| 126 | +from typing_extensions import Sentinel |
| 127 | + |
| 128 | +MISSING = Sentinel("MISSING") |
| 129 | + |
| 130 | +def f(x: MISSING[int]) -> None: ... # error: [invalid-type-form] |
| 131 | +``` |
| 132 | + |
| 133 | +Invalid sentinel constructor calls fall back to the normal call path: |
| 134 | + |
| 135 | +```py |
| 136 | +from typing_extensions import Sentinel |
| 137 | + |
| 138 | +NAME = "NAME" |
| 139 | + |
| 140 | +NON_LITERAL_NAME = Sentinel(NAME) |
| 141 | +UNKNOWN_NAME = Sentinel(UNKNOWN) # error: [unresolved-reference] |
| 142 | +NON_LITERAL_REPR = Sentinel("NON_LITERAL_REPR", repr=NAME) |
| 143 | +UNKNOWN_REPR = Sentinel("UNKNOWN_REPR", repr=UNKNOWN) # error: [unresolved-reference] |
| 144 | +UNKNOWN_KEYWORD = Sentinel("UNKNOWN_KEYWORD", unknown=NAME) # error: [unknown-argument] |
| 145 | +``` |
0 commit comments