Skip to content

Commit c694399

Browse files
charrisjorenham
andauthored
TYP: Backport typing fixes from main (2) (#28533)
* TYP: don't use literals in shape-types Partial backport of numpy/numtype#122 and numpy/numtype#152 * TYP: annotate the missing ``ufunc.resolve_dtypes`` method Ported from numpy/numtype#218 * TYP: stub ``numpy._core.overrides`` Ported from numpy/numtype#226 * TYP: stub ``numpy._utils`` Ported from numpy/numtype#225 * TYP: stub ``numpy._core._dtype[_ctypes]`` Ported from numpy/numtype#227 * TYP: stub the remaining ``numpy._core.*`` modules Ported from numpy/numtype#241 * TYP: stub the missing submodules of ``numpy.linalg`` Ported from numpy/numtype#248 * TYP: stub ``numpy._pyinstaller`` Ported from numpy/numtype#264 * TYP: stub ``numpy.fft.helper`` (deprecated) Ported from numpy/numtype#261 * TYP: annotate the missing deprecated ``row_stack`` function Ported from numpy/numtype#223 --------- Co-authored-by: jorenham <jhammudoglu@gmail.com>
1 parent 8c6007f commit c694399

36 files changed

+1131
-128
lines changed

numpy/__init__.pyi

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ from numpy.lib._polynomial_impl import (
574574

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

4806+
#
4807+
def resolve_dtypes(
4808+
self,
4809+
/,
4810+
dtypes: tuple[dtype[Any] | type | None, ...],
4811+
*,
4812+
signature: tuple[dtype[Any] | None, ...] | None = None,
4813+
casting: _CastingKind | None = None,
4814+
reduction: builtins.bool = False,
4815+
) -> tuple[dtype[Any], ...]: ...
4816+
48064817
# Parameters: `__name__`, `ntypes` and `identity`
48074818
absolute: _UFunc_Nin1_Nout1[L['absolute'], L[20], None]
48084819
add: _UFunc_Nin2_Nout1[L['add'], L[22], L[0]]

numpy/_core/_add_newdocs.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .overrides import get_array_function_like_doc as get_array_function_like_doc
2+
3+
def refer_to_array_attribute(attr: str, method: bool = True) -> tuple[str, str]: ...

numpy/_core/_add_newdocs_scalars.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections.abc import Iterable
2+
from typing import Final
3+
4+
import numpy as np
5+
6+
possible_aliases: Final[list[tuple[type[np.number], str, str]]] = ...
7+
_system: Final[str] = ...
8+
_machine: Final[str] = ...
9+
_doc_alias_string: Final[str] = ...
10+
_bool_docstring: Final[str] = ...
11+
int_name: str = ...
12+
float_name: str = ...
13+
14+
def numeric_type_aliases(aliases: list[tuple[str, str]]) -> list[tuple[type[np.number], str, str]]: ...
15+
def add_newdoc_for_scalar_type(obj: str, fixed_aliases: Iterable[str], doc: str) -> None: ...
16+
def _get_platform_and_machine() -> tuple[str, str]: ...

numpy/_core/_dtype.pyi

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Any, Final, TypeAlias, TypedDict, overload, type_check_only
2+
from typing import Literal as L
3+
4+
from typing_extensions import ReadOnly, TypeVar
5+
6+
import numpy as np
7+
8+
###
9+
10+
_T = TypeVar("_T")
11+
12+
_Name: TypeAlias = L["uint", "int", "complex", "float", "bool", "void", "object", "datetime", "timedelta", "bytes", "str"]
13+
14+
@type_check_only
15+
class _KindToStemType(TypedDict):
16+
u: ReadOnly[L["uint"]]
17+
i: ReadOnly[L["int"]]
18+
c: ReadOnly[L["complex"]]
19+
f: ReadOnly[L["float"]]
20+
b: ReadOnly[L["bool"]]
21+
V: ReadOnly[L["void"]]
22+
O: ReadOnly[L["object"]]
23+
M: ReadOnly[L["datetime"]]
24+
m: ReadOnly[L["timedelta"]]
25+
S: ReadOnly[L["bytes"]]
26+
U: ReadOnly[L["str"]]
27+
28+
###
29+
30+
_kind_to_stem: Final[_KindToStemType] = ...
31+
32+
#
33+
def _kind_name(dtype: np.dtype[Any]) -> _Name: ...
34+
def __str__(dtype: np.dtype[Any]) -> str: ...
35+
def __repr__(dtype: np.dtype[Any]) -> str: ...
36+
37+
#
38+
def _isunsized(dtype: np.dtype[Any]) -> bool: ...
39+
def _is_packed(dtype: np.dtype[Any]) -> bool: ...
40+
def _name_includes_bit_suffix(dtype: np.dtype[Any]) -> bool: ...
41+
42+
#
43+
def _construction_repr(dtype: np.dtype[Any], include_align: bool = False, short: bool = False) -> str: ...
44+
def _scalar_str(dtype: np.dtype[Any], short: bool) -> str: ...
45+
def _byte_order_str(dtype: np.dtype[Any]) -> str: ...
46+
def _datetime_metadata_str(dtype: np.dtype[Any]) -> str: ...
47+
def _struct_dict_str(dtype: np.dtype[Any], includealignedflag: bool) -> str: ...
48+
def _struct_list_str(dtype: np.dtype[Any]) -> str: ...
49+
def _struct_str(dtype: np.dtype[Any], include_align: bool) -> str: ...
50+
def _subarray_str(dtype: np.dtype[Any]) -> str: ...
51+
def _name_get(dtype: np.dtype[Any]) -> str: ...
52+
53+
#
54+
@overload
55+
def _unpack_field(dtype: np.dtype[Any], offset: int, title: _T) -> tuple[np.dtype[Any], int, _T]: ...
56+
@overload
57+
def _unpack_field(dtype: np.dtype[Any], offset: int, title: None = None) -> tuple[np.dtype[Any], int, None]: ...
58+
def _aligned_offset(offset: int, alignment: int) -> int: ...

numpy/_core/_dtype_ctypes.pyi

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import _ctypes
2+
import ctypes as ct
3+
from typing import Any, overload
4+
5+
import numpy as np
6+
7+
#
8+
@overload
9+
def dtype_from_ctypes_type(t: type[_ctypes.Array[Any] | _ctypes.Structure]) -> np.dtype[np.void]: ...
10+
@overload
11+
def dtype_from_ctypes_type(t: type[ct.c_bool]) -> np.dtype[np.bool]: ...
12+
@overload
13+
def dtype_from_ctypes_type(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ...
14+
@overload
15+
def dtype_from_ctypes_type(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ...
16+
@overload
17+
def dtype_from_ctypes_type(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ...
18+
@overload
19+
def dtype_from_ctypes_type(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ...
20+
@overload
21+
def dtype_from_ctypes_type(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ...
22+
@overload
23+
def dtype_from_ctypes_type(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ...
24+
@overload
25+
def dtype_from_ctypes_type(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ...
26+
@overload
27+
def dtype_from_ctypes_type(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ...
28+
@overload
29+
def dtype_from_ctypes_type(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ...
30+
@overload
31+
def dtype_from_ctypes_type(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ...
32+
@overload
33+
def dtype_from_ctypes_type(t: type[ct.c_float]) -> np.dtype[np.float32]: ...
34+
@overload
35+
def dtype_from_ctypes_type(t: type[ct.c_double]) -> np.dtype[np.float64]: ...
36+
@overload
37+
def dtype_from_ctypes_type(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ...
38+
@overload
39+
def dtype_from_ctypes_type(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ...
40+
@overload
41+
def dtype_from_ctypes_type(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ...
42+
43+
# NOTE: the complex ctypes on python>=3.14 are not yet supported at runtim, see
44+
# https://github.com/numpy/numpy/issues/28360
45+
46+
#
47+
def _from_ctypes_array(t: type[_ctypes.Array[Any]]) -> np.dtype[np.void]: ...
48+
def _from_ctypes_structure(t: type[_ctypes.Structure]) -> np.dtype[np.void]: ...
49+
def _from_ctypes_union(t: type[_ctypes.Union]) -> np.dtype[np.void]: ...
50+
51+
# keep in sync with `dtype_from_ctypes_type` (minus the first overload)
52+
@overload
53+
def _from_ctypes_scalar(t: type[ct.c_bool]) -> np.dtype[np.bool]: ...
54+
@overload
55+
def _from_ctypes_scalar(t: type[ct.c_int8 | ct.c_byte]) -> np.dtype[np.int8]: ...
56+
@overload
57+
def _from_ctypes_scalar(t: type[ct.c_uint8 | ct.c_ubyte]) -> np.dtype[np.uint8]: ...
58+
@overload
59+
def _from_ctypes_scalar(t: type[ct.c_int16 | ct.c_short]) -> np.dtype[np.int16]: ...
60+
@overload
61+
def _from_ctypes_scalar(t: type[ct.c_uint16 | ct.c_ushort]) -> np.dtype[np.uint16]: ...
62+
@overload
63+
def _from_ctypes_scalar(t: type[ct.c_int32 | ct.c_int]) -> np.dtype[np.int32]: ...
64+
@overload
65+
def _from_ctypes_scalar(t: type[ct.c_uint32 | ct.c_uint]) -> np.dtype[np.uint32]: ...
66+
@overload
67+
def _from_ctypes_scalar(t: type[ct.c_ssize_t | ct.c_long]) -> np.dtype[np.int32 | np.int64]: ...
68+
@overload
69+
def _from_ctypes_scalar(t: type[ct.c_size_t | ct.c_ulong]) -> np.dtype[np.uint32 | np.uint64]: ...
70+
@overload
71+
def _from_ctypes_scalar(t: type[ct.c_int64 | ct.c_longlong]) -> np.dtype[np.int64]: ...
72+
@overload
73+
def _from_ctypes_scalar(t: type[ct.c_uint64 | ct.c_ulonglong]) -> np.dtype[np.uint64]: ...
74+
@overload
75+
def _from_ctypes_scalar(t: type[ct.c_float]) -> np.dtype[np.float32]: ...
76+
@overload
77+
def _from_ctypes_scalar(t: type[ct.c_double]) -> np.dtype[np.float64]: ...
78+
@overload
79+
def _from_ctypes_scalar(t: type[ct.c_longdouble]) -> np.dtype[np.longdouble]: ...
80+
@overload
81+
def _from_ctypes_scalar(t: type[ct.c_char]) -> np.dtype[np.bytes_]: ...
82+
@overload
83+
def _from_ctypes_scalar(t: type[ct.py_object[Any]]) -> np.dtype[np.object_]: ...

numpy/_core/_exceptions.pyi

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from collections.abc import Iterable
2+
from typing import Any, Final, overload
3+
4+
from typing_extensions import TypeVar, Unpack
5+
6+
import numpy as np
7+
from numpy import _CastingKind
8+
from numpy._utils import set_module as set_module
9+
10+
###
11+
12+
_T = TypeVar("_T")
13+
_TupleT = TypeVar("_TupleT", bound=tuple[()] | tuple[Any, Any, Unpack[tuple[Any, ...]]])
14+
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
15+
16+
###
17+
18+
class UFuncTypeError(TypeError):
19+
ufunc: Final[np.ufunc]
20+
def __init__(self, /, ufunc: np.ufunc) -> None: ...
21+
22+
class _UFuncNoLoopError(UFuncTypeError):
23+
dtypes: tuple[np.dtype[Any], ...]
24+
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...
25+
26+
class _UFuncBinaryResolutionError(_UFuncNoLoopError):
27+
dtypes: tuple[np.dtype[Any], np.dtype[Any]]
28+
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...
29+
30+
class _UFuncCastingError(UFuncTypeError):
31+
casting: Final[_CastingKind]
32+
from_: Final[np.dtype[Any]]
33+
to: Final[np.dtype[Any]]
34+
def __init__(self, /, ufunc: np.ufunc, casting: _CastingKind, from_: np.dtype[Any], to: np.dtype[Any]) -> None: ...
35+
36+
class _UFuncInputCastingError(_UFuncCastingError):
37+
in_i: Final[int]
38+
def __init__(
39+
self,
40+
/,
41+
ufunc: np.ufunc,
42+
casting: _CastingKind,
43+
from_: np.dtype[Any],
44+
to: np.dtype[Any],
45+
i: int,
46+
) -> None: ...
47+
48+
class _UFuncOutputCastingError(_UFuncCastingError):
49+
out_i: Final[int]
50+
def __init__(
51+
self,
52+
/,
53+
ufunc: np.ufunc,
54+
casting: _CastingKind,
55+
from_: np.dtype[Any],
56+
to: np.dtype[Any],
57+
i: int,
58+
) -> None: ...
59+
60+
class _ArrayMemoryError(MemoryError):
61+
shape: tuple[int, ...]
62+
dtype: np.dtype[Any]
63+
def __init__(self, /, shape: tuple[int, ...], dtype: np.dtype[Any]) -> None: ...
64+
@property
65+
def _total_size(self) -> int: ...
66+
@staticmethod
67+
def _size_to_string(num_bytes: int) -> str: ...
68+
69+
@overload
70+
def _unpack_tuple(tup: tuple[_T]) -> _T: ...
71+
@overload
72+
def _unpack_tuple(tup: _TupleT) -> _TupleT: ...
73+
def _display_as_base(cls: type[_ExceptionT]) -> type[_ExceptionT]: ...

numpy/_core/_machar.pyi

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from collections.abc import Iterable
2+
from typing import Any, Final, overload
3+
4+
from typing_extensions import TypeVar, Unpack
5+
6+
import numpy as np
7+
from numpy import _CastingKind
8+
from numpy._utils import set_module as set_module
9+
10+
###
11+
12+
_T = TypeVar("_T")
13+
_TupleT = TypeVar("_TupleT", bound=tuple[()] | tuple[Any, Any, Unpack[tuple[Any, ...]]])
14+
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
15+
16+
###
17+
18+
class UFuncTypeError(TypeError):
19+
ufunc: Final[np.ufunc]
20+
def __init__(self, /, ufunc: np.ufunc) -> None: ...
21+
22+
class _UFuncNoLoopError(UFuncTypeError):
23+
dtypes: tuple[np.dtype[Any], ...]
24+
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...
25+
26+
class _UFuncBinaryResolutionError(_UFuncNoLoopError):
27+
dtypes: tuple[np.dtype[Any], np.dtype[Any]]
28+
def __init__(self, /, ufunc: np.ufunc, dtypes: Iterable[np.dtype[Any]]) -> None: ...
29+
30+
class _UFuncCastingError(UFuncTypeError):
31+
casting: Final[_CastingKind]
32+
from_: Final[np.dtype[Any]]
33+
to: Final[np.dtype[Any]]
34+
def __init__(self, /, ufunc: np.ufunc, casting: _CastingKind, from_: np.dtype[Any], to: np.dtype[Any]) -> None: ...
35+
36+
class _UFuncInputCastingError(_UFuncCastingError):
37+
in_i: Final[int]
38+
def __init__(
39+
self,
40+
/,
41+
ufunc: np.ufunc,
42+
casting: _CastingKind,
43+
from_: np.dtype[Any],
44+
to: np.dtype[Any],
45+
i: int,
46+
) -> None: ...
47+
48+
class _UFuncOutputCastingError(_UFuncCastingError):
49+
out_i: Final[int]
50+
def __init__(
51+
self,
52+
/,
53+
ufunc: np.ufunc,
54+
casting: _CastingKind,
55+
from_: np.dtype[Any],
56+
to: np.dtype[Any],
57+
i: int,
58+
) -> None: ...
59+
60+
class _ArrayMemoryError(MemoryError):
61+
shape: tuple[int, ...]
62+
dtype: np.dtype[Any]
63+
def __init__(self, /, shape: tuple[int, ...], dtype: np.dtype[Any]) -> None: ...
64+
@property
65+
def _total_size(self) -> int: ...
66+
@staticmethod
67+
def _size_to_string(num_bytes: int) -> str: ...
68+
69+
@overload
70+
def _unpack_tuple(tup: tuple[_T]) -> _T: ...
71+
@overload
72+
def _unpack_tuple(tup: _TupleT) -> _TupleT: ...
73+
def _display_as_base(cls: type[_ExceptionT]) -> type[_ExceptionT]: ...

numpy/_core/_methods.pyi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections.abc import Callable
2+
from typing import Any, TypeAlias
3+
4+
from typing_extensions import Concatenate
5+
6+
import numpy as np
7+
8+
from . import _exceptions as _exceptions
9+
10+
###
11+
12+
_Reduce2: TypeAlias = Callable[Concatenate[object, ...], Any]
13+
14+
###
15+
16+
bool_dt: np.dtype[np.bool] = ...
17+
umr_maximum: _Reduce2 = ...
18+
umr_minimum: _Reduce2 = ...
19+
umr_sum: _Reduce2 = ...
20+
umr_prod: _Reduce2 = ...
21+
umr_bitwise_count = np.bitwise_count
22+
umr_any: _Reduce2 = ...
23+
umr_all: _Reduce2 = ...
24+
_complex_to_float: dict[np.dtype[np.complexfloating], np.dtype[np.floating]] = ...

0 commit comments

Comments
 (0)