From 29929338af26f8d3ed444364fdbf520a9954f2c9 Mon Sep 17 00:00:00 2001 From: jorenham Date: Fri, 14 Mar 2025 23:00:42 +0100 Subject: [PATCH 1/6] TYP: fix stubtest errors in ``numpy.dtype`` and ``numpy.dtypes.*`` Ported from numpy/numtype#229 --- numpy/__init__.pyi | 15 ++++++++++++++- numpy/dtypes.pyi | 45 +++++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 527f71d897ee..f8c499410993 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1196,8 +1196,21 @@ __future_scalars__: Final[set[L["bytes", "str", "object"]]] = ... __array_api_version__: Final[L["2023.12"]] = "2023.12" test: Final[PytestTester] = ... +@type_check_only +class _DTypeMeta(type): + @property + def type(cls, /) -> type[generic] | None: ... + @property + def _abstract(cls, /) -> bool: ... + @property + def _is_numeric(cls, /) -> bool: ... + @property + def _parametric(cls, /) -> bool: ... + @property + def _legacy(cls, /) -> bool: ... + @final -class dtype(Generic[_SCT_co]): +class dtype(Generic[_SCT_co], metaclass=_DTypeMeta): names: None | tuple[builtins.str, ...] def __hash__(self) -> int: ... diff --git a/numpy/dtypes.pyi b/numpy/dtypes.pyi index 5cb345035f2c..11e5611653fa 100644 --- a/numpy/dtypes.pyi +++ b/numpy/dtypes.pyi @@ -1,18 +1,13 @@ -from typing import ( - Any, - Final, - Generic, - Literal as L, - NoReturn, - TypeAlias, - final, - type_check_only, -) +# ruff: noqa: ANN401 +from types import MemberDescriptorType +from typing import Any, ClassVar, Generic, NoReturn, TypeAlias, final, type_check_only +from typing import Literal as L + from typing_extensions import LiteralString, Self, TypeVar import numpy as np -__all__ = [ +__all__ = [ # noqa: RUF022 'BoolDType', 'Int8DType', 'ByteDType', @@ -53,7 +48,7 @@ __all__ = [ _SCT_co = TypeVar("_SCT_co", bound=np.generic, covariant=True) @type_check_only -class _SimpleDType(Generic[_SCT_co], np.dtype[_SCT_co]): # type: ignore[misc] +class _SimpleDType(np.dtype[_SCT_co], Generic[_SCT_co]): # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] names: None # pyright: ignore[reportIncompatibleVariableOverride] def __new__(cls, /) -> Self: ... def __getitem__(self, key: Any, /) -> NoReturn: ... @@ -73,7 +68,7 @@ class _SimpleDType(Generic[_SCT_co], np.dtype[_SCT_co]): # type: ignore[misc] def subdtype(self) -> None: ... @type_check_only -class _LiteralDType(Generic[_SCT_co], _SimpleDType[_SCT_co]): # type: ignore[misc] +class _LiteralDType(_SimpleDType[_SCT_co], Generic[_SCT_co]): # type: ignore[misc] @property def flags(self) -> L[0]: ... @property @@ -234,10 +229,11 @@ class UInt64DType( # type: ignore[misc] def str(self) -> L["u8"]: ... # Standard C-named version/alias: -ByteDType: Final = Int8DType -UByteDType: Final = UInt8DType -ShortDType: Final = Int16DType -UShortDType: Final = UInt16DType +# NOTE: Don't make these `Final`: it will break stubtest +ByteDType = Int8DType +UByteDType = UInt8DType +ShortDType = Int16DType +UShortDType = UInt16DType @final class IntDType( # type: ignore[misc] @@ -419,11 +415,11 @@ class ObjectDType( # type: ignore[misc] @final class BytesDType( # type: ignore[misc] - Generic[_ItemSize_co], _TypeCodes[L["S"], L["S"], L[18]], _NoOrder, _NBit[L[1],_ItemSize_co], _SimpleDType[np.bytes_], + Generic[_ItemSize_co], ): def __new__(cls, size: _ItemSize_co, /) -> BytesDType[_ItemSize_co]: ... @property @@ -435,11 +431,11 @@ class BytesDType( # type: ignore[misc] @final class StrDType( # type: ignore[misc] - Generic[_ItemSize_co], _TypeCodes[L["U"], L["U"], L[19]], _NativeOrder, _NBit[L[4],_ItemSize_co], _SimpleDType[np.str_], + Generic[_ItemSize_co], ): def __new__(cls, size: _ItemSize_co, /) -> StrDType[_ItemSize_co]: ... @property @@ -451,11 +447,11 @@ class StrDType( # type: ignore[misc] @final class VoidDType( # type: ignore[misc] - Generic[_ItemSize_co], _TypeCodes[L["V"], L["V"], L[20]], _NoOrder, _NBit[L[1], _ItemSize_co], - np.dtype[np.void], + np.dtype[np.void], # pyright: ignore[reportGeneralTypeIssues] + Generic[_ItemSize_co], ): # NOTE: `VoidDType(...)` raises a `TypeError` at the moment def __new__(cls, length: _ItemSize_co, /) -> NoReturn: ... @@ -578,8 +574,13 @@ class StringDType( # type: ignore[misc] _NativeOrder, _NBit[L[8], L[16]], # TODO: Replace the (invalid) `str` with the scalar type, once implemented - np.dtype[str], # type: ignore[type-var] + np.dtype[str], # type: ignore[type-var] # pyright: ignore[reportGeneralTypeIssues,reportInvalidTypeArguments] ): + @property + def coerce(self) -> L[True]: ... + na_object: ClassVar[MemberDescriptorType] # does not get instantiated + + # def __new__(cls, /) -> StringDType: ... def __getitem__(self, key: Any, /) -> NoReturn: ... @property From 79b8c26a0ae7b2b1eda1a8b23ded33f16b07d19c Mon Sep 17 00:00:00 2001 From: jorenham Date: Fri, 14 Mar 2025 23:08:23 +0100 Subject: [PATCH 2/6] TYP: fix stubtest errors in ``timedelta64`` and ``object_`` Ported from numpy/numtype#228 --- the following methods were missing according to stubtest: - `numpy.object_.__call__` - `numpy.timedelta64.__class_getitem__` --- numpy/__init__.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index f8c499410993..7806724d707b 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -3990,7 +3990,7 @@ bool_ = bool # NOTE: Because mypy has some long-standing bugs related to `__new__`, `object_` can't # be made generic. @final -class object_(_RealMixin, generic): +class object_(_RealMixin, generic[Any]): @overload def __new__(cls, nothing_to_see_here: None = None, /) -> None: ... # type: ignore[misc] @overload @@ -4004,6 +4004,8 @@ class object_(_RealMixin, generic): @overload # catch-all def __new__(cls, value: Any = ..., /) -> object | NDArray[Self]: ... # type: ignore[misc] def __init__(self, value: object = ..., /) -> None: ... + def __hash__(self, /) -> int: ... + def __call__(self, /, *args: object, **kwargs: object) -> Any: ... if sys.version_info >= (3, 12): def __release_buffer__(self, buffer: memoryview, /) -> None: ... @@ -4466,6 +4468,9 @@ class timedelta64(_IntegralMixin, generic[_TD64ItemT_co], Generic[_TD64ItemT_co] @overload def __init__(self, value: _ConvertibleToTD64, format: _TimeUnitSpec = ..., /) -> None: ... + # inherited at runtime from `signedinteger` + def __class_getitem__(cls, type_arg: type | object, /) -> GenericAlias: ... + # NOTE: Only a limited number of units support conversion # to builtin scalar types: `Y`, `M`, `ns`, `ps`, `fs`, `as` def __int__(self: timedelta64[int], /) -> int: ... From 4a20c5154b7cca9a627df298f8ab6aefbdea0acd Mon Sep 17 00:00:00 2001 From: jorenham Date: Fri, 14 Mar 2025 23:41:42 +0100 Subject: [PATCH 3/6] 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` --- numpy/lib/_function_base_impl.pyi | 185 +++++++++++++++--------------- numpy/ma/extras.pyi | 3 +- 2 files changed, 95 insertions(+), 93 deletions(-) diff --git a/numpy/lib/_function_base_impl.pyi b/numpy/lib/_function_base_impl.pyi index 214ad1f04f4b..e98dcbb7e741 100644 --- a/numpy/lib/_function_base_impl.pyi +++ b/numpy/lib/_function_base_impl.pyi @@ -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__ = [ @@ -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], @@ -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( @@ -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]]: ... @@ -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: ... @@ -749,7 +751,6 @@ def percentile( q: _ArrayLikeFloat_co, axis: None | _ShapeLike, out: _ArrayType, - /, overwrite_input: bool = ..., method: _MethodKind = ..., keepdims: bool = ..., diff --git a/numpy/ma/extras.pyi b/numpy/ma/extras.pyi index df69cd5d3465..580309cc679d 100644 --- a/numpy/ma/extras.pyi +++ b/numpy/ma/extras.pyi @@ -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__ = [ @@ -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=...): ... From b6de917f8bc88b9d6240d68764429b3c9d108f16 Mon Sep 17 00:00:00 2001 From: jorenham Date: Sat, 15 Mar 2025 01:10:10 +0100 Subject: [PATCH 4/6] TYP: fix stubtest errors in ``numpy.lib._index_tricks_impl`` Ported from numpy/numtype#235 --- - move `ndenumerate` and `ndindex` definitions to `lib._index_tricks_impl` - add deprecated `ndenumerate.ndincr` property - removed non-existent `ndenumerate.iter` property - remove incorrect "pass" and "reveal" type-tests for `ndenumerate.iter` - fix incorrect `ndenumerate` constructor fallback return type - fix `AxisConcatenator.makemat` signature --- numpy/__init__.pyi | 46 +-- numpy/lib/_index_tricks_impl.pyi | 261 ++++++++++-------- numpy/typing/tests/data/pass/index_tricks.py | 4 - .../typing/tests/data/reveal/index_tricks.pyi | 12 +- 4 files changed, 149 insertions(+), 174 deletions(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index 7806724d707b..539c5fa53c24 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -515,6 +515,8 @@ from numpy.lib._histograms_impl import ( ) from numpy.lib._index_tricks_impl import ( + ndenumerate, + ndindex, ravel_multi_index, unravel_index, mgrid, @@ -4964,50 +4966,6 @@ class errstate: ) -> None: ... def __call__(self, func: _CallableT) -> _CallableT: ... -class ndenumerate(Generic[_SCT_co]): - @property - def iter(self) -> flatiter[NDArray[_SCT_co]]: ... - - @overload - def __new__( - cls, arr: _FiniteNestedSequence[_SupportsArray[dtype[_SCT]]], - ) -> ndenumerate[_SCT]: ... - @overload - def __new__(cls, arr: str | _NestedSequence[str]) -> ndenumerate[str_]: ... - @overload - def __new__(cls, arr: bytes | _NestedSequence[bytes]) -> ndenumerate[bytes_]: ... - @overload - def __new__(cls, arr: builtins.bool | _NestedSequence[builtins.bool]) -> ndenumerate[np.bool]: ... - @overload - def __new__(cls, arr: int | _NestedSequence[int]) -> ndenumerate[int_]: ... - @overload - def __new__(cls, arr: float | _NestedSequence[float]) -> ndenumerate[float64]: ... - @overload - def __new__(cls, arr: complex | _NestedSequence[complex]) -> ndenumerate[complex128]: ... - @overload - def __new__(cls, arr: object) -> ndenumerate[object_]: ... - - # The first overload is a (semi-)workaround for a mypy bug (tested with v1.10 and v1.11) - @overload - def __next__( - self: ndenumerate[np.bool | datetime64 | timedelta64 | number[Any] | flexible], - /, - ) -> tuple[_Shape, _SCT_co]: ... - @overload - def __next__(self: ndenumerate[object_], /) -> tuple[_Shape, Any]: ... - @overload - def __next__(self, /) -> tuple[_Shape, _SCT_co]: ... - - def __iter__(self) -> Self: ... - -class ndindex: - @overload - def __init__(self, shape: tuple[SupportsIndex, ...], /) -> None: ... - @overload - def __init__(self, *shape: SupportsIndex) -> None: ... - def __iter__(self) -> Self: ... - def __next__(self) -> _Shape: ... - # TODO: The type of each `__next__` and `iters` return-type depends # on the length and dtype of `args`; we can't describe this behavior yet # as we lack variadics (PEP 646). diff --git a/numpy/lib/_index_tricks_impl.pyi b/numpy/lib/_index_tricks_impl.pyi index bd508a8b5905..4a1426fd4d6c 100644 --- a/numpy/lib/_index_tricks_impl.pyi +++ b/numpy/lib/_index_tricks_impl.pyi @@ -1,45 +1,23 @@ from collections.abc import Sequence -from typing import ( - Any, - TypeVar, - Generic, - overload, - Literal, - SupportsIndex, -) +from typing import Any, ClassVar, Final, Generic, SupportsIndex, final, overload +from typing import Literal as L + +from _typeshed import Incomplete +from typing_extensions import Self, TypeVar, deprecated import numpy as np -from numpy import ( - # Circumvent a naming conflict with `AxisConcatenator.matrix` - matrix as _Matrix, - ndenumerate, - ndindex, - ndarray, - dtype, - str_, - bytes_, - int_, - float64, - complex128, -) +from numpy._core.multiarray import ravel_multi_index, unravel_index from numpy._typing import ( - # Arrays ArrayLike, - _NestedSequence, - _FiniteNestedSequence, NDArray, - - # DTypes - DTypeLike, - _SupportsDType, - - # Shapes + _FiniteNestedSequence, + _NestedSequence, _Shape, + _SupportsArray, + _SupportsDType, ) -from numpy._core.multiarray import unravel_index, ravel_multi_index - -__all__ = [ +__all__ = [ # noqa: RUF022 "ravel_multi_index", "unravel_index", "mgrid", @@ -56,114 +34,163 @@ __all__ = [ "diag_indices_from", ] +### + _T = TypeVar("_T") -_DType = TypeVar("_DType", bound=dtype[Any]) -_BoolType = TypeVar("_BoolType", Literal[True], Literal[False]) -_TupType = TypeVar("_TupType", bound=tuple[Any, ...]) -_ArrayType = TypeVar("_ArrayType", bound=NDArray[Any]) +_TupleT = TypeVar("_TupleT", bound=tuple[Any, ...]) +_ArrayT = TypeVar("_ArrayT", bound=NDArray[Any]) +_DTypeT = TypeVar("_DTypeT", bound=np.dtype[Any]) +_ScalarT = TypeVar("_ScalarT", bound=np.generic) +_ScalarT_co = TypeVar("_ScalarT_co", bound=np.generic, covariant=True) +_BoolT_co = TypeVar("_BoolT_co", bound=bool, default=bool, covariant=True) -@overload -def ix_(*args: _FiniteNestedSequence[_SupportsDType[_DType]]) -> tuple[ndarray[_Shape, _DType], ...]: ... -@overload -def ix_(*args: str | _NestedSequence[str]) -> tuple[NDArray[str_], ...]: ... -@overload -def ix_(*args: bytes | _NestedSequence[bytes]) -> tuple[NDArray[bytes_], ...]: ... -@overload -def ix_(*args: bool | _NestedSequence[bool]) -> tuple[NDArray[np.bool], ...]: ... -@overload -def ix_(*args: int | _NestedSequence[int]) -> tuple[NDArray[int_], ...]: ... -@overload -def ix_(*args: float | _NestedSequence[float]) -> tuple[NDArray[float64], ...]: ... -@overload -def ix_(*args: complex | _NestedSequence[complex]) -> tuple[NDArray[complex128], ...]: ... +_AxisT_co = TypeVar("_AxisT_co", bound=int, default=L[0], covariant=True) +_MatrixT_co = TypeVar("_MatrixT_co", bound=bool, default=L[False], covariant=True) +_NDMinT_co = TypeVar("_NDMinT_co", bound=int, default=L[1], covariant=True) +_Trans1DT_co = TypeVar("_Trans1DT_co", bound=int, default=L[-1], covariant=True) + +### -class nd_grid(Generic[_BoolType]): - sparse: _BoolType - def __init__(self, sparse: _BoolType = ...) -> None: ... +class ndenumerate(Generic[_ScalarT_co]): + @overload + def __new__(cls, arr: _FiniteNestedSequence[_SupportsArray[np.dtype[_ScalarT]]]) -> ndenumerate[_ScalarT]: ... + @overload + def __new__(cls, arr: str | _NestedSequence[str]) -> ndenumerate[np.str_]: ... @overload - def __getitem__( - self: nd_grid[Literal[False]], - key: slice | Sequence[slice], - ) -> NDArray[Any]: ... + def __new__(cls, arr: bytes | _NestedSequence[bytes]) -> ndenumerate[np.bytes_]: ... @overload - def __getitem__( - self: nd_grid[Literal[True]], - key: slice | Sequence[slice], - ) -> tuple[NDArray[Any], ...]: ... + def __new__(cls, arr: bool | _NestedSequence[bool]) -> ndenumerate[np.bool]: ... + @overload + def __new__(cls, arr: int | _NestedSequence[int]) -> ndenumerate[np.intp]: ... + @overload + def __new__(cls, arr: float | _NestedSequence[float]) -> ndenumerate[np.float64]: ... + @overload + def __new__(cls, arr: complex | _NestedSequence[complex]) -> ndenumerate[np.complex128]: ... + @overload + def __new__(cls, arr: object) -> ndenumerate[Any]: ... -class MGridClass(nd_grid[Literal[False]]): - def __init__(self) -> None: ... + # The first overload is a (semi-)workaround for a mypy bug (tested with v1.10 and v1.11) + @overload + def __next__( + self: ndenumerate[np.bool | np.number | np.flexible | np.datetime64 | np.timedelta64], + /, + ) -> tuple[tuple[int, ...], _ScalarT_co]: ... + @overload + def __next__(self: ndenumerate[np.object_], /) -> tuple[tuple[int, ...], Any]: ... + @overload + def __next__(self, /) -> tuple[tuple[int, ...], _ScalarT_co]: ... + + # + def __iter__(self) -> Self: ... + +class ndindex: + @overload + def __init__(self, shape: tuple[SupportsIndex, ...], /) -> None: ... + @overload + def __init__(self, /, *shape: SupportsIndex) -> None: ... + + # + def __iter__(self) -> Self: ... + def __next__(self) -> tuple[int, ...]: ... -mgrid: MGridClass + # + @deprecated("Deprecated since 1.20.0.") + def ndincr(self, /) -> None: ... -class OGridClass(nd_grid[Literal[True]]): +class nd_grid(Generic[_BoolT_co]): + sparse: _BoolT_co + def __init__(self, sparse: _BoolT_co = ...) -> None: ... + @overload + def __getitem__(self: nd_grid[L[False]], key: slice | Sequence[slice]) -> NDArray[Any]: ... + @overload + def __getitem__(self: nd_grid[L[True]], key: slice | Sequence[slice]) -> tuple[NDArray[Any], ...]: ... + +@final +class MGridClass(nd_grid[L[False]]): + def __init__(self) -> None: ... + +@final +class OGridClass(nd_grid[L[True]]): def __init__(self) -> None: ... -ogrid: OGridClass +class AxisConcatenator(Generic[_AxisT_co, _MatrixT_co, _NDMinT_co, _Trans1DT_co]): + __slots__ = "axis", "matrix", "ndmin", "trans1d" + + makemat: ClassVar[type[np.matrix[tuple[int, int], np.dtype[Any]]]] -class AxisConcatenator: - axis: int - matrix: bool - ndmin: int - trans1d: int + axis: _AxisT_co + matrix: _MatrixT_co + ndmin: _NDMinT_co + trans1d: _Trans1DT_co + + # def __init__( self, - axis: int = ..., - matrix: bool = ..., - ndmin: int = ..., - trans1d: int = ..., + /, + axis: _AxisT_co = ..., + matrix: _MatrixT_co = ..., + ndmin: _NDMinT_co = ..., + trans1d: _Trans1DT_co = ..., ) -> None: ... + + # TODO(jorenham): annotate this + def __getitem__(self, key: Incomplete, /) -> Incomplete: ... + def __len__(self, /) -> L[0]: ... + + # @staticmethod @overload - def concatenate( # type: ignore[misc] - *a: ArrayLike, axis: SupportsIndex = ..., out: None = ... - ) -> NDArray[Any]: ... + def concatenate(*a: ArrayLike, axis: SupportsIndex | None = 0, out: _ArrayT) -> _ArrayT: ... @staticmethod @overload - def concatenate( - *a: ArrayLike, axis: SupportsIndex = ..., out: _ArrayType = ... - ) -> _ArrayType: ... - @staticmethod - def makemat( - data: ArrayLike, dtype: DTypeLike = ..., copy: bool = ... - ) -> _Matrix[Any, Any]: ... - - # TODO: Sort out this `__getitem__` method - def __getitem__(self, key: Any) -> Any: ... - -class RClass(AxisConcatenator): - axis: Literal[0] - matrix: Literal[False] - ndmin: Literal[1] - trans1d: Literal[-1] - def __init__(self) -> None: ... + def concatenate(*a: ArrayLike, axis: SupportsIndex | None = 0, out: None = None) -> NDArray[Any]: ... -r_: RClass - -class CClass(AxisConcatenator): - axis: Literal[-1] - matrix: Literal[False] - ndmin: Literal[2] - trans1d: Literal[0] - def __init__(self) -> None: ... +@final +class RClass(AxisConcatenator[L[0], L[False], L[1], L[-1]]): + def __init__(self, /) -> None: ... -c_: CClass +@final +class CClass(AxisConcatenator[L[-1], L[False], L[2], L[0]]): + def __init__(self, /) -> None: ... -class IndexExpression(Generic[_BoolType]): - maketuple: _BoolType - def __init__(self, maketuple: _BoolType) -> None: ... +class IndexExpression(Generic[_BoolT_co]): + maketuple: _BoolT_co + def __init__(self, maketuple: _BoolT_co) -> None: ... @overload - def __getitem__(self, item: _TupType) -> _TupType: ... # type: ignore[misc] + def __getitem__(self, item: _TupleT) -> _TupleT: ... @overload - def __getitem__(self: IndexExpression[Literal[True]], item: _T) -> tuple[_T]: ... + def __getitem__(self: IndexExpression[L[True]], item: _T) -> tuple[_T]: ... @overload - def __getitem__(self: IndexExpression[Literal[False]], item: _T) -> _T: ... + def __getitem__(self: IndexExpression[L[False]], item: _T) -> _T: ... + +@overload +def ix_(*args: _FiniteNestedSequence[_SupportsDType[_DTypeT]]) -> tuple[np.ndarray[_Shape, _DTypeT], ...]: ... +@overload +def ix_(*args: str | _NestedSequence[str]) -> tuple[NDArray[np.str_], ...]: ... +@overload +def ix_(*args: bytes | _NestedSequence[bytes]) -> tuple[NDArray[np.bytes_], ...]: ... +@overload +def ix_(*args: bool | _NestedSequence[bool]) -> tuple[NDArray[np.bool], ...]: ... +@overload +def ix_(*args: int | _NestedSequence[int]) -> tuple[NDArray[np.intp], ...]: ... +@overload +def ix_(*args: float | _NestedSequence[float]) -> tuple[NDArray[np.float64], ...]: ... +@overload +def ix_(*args: complex | _NestedSequence[complex]) -> tuple[NDArray[np.complex128], ...]: ... + +# +def fill_diagonal(a: NDArray[Any], val: object, wrap: bool = ...) -> None: ... + +# +def diag_indices(n: int, ndim: int = ...) -> tuple[NDArray[np.intp], ...]: ... +def diag_indices_from(arr: ArrayLike) -> tuple[NDArray[np.intp], ...]: ... -index_exp: IndexExpression[Literal[True]] -s_: IndexExpression[Literal[False]] +# +mgrid: Final[MGridClass] = ... +ogrid: Final[OGridClass] = ... -def fill_diagonal(a: NDArray[Any], val: Any, wrap: bool = ...) -> None: ... -def diag_indices(n: int, ndim: int = ...) -> tuple[NDArray[int_], ...]: ... -def diag_indices_from(arr: ArrayLike) -> tuple[NDArray[int_], ...]: ... +r_: Final[RClass] = ... +c_: Final[CClass] = ... -# NOTE: see `numpy/__init__.pyi` for `ndenumerate` and `ndindex` +index_exp: Final[IndexExpression[L[True]]] = ... +s_: Final[IndexExpression[L[False]]] = ... diff --git a/numpy/typing/tests/data/pass/index_tricks.py b/numpy/typing/tests/data/pass/index_tricks.py index 4c4c1195990a..dfc4ff2f314a 100644 --- a/numpy/typing/tests/data/pass/index_tricks.py +++ b/numpy/typing/tests/data/pass/index_tricks.py @@ -13,10 +13,6 @@ np.ndenumerate(AR_LIKE_f) np.ndenumerate(AR_LIKE_U) -np.ndenumerate(AR_i8).iter -np.ndenumerate(AR_LIKE_f).iter -np.ndenumerate(AR_LIKE_U).iter - next(np.ndenumerate(AR_i8)) next(np.ndenumerate(AR_LIKE_f)) next(np.ndenumerate(AR_LIKE_U)) diff --git a/numpy/typing/tests/data/reveal/index_tricks.pyi b/numpy/typing/tests/data/reveal/index_tricks.pyi index 1db10928d2f5..06071feddd79 100644 --- a/numpy/typing/tests/data/reveal/index_tricks.pyi +++ b/numpy/typing/tests/data/reveal/index_tricks.pyi @@ -18,23 +18,17 @@ AR_O: npt.NDArray[np.object_] assert_type(np.ndenumerate(AR_i8), np.ndenumerate[np.int64]) assert_type(np.ndenumerate(AR_LIKE_f), np.ndenumerate[np.float64]) assert_type(np.ndenumerate(AR_LIKE_U), np.ndenumerate[np.str_]) -assert_type(np.ndenumerate(AR_LIKE_O), np.ndenumerate[np.object_]) - -assert_type(np.ndenumerate(AR_i8).iter, np.flatiter[npt.NDArray[np.int64]]) -assert_type(np.ndenumerate(AR_LIKE_f).iter, np.flatiter[npt.NDArray[np.float64]]) -assert_type(np.ndenumerate(AR_LIKE_U).iter, np.flatiter[npt.NDArray[np.str_]]) -assert_type(np.ndenumerate(AR_LIKE_O).iter, np.flatiter[npt.NDArray[np.object_]]) +assert_type(np.ndenumerate(AR_LIKE_O), np.ndenumerate[Any]) assert_type(next(np.ndenumerate(AR_i8)), tuple[tuple[int, ...], np.int64]) assert_type(next(np.ndenumerate(AR_LIKE_f)), tuple[tuple[int, ...], np.float64]) assert_type(next(np.ndenumerate(AR_LIKE_U)), tuple[tuple[int, ...], np.str_]) -# this fails due to an unknown mypy bug -# assert_type(next(np.ndenumerate(AR_LIKE_O)), tuple[tuple[int, ...], Any]) +assert_type(next(np.ndenumerate(AR_LIKE_O)), tuple[tuple[int, ...], Any]) assert_type(iter(np.ndenumerate(AR_i8)), np.ndenumerate[np.int64]) assert_type(iter(np.ndenumerate(AR_LIKE_f)), np.ndenumerate[np.float64]) assert_type(iter(np.ndenumerate(AR_LIKE_U)), np.ndenumerate[np.str_]) -assert_type(iter(np.ndenumerate(AR_LIKE_O)), np.ndenumerate[np.object_]) +assert_type(iter(np.ndenumerate(AR_LIKE_O)), np.ndenumerate[Any]) assert_type(np.ndindex(1, 2, 3), np.ndindex) assert_type(np.ndindex((1, 2, 3)), np.ndindex) From 710d3b1e9c4253579a199d9828537fabbab051b1 Mon Sep 17 00:00:00 2001 From: jorenham Date: Sat, 15 Mar 2025 03:23:05 +0100 Subject: [PATCH 5/6] TYP: work around a quantum-entangled mypy issue, somehow --- numpy/_core/fromnumeric.pyi | 72 ++++++++++--------- .../typing/tests/data/reveal/fromnumeric.pyi | 5 +- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/numpy/_core/fromnumeric.pyi b/numpy/_core/fromnumeric.pyi index 3de05f3db362..52f48efa9345 100644 --- a/numpy/_core/fromnumeric.pyi +++ b/numpy/_core/fromnumeric.pyi @@ -3,7 +3,6 @@ from collections.abc import Sequence from typing import ( Any, Literal, - NoReturn, Protocol, SupportsIndex, TypeAlias, @@ -11,6 +10,8 @@ from typing import ( overload, type_check_only, ) + +from _typeshed import Incomplete from typing_extensions import Never, deprecated import numpy as np @@ -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 @@ -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, @@ -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, @@ -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 = ..., @@ -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, @@ -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( diff --git a/numpy/typing/tests/data/reveal/fromnumeric.pyi b/numpy/typing/tests/data/reveal/fromnumeric.pyi index 366e34d8af99..7e778dc58410 100644 --- a/numpy/typing/tests/data/reveal/fromnumeric.pyi +++ b/numpy/typing/tests/data/reveal/fromnumeric.pyi @@ -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 @@ -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], ...]) From c80a57ffae3279936b76c4d56432709987d2922e Mon Sep 17 00:00:00 2001 From: jorenham Date: Sat, 15 Mar 2025 02:13:11 +0100 Subject: [PATCH 6/6] TYP: fix stubtest errors in ``numpy.lib._twodim_base_impl`` Ported from numpy/numtype#245 --- This fixes incorrect parameter names of `tril` and `triu`, and resolves a typing error in the signatures of `eye` and `tri`. --- numpy/lib/_twodim_base_impl.pyi | 90 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/numpy/lib/_twodim_base_impl.pyi b/numpy/lib/_twodim_base_impl.pyi index e748e91fb908..5d3ea54511b8 100644 --- a/numpy/lib/_twodim_base_impl.pyi +++ b/numpy/lib/_twodim_base_impl.pyi @@ -10,7 +10,6 @@ from typing import ( import numpy as np from numpy import ( generic, - number, timedelta64, datetime64, int_, @@ -56,14 +55,28 @@ __all__ = [ "triu_indices_from", ] +### + _T = TypeVar("_T") _SCT = TypeVar("_SCT", bound=generic) +_SCT_complex = TypeVar("_SCT_complex", bound=np.complexfloating) +_SCT_inexact = TypeVar("_SCT_inexact", bound=np.inexact) +_SCT_number_co = TypeVar("_SCT_number_co", bound=_Number_co) # The returned arrays dtype must be compatible with `np.equal` -_MaskFunc: TypeAlias = Callable[ - [NDArray[int_], _T], - NDArray[number[Any] | np.bool | timedelta64 | datetime64 | object_], -] +_MaskFunc: TypeAlias = Callable[[NDArray[int_], _T], NDArray[_Number_co | timedelta64 | datetime64 | object_]] + +_Int_co: TypeAlias = np.integer | np.bool +_Float_co: TypeAlias = np.floating | _Int_co +_Number_co: TypeAlias = np.number | np.bool + +_ArrayLike1D: TypeAlias = _SupportsArray[np.dtype[_SCT]] | Sequence[_SCT] +_ArrayLike1DInt_co: TypeAlias = _SupportsArray[np.dtype[_Int_co]] | Sequence[int | _Int_co] +_ArrayLike1DFloat_co: TypeAlias = _SupportsArray[np.dtype[_Float_co]] | Sequence[float | _Float_co] +_ArrayLike2DFloat_co: TypeAlias = _SupportsArray[np.dtype[_Float_co]] | Sequence[_ArrayLike1DFloat_co] +_ArrayLike1DNumber_co: TypeAlias = _SupportsArray[np.dtype[_Number_co]] | Sequence[complex | _Number_co] + +### @overload def fliplr(m: _ArrayLike[_SCT]) -> NDArray[_SCT]: ... @@ -87,13 +100,24 @@ def eye( like: None | _SupportsArrayFunc = ..., ) -> NDArray[float64]: ... @overload +def eye( + N: int, + M: None | int, + k: int, + dtype: _DTypeLike[_SCT], + order: _OrderCF = ..., + *, + device: None | L["cpu"] = ..., + like: None | _SupportsArrayFunc = ..., +) -> NDArray[_SCT]: ... +@overload def eye( N: int, M: None | int = ..., k: int = ..., - dtype: _DTypeLike[_SCT] = ..., - order: _OrderCF = ..., *, + dtype: _DTypeLike[_SCT], + order: _OrderCF = ..., device: None | L["cpu"] = ..., like: None | _SupportsArrayFunc = ..., ) -> NDArray[_SCT]: ... @@ -129,12 +153,21 @@ def tri( like: None | _SupportsArrayFunc = ... ) -> NDArray[float64]: ... @overload +def tri( + N: int, + M: None | int, + k: int, + dtype: _DTypeLike[_SCT], + *, + like: None | _SupportsArrayFunc = ... +) -> NDArray[_SCT]: ... +@overload def tri( N: int, M: None | int = ..., k: int = ..., - dtype: _DTypeLike[_SCT] = ..., *, + dtype: _DTypeLike[_SCT], like: None | _SupportsArrayFunc = ... ) -> NDArray[_SCT]: ... @overload @@ -148,14 +181,14 @@ def tri( ) -> NDArray[Any]: ... @overload -def tril(v: _ArrayLike[_SCT], k: int = ...) -> NDArray[_SCT]: ... +def tril(m: _ArrayLike[_SCT], k: int = 0) -> NDArray[_SCT]: ... @overload -def tril(v: ArrayLike, k: int = ...) -> NDArray[Any]: ... +def tril(m: ArrayLike, k: int = 0) -> NDArray[Any]: ... @overload -def triu(v: _ArrayLike[_SCT], k: int = ...) -> NDArray[_SCT]: ... +def triu(m: _ArrayLike[_SCT], k: int = 0) -> NDArray[_SCT]: ... @overload -def triu(v: ArrayLike, k: int = ...) -> NDArray[Any]: ... +def triu(m: ArrayLike, k: int = 0) -> NDArray[Any]: ... @overload def vander( # type: ignore[misc] @@ -182,38 +215,6 @@ def vander( increasing: bool = ..., ) -> NDArray[object_]: ... - -_Int_co: TypeAlias = np.integer[Any] | np.bool -_Float_co: TypeAlias = np.floating[Any] | _Int_co -_Number_co: TypeAlias = np.number[Any] | np.bool - -_ArrayLike1D: TypeAlias = _SupportsArray[np.dtype[_SCT]] | Sequence[_SCT] -_ArrayLike2D: TypeAlias = ( - _SupportsArray[np.dtype[_SCT]] - | Sequence[_ArrayLike1D[_SCT]] -) - -_ArrayLike1DInt_co: TypeAlias = ( - _SupportsArray[np.dtype[_Int_co]] - | Sequence[int | _Int_co] -) -_ArrayLike1DFloat_co: TypeAlias = ( - _SupportsArray[np.dtype[_Float_co]] - | Sequence[float | int | _Float_co] -) -_ArrayLike2DFloat_co: TypeAlias = ( - _SupportsArray[np.dtype[_Float_co]] - | Sequence[_ArrayLike1DFloat_co] -) -_ArrayLike1DNumber_co: TypeAlias = ( - _SupportsArray[np.dtype[_Number_co]] - | Sequence[int | float | complex | _Number_co] -) - -_SCT_complex = TypeVar("_SCT_complex", bound=np.complexfloating[Any, Any]) -_SCT_inexact = TypeVar("_SCT_inexact", bound=np.inexact[Any]) -_SCT_number_co = TypeVar("_SCT_number_co", bound=_Number_co) - @overload def histogram2d( x: _ArrayLike1D[_SCT_complex], @@ -344,7 +345,6 @@ def histogram2d( NDArray[_SCT_number_co | complex128 | float64], NDArray[_SCT_number_co | complex128 | float64] , ]: ... - @overload def histogram2d( x: _ArrayLike1DNumber_co,