8000 TYP: Backport typing fixes from main (3) by charris · Pull Request #28534 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Backport typing fixes from main (3) #28534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TYP: work around a quantum-entangled mypy issue, somehow
  • Loading branch information
jorenham authored and charris committed Mar 15, 2025
commit 710d3b1e9c4253579a199d9828537fabbab051b1
72 changes: 40 additions & 32 deletions numpy/_core/fromnumeric.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ from collections.abc import Sequence
from typing import (
Any,
Literal,
NoReturn,
Protocol,
SupportsIndex,
TypeAlias,
TypeVar,
overload,
type_check_only,
)

from _typeshed import Incomplete
from typing_extensions import Never, deprecated

import numpy as np
Expand Down Expand Up @@ -551,9 +552,6 @@ def ravel(
@overload
def ravel(a: ArrayLike, order: _OrderKACF = "C") -> np.ndarray[tuple[int], np.dtype[Any]]: ...

@overload
def nonzero(a: np.generic | np.ndarray[tuple[()], Any]) -> NoReturn: ...
@overload
def nonzero(a: _ArrayLike[Any]) -> tuple[NDArray[intp], ...]: ...

# this prevents `Any` from being returned with Pyright
Expand Down Expand Up @@ -813,7 +811,7 @@ def all(
keepdims: _BoolLike_co | _NoValueType = ...,
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> np.bool | NDArray[np.bool]: ...
) -> Incomplete: ...
@overload
def all(
a: ArrayLike,
Expand Down Expand Up @@ -850,7 +848,7 @@ def any(
keepdims: _BoolLike_co | _NoValueType = ...,
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> np.bool | NDArray[np.bool]: ...
) -> Incomplete: ...
@overload
def any(
a: ArrayLike,
Expand Down Expand Up @@ -1443,10 +1441,10 @@ def mean(
keepdims: Literal[False] | _NoValueType = ...,
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> complexfloating[Any, Any]: ...
) -> complexfloating[Any]: ...
@overload
def mean(
a: _ArrayLikeTD64_co,
a: _ArrayLike[np.timedelta64],
axis: None = ...,
dtype: None = ...,
out: None = ...,
Expand All @@ -1457,23 +1455,33 @@ def mean(
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: _ShapeLike | None = ...,
dtype: None = ...,
out: None = ...,
axis: _ShapeLike | None,
dtype: DTypeLike,
out: _ArrayT,
keepdims: bool | _NoValueType = ...,
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> Any: ...
) -> _ArrayT: ...
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: _ShapeLike | None = ...,
dtype: DTypeLike | None = ...,
*,
out: _ArrayT,
keepdims: bool | _NoValueType = ...,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> _ArrayT: ...
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: None,
dtype: _DTypeLike[_SCT],
out: None = ...,
keepdims: bool | _NoValueType = ...,
keepdims: Literal[False] | _NoValueType = ...,
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> _SCT | NDArray[_SCT]: ...
) -> _SCT: ...
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
Expand All @@ -1487,43 +1495,43 @@ def mean(
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: None = ...,
*,
axis: _ShapeLike | None,
dtype: _DTypeLike[_SCT],
out: None = ...,
keepdims: bool | _NoValueType = ...,
out: None,
keepdims: Literal[True, 1],
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> _SCT | NDArray[_SCT]: ...
) -> NDArray[_SCT]: ...
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: _ShapeLike | None = ...,
dtype: DTypeLike = ...,
axis: _ShapeLike | None,
dtype: _DTypeLike[_SCT],
out: None = ...,
keepdims: bool | _NoValueType = ...,
*,
keepdims: bool | _NoValueType = ...,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> Any: ...
) -> _SCT | NDArray[_SCT]: ...
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: _ShapeLike | None,
dtype: DTypeLike,
out: _ArrayT,
keepdims: bool | _NoValueType = ...,
axis: _ShapeLike | None = ...,
*,
dtype: _DTypeLike[_SCT],
out: None = ...,
keepdims: bool | _NoValueType = ...,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> _ArrayT: ...
) -> _SCT | NDArray[_SCT]: ...
@overload
def mean(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: _ShapeLike | None = ...,
dtype: DTypeLike = ...,
*,
out: _ArrayT,
dtype: DTypeLike | None = ...,
out: None = ...,
keepdims: bool | _NoValueType = ...,
*,
where: _ArrayLikeBool_co | _NoValueType = ...,
) -> _ArrayT: ...
) -> Incomplete: ...

@overload
def std(
Expand Down
5 changes: 1 addition & 4 deletions numpy/typing/tests/data/reveal/fromnumeric.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Tests for :mod:`_core.fromnumeric`."""

from typing import Any, Literal as L, NoReturn
from typing import Any, Literal as L

import numpy as np
import numpy.typing as npt
Expand Down Expand Up @@ -127,11 +127,8 @@ assert_type(np.ravel(f), np.ndarray[tuple[int], np.dtype[np.float64 | np.int_ |
assert_type(np.ravel(AR_b), np.ndarray[tuple[int], np.dtype[np.bool]])
assert_type(np.ravel(AR_f4), np.ndarray[tuple[int], np.dtype[np.float32]])

assert_type(np.nonzero(b), NoReturn)
assert_type(np.nonzero(f4), NoReturn)
assert_type(np.nonzero(AR_b), tuple[npt.NDArray[np.intp], ...])
assert_type(np.nonzero(AR_f4), tuple[npt.NDArray[np.intp], ...])
assert_type(np.nonzero(AR_0d), NoReturn)
assert_type(np.nonzero(AR_1d), tuple[npt.NDArray[np.intp], ...])
assert_type(np.nonzero(AR_nd), tuple[npt.NDArray[np.intp], ...])

Expand Down
0