Skip to content

TYP: Backport typing fixes from main (2) #28533

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 10 commits into from
Mar 15, 2025
15 changes: 13 additions & 2 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,7 @@ from numpy.lib._polynomial_impl import (

from numpy.lib._shape_base_impl import (
column_stack,
row_stack,
dstack,
array_split,
split,
Expand Down Expand Up @@ -730,10 +731,9 @@ __all__ = [ # noqa: RUF022
"histogram2d", "mask_indices", "tril_indices", "tril_indices_from", "triu_indices",
"triu_indices_from",
# lib._shape_base_impl.__all__
# NOTE: `row_stack` is omitted because it is deprecated
"column_stack", "dstack", "array_split", "split", "hsplit", "vsplit", "dsplit",
"apply_over_axes", "expand_dims", "apply_along_axis", "kron", "tile",
"take_along_axis", "put_along_axis",
"take_along_axis", "put_along_axis", "row_stack",
# lib._type_check_impl.__all__
"iscomplexobj", "isrealobj", "imag", "iscomplex", "isreal", "nan_to_num", "real",
"real_if_close", "typename", "mintypecode", "common_type",
Expand Down Expand Up @@ -4803,6 +4803,17 @@ class ufunc:
# outputs, so we can't type it very precisely.
def at(self, /, *args: Any, **kwargs: Any) -> None: ...

#
def resolve_dtypes(
self,
/,
dtypes: tuple[dtype[Any] | type | None, ...],
*,
signature: tuple[dtype[Any] | None, ...] | None = None,
casting: _CastingKind | None = None,
reduction: builtins.bool = False,
) -> tuple[dtype[Any], ...]: ...

# Parameters: `__name__`, `ntypes` and `identity`
absolute: _UFunc_Nin1_Nout1[L['absolute'], L[20], None]
add: _UFunc_Nin2_Nout1[L['add'], L[22], L[0]]
Expand Down
3 changes: 3 additions & 0 deletions numpy/_core/_add_newdocs.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .overrides import get_array_function_like_doc as get_array_function_like_doc

def refer_to_array_attribute(attr: str, method: bool = True) -> tuple[str, str]: ...
16 changes: 16 additions & 0 deletions numpy/_core/_add_newdocs_scalars.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from collections.abc import Iterable
from typing import Final

import numpy as np

possible_aliases: Final[list[tuple[type[np.number], str, str]]] = ...
_system: Final[str] = ...
_machine: Final[str] = ...
_doc_alias_string: Final[str] = ...
_bool_docstring: Final[str] = ...
int_name: str = ...
float_name: str = ...

def numeric_type_aliases(aliases: list[tuple[str, str]]) -> list[tuple[type[np.number], str, str]]: ...
def add_newdoc_for_scalar_type(obj: str, fixed_aliases: Iterable[str], doc: str) -> None: ...
def _get_platform_and_machine() -> tuple[str, str]: ...
58 changes: 58 additions & 0 deletions numpy/_core/_dtype.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from typing import Any, Final, TypeAlias, TypedDict, overload, type_check_only
from typing import Literal as L

from typing_extensions import ReadOnly, TypeVar

import numpy as np

###

_T = TypeVar("_T")

_Name: TypeAlias = L["uint", "int", "complex", "float", "bool", "void", "object", "datetime", "timedelta", "bytes", "str"]

@type_check_only
class _KindToStemType(TypedDict):
u: ReadOnly[L["uint"]]
i: ReadOnly[L["int"]]
c: ReadOnly[L["complex"]]
f: ReadOnly[L["float"]]
b: ReadOnly[L["bool"]]
V: ReadOnly[L["void"]]
O: ReadOnly[L["object"]]
M: ReadOnly[L["datetime"]]
m: ReadOnly[L["timedelta"]]
S: ReadOnly[L["bytes"]]
U: ReadOnly[L["str"]]

###

_kind_to_stem: Final[_KindToStemType] = ...

#
def _kind_name(dtype: np.dtype[Any]) -> _Name: ...
def __str__(dtype: np.dtype[Any]) -> str: ...
def __repr__(dtype: np.dtype[Any]) -> str: ...

#
def _isunsized(dtype: np.dtype[Any]) -> bool: ...
def _is_packed(dtype: np.dtype[Any]) -> bool: ...
def _name_includes_bit_suffix(dtype: np.dtype[Any]) -> bool: ...

#
def _construction_repr(dtype: np.dtype[Any], include_align: bool = False, short: bool = False) -> str: ...
def _scalar_str(dtype: np.dtype[Any], short: bool) -> str: ...
def _byte_order_str(dtype: np.dtype[Any]) -> str: ...
def _datetime_metadata_str(dtype: np.dtype[Any]) -> str: ...
def _struct_dict_str(dtype: np.dtype[Any], includealignedflag: bool) -> str: ...
def _struct_list_str(dtype: np.dtype[Any]) -> str: ...
def _struct_str(dtype: np.dtype[Any], include_align: bool) -> str: ...
def _subarray_str(dtype: np.dtype[Any]) -> str: ...
def _name_get(dtype: np.dtype[Any]) -> str: ...

#
@overload
def _unpack_field(dtype: np.dtype[Any], offset: int, title: _T) -> tuple[np.dtype[Any], int, _T]: ...
@overload
def _unpack_field(dtype: np.dtype[Any], offset: int, title: None = None) -> tuple[np.dtype[Any], int, None]: ...
def _aligned_offset(offset: int, alignment: int) -> int: ...
83 changes: 83 additions & 0 deletions numpy/_core/_dtype_ctypes.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import _ctypes
import ctypes as ct
from typing import Any, overload

import numpy as np

#
@overload
def dtype_from_ctypes_type(t: type[_ctypes.Array[Any] | _ctypes.Structure]) -> np.dtype[np.void]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_bool]) -> np.dtype[np.bool]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_float]) -> np.dtype[np.float32]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_double]) -> np.dtype[np.float64]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ...
@overload
def dtype_from_ctypes_type(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ...

# NOTE: the complex ctypes on python>=3.14 are not yet supported at runtim, see
# https://github.com/numpy/numpy/issues/28360

#
def _from_ctypes_array(t: type[_ctypes.Array[Any]]) -> np.dtype[np.void]: ...
def _from_ctypes_structure(t: type[_ctypes.Structure]) -> np.dtype[np.void]: ...
def _from_ctypes_union(t: type[_ctypes.Union]) -> np.dtype[np.void]: ...

# keep in sync with `dtype_from_ctypes_type` (minus the first overload)
@overload
def _from_ctypes_scalar(t: type[ct.c_bool]) -> np.dtype[np.bool]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_float]) -> np.dtype[np.float32]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_double]) -> np.dtype[np.float64]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ...
@overload
def _from_ctypes_scalar(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ...
@overload
def _from_ctypes_scalar(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ...
73 changes: 73 additions & 0 deletions numpy/_core/_exceptions.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from collections.abc import Iterable
from typing import Any, Final, overload

from typing_extensions import TypeVar, Unpack

import numpy as np
from numpy import _CastingKind
from numpy._utils import set_module as set_module

###

_T = TypeVar("_T")
_TupleT = TypeVar("_TupleT", bound=tuple[()] | tuple[Any, Any, Unpack[tuple[Any, ...]]])
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)

###

class UFuncTypeError(TypeError):
ufunc: Final[np.ufunc]
def __init__(self, /, ufunc: np.ufunc) -> None: ...

class _UFuncNoLoopError(UFuncTypeError):
dtypes: tuple[np.dtype[Any], ...]
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...

class _UFuncBinaryResolutionError(_UFuncNoLoopError):
dtypes: tuple[np.dtype[Any], np.dtype[Any]]
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...

class _UFuncCastingError(UFuncTypeError):
casting: Final[_CastingKind]
from_: Final[np.dtype[Any]]
to: Final[np.dtype[Any]]
def __init__(self, /, ufunc: np.ufunc, casting: _CastingKind, from_: np.dtype[Any], to: np.dtype[Any]) -> None: ...

class _UFuncInputCastingError(_UFuncCastingError):
in_i: Final[int]
def __init__(
self,
/,
ufunc: np.ufunc,
casting: _CastingKind,
from_: np.dtype[Any],
to: np.dtype[Any],
i: int,
) -> None: ...

class _UFuncOutputCastingError(_UFuncCastingError):
out_i: Final[int]
def __init__(
self,
/,
ufunc: np.ufunc,
casting: _CastingKind,
from_: np.dtype[Any],
to: np.dtype[Any],
i: int,
) -> None: ...

class _ArrayMemoryError(MemoryError):
shape: tuple[int, ...]
dtype: np.dtype[Any]
def __init__(self, /, shape: tuple[int, ...], dtype: np.dtype[Any]) -> None: ...
@property
def _total_size(self) -> int: ...
@staticmethod
def _size_to_string(num_bytes: int) -> str: ...

@overload
def _unpack_tuple(tup: tuple[_T]) -> _T: ...
@overload
def _unpack_tuple(tup: _TupleT) -> _TupleT: ...
def _display_as_base(cls: type[_ExceptionT]) -> type[_ExceptionT]: ...
73 changes: 73 additions & 0 deletions numpy/_core/_machar.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from collections.abc import Iterable
from typing import Any, Final, overload

from typing_extensions import TypeVar, Unpack

import numpy as np
from numpy import _CastingKind
from numpy._utils import set_module as set_module

###

_T = TypeVar("_T")
_TupleT = TypeVar("_TupleT", bound=tuple[()] | tuple[Any, Any, Unpack[tuple[Any, ...]]])
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)

###

class UFuncTypeError(TypeError):
ufunc: Final[np.ufunc]
def __init__(self, /, ufunc: np.ufunc) -> None: ...

class _UFuncNoLoopError(UFuncTypeError):
dtypes: tuple[np.dtype[Any], ...]
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...

class _UFuncBinaryResolutionError(_UFuncNoLoopError):
dtypes: tuple[np.dtype[Any], np.dtype[Any]]
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...

class _UFuncCastingError(UFuncTypeError):
casting: Final[_CastingKind]
from_: Final[np.dtype[Any]]
to: Final[np.dtype[Any]]
def __init__(self, /, ufunc: np.ufunc, casting: _CastingKind, from_: np.dtype[Any], to: np.dtype[Any]) -> None: ...

class _UFuncInputCastingError(_UFuncCastingError):
in_i: Final[int]
def __init__(
self,
/,
ufunc: np.ufunc,
casting: _CastingKind,
from_: np.dtype[Any],
to: np.dtype[Any],
i: int,
) -> None: ...

class _UFuncOutputCastingError(_UFuncCastingError):
out_i: Final[int]
def __init__(
self,
/,
ufunc: np.ufunc,
casting: _CastingKind,
from_: np.dtype[Any],
to: np.dtype[Any],
i: int,
) -> None: ...

class _ArrayMemoryError(MemoryError):
shape: tuple[int, ...]
dtype: np.dtype[Any]
def __init__(self, /, shape: tuple[int, ...], dtype: np.dtype[Any]) -> None: ...
@property
def _total_size(self) -> int: ...
@staticmethod
def _size_to_string(num_bytes: int) -> str: ...

@overload
def _unpack_tuple(tup: tuple[_T]) -> _T: ...
@overload
def _unpack_tuple(tup: _TupleT) -> _TupleT: ...
def _display_as_base(cls: type[_ExceptionT]) -> type[_ExceptionT]: ...
24 changes: 24 additions & 0 deletions numpy/_core/_methods.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from collections.abc import Callable
from typing import Any, TypeAlias

from typing_extensions import Concatenate

import numpy as np

from . import _exceptions as _exceptions

###

_Reduce2: TypeAlias = Callable[Concatenate[object, ...], Any]

###

bool_dt: np.dtype[np.bool] = ...
umr_maximum: _Reduce2 = ...
umr_minimum: _Reduce2 = ...
umr_sum: _Reduce2 = ...
umr_prod: _Reduce2 = ...
umr_bitwise_count = np.bitwise_count
umr_any: _Reduce2 = ...
umr_all: _Reduce2 = ...
_complex_to_float: dict[np.dtype[np.complexfloating], np.dtype[np.floating]] = ...
Loading
Loading