Skip to content

Commit 0cbb66b

Browse files
committed
TYP: Disallow scalars and 0d-arrays in numpy.nonzero
1 parent be296e2 commit 0cbb66b

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

numpy/_core/fromnumeric.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Sequence
2-
from typing import Any, overload, TypeVar, Literal, SupportsIndex
2+
from typing import Any, NoReturn, overload, TypeVar, Literal, SupportsIndex
33

44
import numpy as np
55
from numpy import (
@@ -368,7 +368,10 @@ def ravel(a: _ArrayLike[_SCT], order: _OrderKACF = ...) -> NDArray[_SCT]: ...
368368
@overload
369369
def ravel(a: ArrayLike, order: _OrderKACF = ...) -> NDArray[Any]: ...
370370

371-
def nonzero(a: ArrayLike) -> tuple[NDArray[intp], ...]: ...
371+
@overload
372+
def nonzero(a: np.generic | np.ndarray[tuple[()], Any]) -> NoReturn: ...
373+
@overload
374+
def nonzero(a: _ArrayLike[Any]) -> tuple[NDArray[intp], ...]: ...
372375

373376
def shape(a: ArrayLike) -> _Shape: ...
374377

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ np.trace(A, axis2=[]) # E: No overload variant
8989

9090
np.ravel(a, order="bob") # E: No overload variant
9191

92+
np.nonzero(0) # E: No overload variant
93+
9294
np.compress( # E: No overload variant
9395
[True], A, axis=1.0
9496
)

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for :mod:`_core.fromnumeric`."""
22

33
import sys
4-
from typing import Any
4+
from typing import Any, NoReturn
55

66
import numpy as np
77
import numpy.typing as npt
@@ -22,6 +22,7 @@ AR_i8: npt.NDArray[np.int64]
2222
AR_O: npt.NDArray[np.object_]
2323
AR_subclass: NDArraySubclass
2424
AR_m: npt.NDArray[np.timedelta64]
25+
AR_0d: np.ndarray[tuple[()], np.dtype[Any]]
2526

2627
b: np.bool
2728
f4: np.float32
@@ -128,9 +129,9 @@ assert_type(np.ravel(f), npt.NDArray[Any])
128129
assert_type(np.ravel(AR_b), npt.NDArray[np.bool])
129130
assert_type(np.ravel(AR_f4), npt.NDArray[np.float32])
130131

131-
assert_type(np.nonzero(b), tuple[npt.NDArray[np.intp], ...])
132-
assert_type(np.nonzero(f4), tuple[npt.NDArray[np.intp], ...])
133-
assert_type(np.nonzero(f), tuple[npt.NDArray[np.intp], ...])
132+
assert_type(np.nonzero(b), NoReturn)
133+
assert_type(np.nonzero(f4), NoReturn)
134+
assert_type(np.nonzero(AR_0d), NoReturn)
134135
assert_type(np.nonzero(AR_b), tuple[npt.NDArray[np.intp], ...])
135136
assert_type(np.nonzero(AR_f4), tuple[npt.NDArray[np.intp], ...])
136137

0 commit comments

Comments
 (0)