|
1 | | -"""Single source for typing backports to avoid depending on typing_extensions at run time.""" |
| 1 | +"""Version-specific compatibility shims for the standard `typing` module. |
| 2 | +
|
| 3 | +For `typing` symbols that require Python 3.10 or later, this submodule redirects to the standard |
| 4 | +`typing` module (when appropriate), the `typing_extensions` package, or provides a minimial stub |
| 5 | +implementation for run time. This allows us to use new typing features without littering the rest of |
| 6 | +our code with conditionals or making our Python packages depend on `typing_extenions` at run time. |
| 7 | +
|
| 8 | +For `typing` symbols that are supported in Python 3.9, you do not need this submodule. Import these |
| 9 | +symbols directly from the standard `typing` module. |
| 10 | +
|
| 11 | +This submodule is vendored in multiple packages (nitypes, nipanel, etc.) to avoid compatibility |
| 12 | +breakage when upgrading these packages. |
| 13 | +
|
| 14 | +Do not add project-specific types to this submodule. |
| 15 | +
|
| 16 | +Many of these symbosl are references to `None` at run time. Clients of this submodule should use |
| 17 | +`from __future__ import annotations` to avoid parsing type hints at run time. |
| 18 | +""" |
2 | 19 |
|
3 | 20 | from __future__ import annotations |
4 | 21 |
|
5 | 22 | import sys |
6 | 23 | from typing import TYPE_CHECKING |
7 | 24 |
|
| 25 | +if sys.version_info >= (3, 10): |
| 26 | + from typing import ( |
| 27 | + Concatenate, |
| 28 | + ParamSpec, |
| 29 | + ParamSpecArgs, |
| 30 | + ParamSpecKwargs, |
| 31 | + TypeAlias, |
| 32 | + TypeGuard, |
| 33 | + ) |
| 34 | +elif TYPE_CHECKING: |
| 35 | + from typing_extensions import ( |
| 36 | + Concatenate, |
| 37 | + ParamSpec, |
| 38 | + ParamSpecArgs, |
| 39 | + ParamSpecKwargs, |
| 40 | + TypeAlias, |
| 41 | + TypeGuard, |
| 42 | + ) |
| 43 | +else: |
| 44 | + Concatenate = ParamSpecArgs = ParamSpecKwargs = TypeAlias = TypeGuard = None |
| 45 | + |
| 46 | + def ParamSpec( # noqa: D103, N802 - Missing docstring, wrong case |
| 47 | + name, *, bound=None, covariant=False, contravariant=False |
| 48 | + ): |
| 49 | + return None |
| 50 | + |
| 51 | + |
8 | 52 | if sys.version_info >= (3, 11): |
9 | | - from typing import Self |
| 53 | + from typing import ( |
| 54 | + LiteralString, |
| 55 | + Never, |
| 56 | + NotRequired, |
| 57 | + Required, |
| 58 | + Self, |
| 59 | + TypeVarTuple, |
| 60 | + Unpack, |
| 61 | + assert_never, |
| 62 | + assert_type, |
| 63 | + reveal_type, |
| 64 | + ) |
10 | 65 | elif TYPE_CHECKING: |
11 | | - from typing_extensions import Self |
| 66 | + from typing_extensions import ( |
| 67 | + LiteralString, |
| 68 | + Never, |
| 69 | + NotRequired, |
| 70 | + Required, |
| 71 | + Self, |
| 72 | + TypeVarTuple, |
| 73 | + Unpack, |
| 74 | + assert_never, |
| 75 | + assert_type, |
| 76 | + reveal_type, |
| 77 | + ) |
12 | 78 | else: |
13 | | - Self = None |
| 79 | + LiteralString = Never = NotRequired = Required = Self = Unpack = None |
14 | 80 |
|
15 | | -if sys.version_info >= (3, 10): |
16 | | - from typing import ParamSpec |
| 81 | + def assert_never(arg, /): # noqa: D103 - Missing docstring in public function |
| 82 | + pass |
| 83 | + |
| 84 | + def assert_type(val, typ, /): # noqa: D103 - Missing docstring in public function |
| 85 | + pass |
| 86 | + |
| 87 | + def reveal_type(obj, /): # noqa: D103 - Missing docstring in public function |
| 88 | + pass |
| 89 | + |
| 90 | + def TypeVarTuple(name): # noqa: D103, N802 - Missing docstring, wrong case |
| 91 | + return None |
| 92 | + |
| 93 | + |
| 94 | +if sys.version_info >= (3, 12): |
| 95 | + from typing import TypeAliasType, override |
| 96 | +elif TYPE_CHECKING: |
| 97 | + from typing_extensions import TypeAliasType, override |
| 98 | +else: |
| 99 | + TypeAliasType = None |
| 100 | + |
| 101 | + def override(arg, /): # noqa: D103 - Missing docstring in public function |
| 102 | + return arg |
| 103 | + |
| 104 | + |
| 105 | +if sys.version_info >= (3, 13): |
| 106 | + from typing import ReadOnly, TypeIs |
17 | 107 | elif TYPE_CHECKING: |
18 | | - from typing_extensions import ParamSpec |
| 108 | + from typing_extensions import ReadOnly, TypeIs |
19 | 109 | else: |
20 | | - from typing import TypeVar as ParamSpec |
| 110 | + ReadOnly = TypeIs = None |
21 | 111 |
|
22 | 112 | __all__ = [ |
23 | | - "Self", |
| 113 | + "assert_never", |
| 114 | + "assert_type", |
| 115 | + "Concatenate", |
| 116 | + "LiteralString", |
| 117 | + "Never", |
| 118 | + "NotRequired", |
| 119 | + "override", |
24 | 120 | "ParamSpec", |
| 121 | + "ParamSpecArgs", |
| 122 | + "ParamSpecKwargs", |
| 123 | + "ReadOnly", |
| 124 | + "Required", |
| 125 | + "reveal_type", |
| 126 | + "Self", |
| 127 | + "TypeAlias", |
| 128 | + "TypeAliasType", |
| 129 | + "TypeGuard", |
| 130 | + "TypeIs", |
| 131 | + "TypeVarTuple", |
| 132 | + "Unpack", |
25 | 133 | ] |
0 commit comments