| 1 |
""" |
| 2 |
This module provides stubs for type hints not supported by all relevant Python |
| 3 |
versions. |
| 4 |
|
| 5 |
NOTICE: This project should have zero required dependencies, which means it |
| 6 |
cannot simply require :module:`typing_extensions`, and I do not want to maintain |
| 7 |
a vendored copy of :module:`typing_extensions`. |
| 8 |
""" |
| 9 |
|
| 10 |
import functools |
| 11 |
import warnings |
| 12 |
from typing import ( |
| 13 |
Any, |
| 14 |
Callable, # Replaced by `collections.abc.Callable` in 3.9.2. |
| 15 |
Optional, # Replaced by `X | None` in 3.10. |
| 16 |
TypeVar) |
| 17 |
try: |
| 18 |
from typing import AnyStr # Removed in 3.18. |
| 19 |
except ImportError: |
| 20 |
AnyStr = TypeVar('AnyStr', str, bytes) # type: ignore[misc] |
| 21 |
try: |
| 22 |
from typing import Never # Added in 3.11. |
| 23 |
except ImportError: |
| 24 |
from typing import NoReturn as Never |
| 25 |
|
| 26 |
F = TypeVar('F', bound=Callable[..., Any]) |
| 27 |
|
| 28 |
try: |
| 29 |
from warnings import deprecated # Added in 3.13. # type: ignore |
| 30 |
except ImportError: |
| 31 |
try: |
| 32 |
from typing_extensions import deprecated # type: ignore |
| 33 |
except ImportError: |
| 34 |
def deprecated( # type: ignore[no-redef] |
| 35 |
message: str, |
| 36 |
/, *, |
| 37 |
category: Optional[type[Warning]] = DeprecationWarning, |
| 38 |
stacklevel: int = 1, |
| 39 |
) -> Callable[[F], F]: |
| 40 |
def decorator(f: F) -> F: |
| 41 |
@functools.wraps(f) |
| 42 |
def wrapper(*a, **k): |
| 43 |
warnings.warn(message, category=category, stacklevel=stacklevel+1) |
| 44 |
return f(*a, **k) |
| 45 |
return wrapper # type: ignore[return-value] |
| 46 |
return decorator |
| 47 |
|
| 48 |
try: |
| 49 |
from typing import override # Added in 3.12. |
| 50 |
except ImportError: |
| 51 |
try: |
| 52 |
from typing_extensions import override |
| 53 |
except ImportError: |
| 54 |
def override(f: F, /) -> F: |
| 55 |
return f |
| 56 |
|
| 57 |
|
| 58 |
def assert_unreachable(message: str) -> Never: |
| 59 |
""" |
| 60 |
The code path is unreachable. Raises an :class:`AssertionError`. |
| 61 |
|
| 62 |
*message* (:class:`str`) is the error message. |
| 63 |
""" |
| 64 |
raise AssertionError(message) |