8000 TYP: Use stricter mypy settings · Python-Repository-Hub/numpy@1621dff · GitHub
[go: up one dir, main page]

Skip to content

Commit 1621dff

Browse files
committed
TYP: Use stricter mypy settings
* Disallow generic types lacking a parameter * Disallow importing from unknown modules
1 parent 3b281dd commit 1621dff

File tree

11 files changed

+34
-25
lines changed

11 files changed

+34
-25
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
2+
import numpy.typing as npt
23

3-
a: np.ndarray
4+
a: npt.NDArray[np.float64]
45
generator = (i for i in range(10))
56

67
np.require(a, requirements=1) # E: No overload variant

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from collections.abc import Callable
22
from typing import Any
3+
34
import numpy as np
5+
import numpy.typing as npt
46

5-
AR: np.ndarray
7+
AR: npt.NDArray[np.float64]
68
func1: Callable[[Any], str]
79
func2: Callable[[np.integer[Any]], str]
810

numpy/typing/tests/data/fail/flatiter.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from typing import Any
22

33
import numpy as np
4-
from numpy._typing import _SupportsArray
4+
import numpy._typing as npt
55

66

77
class Index:
88
def __index__(self) -> int:
99
...
1010

1111

12-
a: "np.flatiter[np.ndarray]"
13-
supports_array: _SupportsArray
12+
a: np.flatiter[npt.NDArray[np.float64]]
13+
supports_array: npt._SupportsArray[np.dtype[np.float64]]
1414

1515
a.base = Any # E: Property "base" defined in "flatiter" is read-only
1616
a.coords = Any # E: Property "coords" defined in "flatiter" is read-only

numpy/typing/tests/data/mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line 10000 change
@@ -3,3 +3,5 @@ plugins = numpy.typing.mypy_plugin
33
show_absolute_path = True
44
implicit_reexport = False
55
pretty = True
6+
disallow_any_unimported = True
7+
disallow_any_generics = True

numpy/typing/tests/data/pass/array_constructors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import sys
21
from typing import Any
3-
import numpy as np
42

3+
import numpy as np
4+
import numpy.typing as npt
55

66
class Index:
77
def __index__(self) -> int:
88
return 0
99

1010

11-
class SubClass(np.ndarray):
11+
class SubClass(npt.NDArray[np.float64]):
1212
pass
1313

1414

numpy/typing/tests/data/pass/array_like.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Any
44

55
import numpy as np
6-
from numpy._typing import ArrayLike, _SupportsArray
6+
from numpy._typing import NDArray, ArrayLike, _SupportsArray
77

88
x1: ArrayLike = True
99
x2: ArrayLike = 5
@@ -20,22 +20,24 @@
2020

2121

2222
class A:
23-
def __array__(self, dtype: None | np.dtype[Any] = None) -> np.ndarray:
24-
return np.array([1, 2, 3])
23+
def __array__(
24+
self, dtype: None | np.dtype[Any] = None
25+
) -> NDArray[np.float64]:
26+
return np.array([1.0, 2.0, 3.0])
2527

2628

2729
x13: ArrayLike = A()
2830

29-
scalar: _SupportsArray = np.int64(1)
31+
scalar: _SupportsArray[np.dtype[np.int64]] = np.int64(1)
3032
scalar.__array__()
31-
array: _SupportsArray = np.array(1)
33+
array: _SupportsArray[np.dtype[np.int_]] = np.array(1)
3234
array.__array__()
3335

34-
a: _SupportsArray = A()
36+
a: _SupportsArray[np.dtype[np.float64]] = A()
3537
a.__array__()
3638
a.__array__()
3739

3840
# Escape hatch for when you mean to make something like an object
3941
# array.
40-
object_array_scalar: Any = (i for i in range(10))
42+
object_array_scalar: object = (i for i in range(10))
4143
np.array(object_array_scalar)

numpy/typing/tests/data/pass/literal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from __future__ import annotations
22

3+
from typing import Any
34
from functools import partial
45
from collections.abc import Callable
56

6-
import pytest # type: ignore
7+
import pytest
78
import numpy as np
89

910
AR = np.array(0)
@@ -13,7 +14,7 @@
1314
ACF = frozenset({None, "A", "C", "F"})
1415
CF = frozenset({None, "C", "F"})
1516

16-
order_list: list[tuple[frozenset, Callable]] = [
17+
order_list: list[tuple[frozenset[str | None], Callable[..., Any]]] = [
1718
(KACF, partial(np.ndarray, 1)),
1819
(KACF, AR.tobytes),
1920
(KACF, partial(AR.astype, int)),

numpy/typing/tests/data/pass/ndarray_misc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from typing import cast, Any
1313

1414
import numpy as np
15+
import numpy.typing as npt
1516

16-
class SubClass(np.ndarray): ...
17+
class SubClass(npt.NDArray[np.float64]): ...
1718

1819
i4 = np.int32(1)
1920
A: np.ndarray[Any, np.dtype[np.int32]] = np.array([[1]], dtype=np.int32)

numpy/typing/tests/data/pass/numeric.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
from __future__ import annotations
99

1010
import numpy as np
11+
import numpy.typing as npt
1112

12-
class SubClass(np.ndarray):
13+
class SubClass(npt.NDArray[np.float64]):
1314
...
1415

1516
i8 = np.int64(1)

numpy/typing/tests/data/pass/simple.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
import operator
33

44
import numpy as np
5+
import numpy.typing as npt
56
from collections.abc import Iterable
67

78
# Basic checks
89
array = np.array([1, 2])
910

1011

11-
def ndarray_func(x):
12-
# type: (np.ndarray) -> np.ndarray
12+
def ndarray_func(x: npt.NDArray[np.float64]) -> npt.NDArray[np.float64]:
1313
return x
1414

1515

16-
ndarray_func(np.array([1, 2]))
16+
ndarray_func(np.array([1, 2], dtype=np.float64))
1717
array == 1
1818
array.dtype == float
1919

@@ -56,8 +56,7 @@ def ndarray_func(x):
5656
np.dtype(float) >= np.dtype(("U", 10))
5757

5858
# Iteration and indexing
59-
def iterable_func(x):
60-
# type: (Iterable) -> Iterable
59+
def iterable_func(x: Iterable[object]) -> Iterable[object]:
6160
return x
6261

6362

0 commit comments

Comments
 (0)
0