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: fix stubtest errors in numpy.lib._function_base_impl
Ported from numpy/numtype#233

---

This fixes the signatures of the following public `numpy` members:

- `average`
- `ma.average`
- `median`
- `corrcoef`
  • Loading branch information
jorenham authored and charris committed Mar 15, 2025
commit 4a20c5154b7cca9a627df298f8ab6aefbdea0acd
185 changes: 93 additions & 92 deletions numpy/lib/_function_base_impl.pyi
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
from collections.abc import Sequence, Callable, Iterable
# ruff: noqa: ANN401
from collections.abc import Callable, Iterable, Sequence
from typing import (
Concatenate,
Literal as L,
Any,
Concatenate,
ParamSpec,
TypeAlias,
TypeVar,
overload,
Protocol,
SupportsIndex,
SupportsInt,
TypeGuard,
type_check_only
TypeAlias,
TypeVar,
overload,
type_check_only,
)
from typing_extensions import deprecated
from typing import Literal as L

from _typeshed import Incomplete
from typing_extensions import TypeIs, deprecated

import numpy as np
from numpy import (
vectorize,
_OrderKACF,
bool_,
complex128,
complexfloating,
datetime64,
float64,
floating,
generic,
integer,
floating,
complexfloating,
intp,
float64,
complex128,
timedelta64,
datetime64,
object_,
bool_,
_OrderKACF,
timedelta64,
vectorize,
)
from numpy._core.multiarray import bincount
from numpy._globals import _NoValueType
from numpy._typing import (
NDArray,
ArrayLike,
DTypeLike,
NDArray,
_ArrayLike,
_DTypeLike,
_ShapeLike,
_ArrayLikeBool_co,
_ArrayLikeInt_co,
_ArrayLikeFloat_co,
_ArrayLikeComplex_co,
_ArrayLikeNumber_co,
_ArrayLikeTD64_co,
_ArrayLikeDT64_co,
_ArrayLikeFloat_co,
_ArrayLikeInt_co,
_ArrayLikeNumber_co,
_ArrayLikeObject_co,
_FloatLike_co,
_ArrayLikeTD64_co,
_ComplexLike_co,
_DTypeLike,
_FloatLike_co,
_NestedSequence,
_NumberLike_co,
_ScalarLike_co,
_NestedSequence
_ShapeLike,
)

__all__ = [
Expand Down Expand Up @@ -106,12 +109,14 @@ _2Tuple: TypeAlias = tuple[_T, _T]

@type_check_only
class _TrimZerosSequence(Protocol[_T_co]):
def __len__(self) -> int: ...
def __len__(self, /) -> int: ...
@overload
def __getitem__(self, key: int, /) -> object: ...
@overload
def __getitem__(self, key: slice, /) -> _T_co: ...

###

@overload
def rot90(
m: _ArrayLike[_SCT],
Expand All @@ -134,72 +139,62 @@ def flip(m: _ArrayLike[_SCT], axis: None | _ShapeLike = ...) -> NDArray[_SCT]: .
@overload
def flip(m: ArrayLike, axis: None | _ShapeLike = ...) -> NDArray[Any]: ...

def iterable(y: object) -> TypeGuard[Iterable[Any]]: ...
def iterable(y: object) -> TypeIs[Iterable[Any]]: ...

@overload
def average(
a: _ArrayLikeFloat_co,
axis: None = ...,
weights: None | _ArrayLikeFloat_co= ...,
returned: L[False] = ...,
keepdims: L[False] = ...,
) -> floating[Any]: ...
@overload
def average(
a: _ArrayLikeComplex_co,
axis: None = ...,
weights: None | _ArrayLikeComplex_co = ...,
returned: L[False] = ...,
keepdims: L[False] = ...,
) -> complexfloating[Any, Any]: ...
@overload
def average(
a: _ArrayLikeObject_co,
axis: None = ...,
weights: None | Any = ...,
returned: L[False] = ...,
keepdims: L[False] = ...,
) -> Any: ...
axis: None = None,
weights: _ArrayLikeFloat_co | None = None,
returned: L[False] = False,
*,
keepdims: L[False] | _NoValueType = ...,
) -> floating: ...
@overload
def average(
a: _ArrayLikeFloat_co,
axis: None = ...,
weights: None | _ArrayLikeFloat_co= ...,
returned: L[True] = ...,
keepdims: L[False] = ...,
) -> _2Tuple[floating[Any]]: ...
axis: None = None,
weights: _ArrayLikeFloat_co | None = None,
*,
returned: L[True],
keepdims: L[False] | _NoValueType = ...,
) -> _2Tuple[floating]: ...
@overload
def average(
a: _ArrayLikeComplex_co,
axis: None = ...,
weights: None | _ArrayLikeComplex_co = ...,
returned: L[True] = ...,
keepdims: L[False] = ...,
) -> _2Tuple[complexfloating[Any, Any]]: ...
axis: None = None,
weights: _ArrayLikeComplex_co | None = None,
returned: L[False] = False,
*,
keepdims: L[False] | _NoValueType = ...,
) -> complexfloating: ...
@overload
def average(
a: _ArrayLikeObject_co,
axis: None = ...,
weights: None | Any = ...,
returned: L[True] = ...,
keepdims: L[False] = ...,
) -> _2Tuple[Any]: ...
a: _ArrayLikeComplex_co,
axis: None = None,
weights: _ArrayLikeComplex_co | None = None,
*,
returned: L[True],
keepdims: L[False] | _NoValueType = ...,
) -> _2Tuple[complexfloating]: ...
@overload
def average(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: None | _ShapeLike = ...,
weights: None | Any = ...,
returned: L[False] = ...,
keepdims: bool = ...,
) -> Any: ...
axis: _ShapeLike | None = None,
weights: object | None = None,
*,
returned: L[True],
keepdims: bool | bool_ | _NoValueType = ...,
) -> _2Tuple[Incomplete]: ...
@overload
def average(
a: _ArrayLikeComplex_co | _ArrayLikeObject_co,
axis: None | _ShapeLike = ...,
weights: None | Any = ...,
returned: L[True] = ...,
keepdims: bool = ...,
) -> _2Tuple[Any]: ...
axis: _ShapeLike | None = None,
weights: object | None = None,
returned: bool | bool_ = False,
*,
keepdims: bool | bool_ | _NoValueType = ...,
) -> Incomplete: ...

@overload
def asarray_chkfinite(
Expand Down Expand Up @@ -478,38 +473,46 @@ def cov(
dtype: DTypeLike,
) -> NDArray[Any]: ...

# NOTE `bias` and `ddof` have been deprecated
# NOTE `bias` and `ddof` are deprecated and ignored
@overload
def corrcoef(
m: _ArrayLikeFloat_co,
y: None | _ArrayLikeFloat_co = ...,
rowvar: bool = ...,
y: _ArrayLikeFloat_co | None = None,
rowvar: bool = True,
bias: _NoValueType = ...,
ddof: _NoValueType = ...,
*,
dtype: None = ...,
) -> NDArray[floating[Any]]: ...
dtype: None = None,
) -> NDArray[floating]: ...
@overload
def corrcoef(
m: _ArrayLikeComplex_co,
y: None | _ArrayLikeComplex_co = ...,
rowvar: bool = ...,
y: _ArrayLikeComplex_co | None = None,
rowvar: bool = True,
bias: _NoValueType = ...,
ddof: _NoValueType = ...,
*,
dtype: None = ...,
) -> NDArray[complexfloating[Any, Any]]: ...
dtype: None = None,
) -> NDArray[complexfloating]: ...
@overload
def corrcoef(
m: _ArrayLikeComplex_co,
y: None | _ArrayLikeComplex_co = ...,
rowvar: bool = ...,
y: _ArrayLikeComplex_co | None = None,
rowvar: bool = True,
bias: _NoValueType = ...,
ddof: _NoValueType = ...,
*,
dtype: _DTypeLike[_SCT],
) -> NDArray[_SCT]: ...
@overload
def corrcoef(
m: _ArrayLikeComplex_co,
y: None | _ArrayLikeComplex_co = ...,
rowvar: bool = ...,
y: _ArrayLikeComplex_co | None = None,
rowvar: bool = True,
bias: _NoValueType = ...,
ddof: _NoValueType = ...,
*,
dtype: DTypeLike,
dtype: DTypeLike | None = None,
) -> NDArray[Any]: ...

def blackman(M: _FloatLike_co) -> NDArray[floating[Any]]: ...
Expand Down Expand Up @@ -581,7 +584,6 @@ def median(
a: _ArrayLikeFloat_co | _ArrayLikeComplex_co | _ArrayLikeTD64_co | _ArrayLikeObject_co,
axis: None | _ShapeLike,
out: _ArrayType,
/,
overwrite_input: bool = ...,
keepdims: bool = ...,
) -> _ArrayType: ...
Expand Down Expand Up @@ -749,7 +751,6 @@ def percentile(
q: _ArrayLikeFloat_co,
axis: None | _ShapeLike,
out: _ArrayType,
/,
overwrite_input: bool = ...,
method: _MethodKind = ...,
keepdims: bool = ...,
Expand Down
3 changes: 2 additions & 1 deletion numpy/ma/extras.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from typing import Any

from numpy.lib._function_base_impl import average
from numpy.lib._index_tricks_impl import AxisConcatenator

from .core import dot, mask_rowcols

__all__ = [
Expand Down Expand Up @@ -88,7 +90,6 @@ diagflat: _fromnxfunction_single

def apply_along_axis(func1d, axis, arr, *args, **kwargs): ...
def apply_over_axes(func, a, axes): ...
def average(a, axis=..., weights=..., returned=..., keepdims=...): ...
def median(a, axis=..., out=..., overwrite_input=..., keepdims=...): ...
def compress_nd(x, axis=...): ...
def compress_rowcols(x, axis=...): ...
Expand Down
0