|
160 | 160 | # Added with bpo-45166 to 3.10.1+ and some 3.9 versions |
161 | 161 | _FORWARD_REF_HAS_CLASS = "__forward_is_class__" in typing.ForwardRef.__slots__ |
162 | 162 |
|
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. |
165 | 167 |
|
| 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}>' |
166 | 179 |
|
167 | | -class _Sentinel: |
168 | 180 | 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") |
170 | 188 |
|
| 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") |
171 | 199 |
|
172 | | -_marker = _Sentinel() |
173 | 200 |
|
| 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. |
174 | 205 |
|
175 | 206 | # Breakpoint: https://github.com/python/cpython/pull/27342 |
176 | 207 | if sys.version_info >= (3, 10): |
@@ -4207,44 +4238,6 @@ def evaluate_forward_ref( |
4207 | 4238 | ) |
4208 | 4239 |
|
4209 | 4240 |
|
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 | | - |
4248 | 4241 | if sys.version_info >= (3, 14, 0, "beta"): |
4249 | 4242 | type_repr = annotationlib.type_repr |
4250 | 4243 | else: |
|
0 commit comments