Skip to content

Commit ac97a65

Browse files
authored
Merge pull request numpy#27132 from jorenham/typing/dtype-constructor-specialization
TYP: Assume that ``typing_extensions`` is always available in the stubs
2 parents 95c3117 + e55eb98 commit ac97a65

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+71
-338
lines changed

numpy/__init__.pyi

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ from collections.abc import (
184184
Sequence,
185185
)
186186
from typing import (
187-
TYPE_CHECKING,
188187
Literal as L,
189188
Any,
190189
Generator,
@@ -203,12 +202,12 @@ from typing import (
203202
TypeAlias,
204203
)
205204

206-
if sys.version_info >= (3, 11):
207-
from typing import LiteralString
208-
elif TYPE_CHECKING:
209-
from typing_extensions import LiteralString
210-
else:
211-
LiteralString: TypeAlias = str
205+
# NOTE: `typing_extensions` is always available in `.pyi` stubs or when
206+
# `TYPE_CHECKING` - even if not available at runtime.
207+
# This is because the `typeshed` stubs for the standard library include
208+
# `typing_extensions` stubs:
209+
# https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi
210+
from typing_extensions import LiteralString
212211

213212
# Ensures that the stubs are picked up
214213
from numpy import (

numpy/_array_api_info.pyi

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import sys
21
from typing import (
3-
TYPE_CHECKING,
42
ClassVar,
53
Literal,
64
TypeAlias,
@@ -9,17 +7,10 @@ from typing import (
97
final,
108
overload,
119
)
10+
from typing_extensions import Never
1211

1312
import numpy as np
1413

15-
if sys.version_info >= (3, 11):
16-
from typing import Never
17-
elif TYPE_CHECKING:
18-
from typing_extensions import Never
19-
else:
20-
# `NoReturn` and `Never` are equivalent (but not equal) for type-checkers,
21-
# but are used in different places by convention
22-
from typing import NoReturn as Never
2314

2415
_Device: TypeAlias = Literal["cpu"]
2516
_DeviceLike: TypeAlias = None | _Device

numpy/polynomial/_polybase.pyi

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import abc
22
import decimal
33
import numbers
4-
import sys
54
from collections.abc import Iterator, Mapping, Sequence
65
from typing import (
7-
TYPE_CHECKING,
86
Any,
97
ClassVar,
108
Final,
@@ -44,12 +42,7 @@ from ._polytypes import (
4442
_ArrayLikeCoef_co,
4543
)
4644

47-
if sys.version_info >= (3, 11):
48-
from typing import LiteralString
49-
elif TYPE_CHECKING:
50-
from typing_extensions import LiteralString
51-
else:
52-
LiteralString: TypeAlias = str
45+
from typing_extensions import LiteralString
5346

5447

5548
__all__: Final[Sequence[str]] = ("ABCPolyBase",)

numpy/polynomial/_polytypes.pyi

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import sys
21
from collections.abc import Callable, Sequence
32
from typing import (
4-
TYPE_CHECKING,
53
Any,
64
Literal,
75
NoReturn,
@@ -31,12 +29,7 @@ from numpy._typing import (
3129
_NumberLike_co,
3230
)
3331

34-
if sys.version_info >= (3, 11):
35-
from typing import LiteralString
36-
elif TYPE_CHECKING:
37-
from typing_extensions import LiteralString
38-
else:
39-
LiteralString: TypeAlias = str
32+
from typing_extensions import LiteralString
4033

4134
_T = TypeVar("_T")
4235
_T_contra = TypeVar("_T_contra", contravariant=True)

numpy/typing/tests/data/reveal/arithmetic.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import sys
21
from typing import Any
32

43
import numpy as np
54
import numpy.typing as npt
65
from numpy._typing import _32Bit,_64Bit, _128Bit
76

8-
if sys.version_info >= (3, 11):
9-
from typing import assert_type
10-
else:
11-
from typing_extensions import assert_type
7+
from typing_extensions import assert_type
128

139
# Can't directly import `np.float128` as it is not available on all platforms
1410
f16: np.floating[_128Bit]

numpy/typing/tests/data/reveal/array_api_info.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import sys
21
from typing import Literal
32

43
import numpy as np
54

6-
if sys.version_info >= (3, 11):
7-
from typing import Never, assert_type
8-
else:
9-
from typing_extensions import Never, assert_type
5+
from typing_extensions import Never, assert_type
106

117
info = np.__array_namespace_info__()
128

numpy/typing/tests/data/reveal/array_constructors.pyi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ from collections import deque
66
import numpy as np
77
import numpy.typing as npt
88

9-
if sys.version_info >= (3, 11):
10-
from typing import assert_type
11-
else:
12-
from typing_extensions import assert_type
9+
from typing_extensions import assert_type
1310

1411
_SCT = TypeVar("_SCT", bound=np.generic, covariant=True)
1512

numpy/typing/tests/data/reveal/arraypad.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import sys
21
from collections.abc import Mapping
32
from typing import Any, SupportsIndex
43

54
import numpy as np
65
import numpy.typing as npt
76

8-
if sys.version_info >= (3, 11):
9-
from typing import assert_type
10-
else:
11-
from typing_extensions import assert_type
7+
from typing_extensions import assert_type
128

139
def mode_func(
1410
ar: npt.NDArray[np.number[Any]],

numpy/typing/tests/data/reveal/arrayprint.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
import contextlib
32
from collections.abc import Callable
43
from typing import Any
@@ -7,10 +6,7 @@ import numpy as np
76
import numpy.typing as npt
87
from numpy._core.arrayprint import _FormatOptions
98

10-
if sys.version_info >= (3, 11):
11-
from typing import assert_type
12-
else:
13-
from typing_extensions import assert_type
9+
from typing_extensions import assert_type
1410

1511
AR: npt.NDArray[np.int64]
1612
func_float: Callable[[np.floating[Any]], str]

numpy/typing/tests/data/reveal/arraysetops.pyi

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from typing import Any
32

43
import numpy as np
@@ -7,10 +6,7 @@ from numpy.lib._arraysetops_impl import (
76
UniqueAllResult, UniqueCountsResult, UniqueInverseResult
87
)
98

10-
if sys.version_info >= (3, 11):
11-
from typing import assert_type
12-
else:
13-
from typing_extensions import assert_type
9+
from typing_extensions import assert_type
1410

1511
AR_b: npt.NDArray[np.bool]
1612
AR_i8: npt.NDArray[np.int64]

0 commit comments

Comments
 (0)