Skip to content

Commit ed3320d

Browse files
authored
_typing: Update from nitypes (#16)
1 parent 83c56f0 commit ed3320d

File tree

1 file changed

+117
-9
lines changed

1 file changed

+117
-9
lines changed

src/nipanel/_typing.py

Lines changed: 117 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,133 @@
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+
"""
219

320
from __future__ import annotations
421

522
import sys
623
from typing import TYPE_CHECKING
724

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+
852
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+
)
1065
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+
)
1278
else:
13-
Self = None
79+
LiteralString = Never = NotRequired = Required = Self = Unpack = None
1480

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
17107
elif TYPE_CHECKING:
18-
from typing_extensions import ParamSpec
108+
from typing_extensions import ReadOnly, TypeIs
19109
else:
20-
from typing import TypeVar as ParamSpec
110+
ReadOnly = TypeIs = None
21111

22112
__all__ = [
23-
"Self",
113+
"assert_never",
114+
"assert_type",
115+
"Concatenate",
116+
"LiteralString",
117+
"Never",
118+
"NotRequired",
119+
"override",
24120
"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",
25133
]

0 commit comments

Comments
 (0)