diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5ea34545..25ae79ee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,13 +45,16 @@ jobs: src/numpy-stubs/_core/__init__.pyi src/numpy-stubs/_core/_asarray.pyi src/numpy-stubs/_core/_internal.pyi + src/numpy-stubs/_core/_multiarray_umath.pyi src/numpy-stubs/_core/_type_aliases.pyi src/numpy-stubs/_core/_ufunc_config.pyi src/numpy-stubs/_core/arrayprint.pyi + src/numpy-stubs/_core/multiarray.pyi src/numpy-stubs/_core/numerictypes.pyi src/numpy-stubs/_core/records.pyi src/numpy-stubs/_core/shape_base.pyi src/numpy-stubs/_core/strings.pyi + src/numpy-stubs/_core/umath.pyi src/numpy-stubs/_typing src/numpy-stubs/char src/numpy-stubs/core @@ -97,17 +100,20 @@ jobs: - name: mypy (numpy-stubs) run: > - uv run --no-editable mypy + uv run test/mypy.py src/numpy-stubs/_core/__init__.pyi src/numpy-stubs/_core/_asarray.pyi src/numpy-stubs/_core/_internal.pyi + src/numpy-stubs/_core/_multiarray_umath.pyi src/numpy-stubs/_core/_type_aliases.pyi src/numpy-stubs/_core/_ufunc_config.pyi src/numpy-stubs/_core/arrayprint.pyi + src/numpy-stubs/_core/multiarray.pyi src/numpy-stubs/_core/numerictypes.pyi src/numpy-stubs/_core/records.pyi src/numpy-stubs/_core/shape_base.pyi src/numpy-stubs/_core/strings.pyi + src/numpy-stubs/_core/umath.pyi src/numpy-stubs/_typing src/numpy-stubs/char src/numpy-stubs/core diff --git a/src/numpy-stubs/_core/_multiarray_umath.pyi b/src/numpy-stubs/_core/_multiarray_umath.pyi index 5cc19cec..fb7cad7c 100644 --- a/src/numpy-stubs/_core/_multiarray_umath.pyi +++ b/src/numpy-stubs/_core/_multiarray_umath.pyi @@ -1,3 +1,4 @@ +import _contextvars import datetime as dt from _typeshed import Incomplete, StrOrBytesPath, SupportsLenAndGetItem from builtins import bool as py_bool @@ -18,11 +19,112 @@ from typing import ( overload, type_check_only, ) -from typing_extensions import CapsuleType, Self, TypeAliasType, TypeVar, Unpack +from typing_extensions import CapsuleType, Self, TypeAliasType, TypeVar, Unpack, deprecated import numpy as np import numpy.typing as npt -from numpy import _AnyShapeT, _CastingKind, _ModeKind, _OrderCF, _OrderKACF, _SupportsBuffer, _SupportsFileMethods # noqa: ICN003 +from numpy import ( # noqa: ICN003 + _AnyShapeT, + _CastingKind, + _ModeKind, + _OrderCF, + _OrderKACF, + _SupportsBuffer, + _SupportsFileMethods, + # NOTE: These implicitly re-exported ufuncs are defined in this ext-module at runtime + absolute as absolute, + add as add, + arccos as arccos, + arccosh as arccosh, + arcsin as arcsin, + arcsinh as arcsinh, + arctan as arctan, + arctan2 as arctan2, + arctanh as arctanh, + bitwise_and as bitwise_and, + bitwise_count as bitwise_count, + bitwise_or as bitwise_or, + bitwise_xor as bitwise_xor, + cbrt as cbrt, + ceil as ceil, + conj as conj, + conjugate as conjugate, + copysign as copysign, + cos as cos, + cosh as cosh, + deg2rad as deg2rad, + degrees as degrees, + divide as divide, + divmod as divmod, + equal as equal, + exp as exp, + exp2 as exp2, + expm1 as expm1, + fabs as fabs, + float_power as float_power, + floor as floor, + floor_divide as floor_divide, + fmax as fmax, + fmin as fmin, + fmod as fmod, + frexp as frexp, + gcd as gcd, + greater as greater, + greater_equal as greater_equal, + heaviside as heaviside, + hypot as hypot, + invert as invert, + isfinite as isfinite, + isinf as isinf, + isnan as isnan, + isnat as isnat, + lcm as lcm, + ldexp as ldexp, + left_shift as left_shift, + less as less, + less_equal as less_equal, + log as log, + log1p as log1p, + log2 as log2, + log10 as log10, + logaddexp as logaddexp, + logaddexp2 as logaddexp2, + logical_and as logical_and, + logical_not as logical_not, + logical_or as logical_or, + logical_xor as logical_xor, + matvec as matvec, + maximum as maximum, + minimum as minimum, + mod as mod, + modf as modf, + multiply as multiply, + negative as negative, + nextafter as nextafter, + not_equal as not_equal, + positive as positive, + power as power, + rad2deg as rad2deg, + radians as radians, + reciprocal as reciprocal, + remainder as remainder, + right_shift as right_shift, + rint as rint, + sign as sign, + signbit as signbit, + sin as sin, + sinh as sinh, + spacing as spacing, + sqrt as sqrt, + square as square, + subtract as subtract, + tan as tan, + tanh as tanh, + true_divide as true_divide, + trunc as trunc, + vecdot as vecdot, + vecmat as vecmat, +) from numpy._globals import _CopyMode from numpy._typing import ( _ArrayLike, @@ -137,7 +239,9 @@ _Roll = TypeAliasType( _TimeUnit: TypeAlias = L["Y", "M", "D", "h", "m", "s", "ms", "us", "μs", "ns", "ps", "fs", "as"] _TimeZone: TypeAlias = L["naive", "UTC", "local"] | dt.tzinfo _CorrMode: TypeAlias = L[0, "valid", 1, "same", 2, "full"] +_ExtObjValue: TypeAlias = L["ignore", "warn", "raise", "call", "print", "log"] +_Ignored: TypeAlias = object _Copy: TypeAlias = py_bool | L[2] | _CopyMode _WeekMask: TypeAlias = str | Sequence[L[0, 1] | py_bool | np.bool] @@ -180,6 +284,10 @@ class _CanArray(Protocol[_ArrayT_co]): class _CanDLPack(Protocol[_T_contra]): def __dlpack__(self, /, *, stream: _T_contra | None = None) -> CapsuleType: ... +@type_check_only +class _CanWriteErr(Protocol): + def write(self, err: str, flag: int, /) -> _Ignored: ... + @type_check_only class _HasDoc(Protocol): __doc__: str | None @@ -218,6 +326,15 @@ class _KwargsDCL(TypedDict, total=False): copy: _Copy | None like: _SupportsArrayFunc | None +@type_check_only +class _ExtObjDict(TypedDict): + divide: _ExtObjValue + over: _ExtObjValue + under: _ExtObjValue + invalid: _ExtObjValue + call: Callable[[str, int], _Ignored] | _CanWriteErr | None + bufsize: int + ### __version__: Final[str] = ... @@ -267,20 +384,39 @@ NAN: Final[float] = ... error: Final = Exception tracemalloc_domain: Final[int] = ... +_extobj_contextvar: _contextvars.ContextVar[CapsuleType] __cpu_baseline__: Final[list[str]] = ... __cpu_dispatch__: Final[list[str]] = ... __cpu_features__: Final[dict[str, bool]] = ... __cpu_targets_info__: Final[dict[str, dict[str, dict[str, str]]]] = ... -_flagdict: Final[dict[str, int]] = ... typeinfo: Final[dict[str, np.dtype[Any]]] = ... +_flagdict: Final[dict[str, int]] = ... e: Final[float] = ... euler_gamma: Final[float] = ... pi: Final[float] = ... -# +clip: np.ufunc +count: np.ufunc +endswith: np.ufunc +find: np.ufunc +index: np.ufunc +isalnum: np.ufunc +isalpha: np.ufunc +isdecimal: np.ufunc +isdigit: np.ufunc +islower: np.ufunc +isnumeric: np.ufunc +isspace: np.ufunc +istitle: np.ufunc +isupper: np.ufunc +rfind: np.ufunc +rindex: np.ufunc +startswith: np.ufunc +str_len: np.ufunc + _arg: np.ufunc _center: np.ufunc _expandtabs: np.ufunc @@ -300,25 +436,6 @@ _rstrip_whitespace: np.ufunc _strip_chars: np.ufunc _strip_whitespace: np.ufunc _zfill: np.ufunc -# -clip: np.ufunc -count: np.ufunc -endswith: np.ufunc -find: np.ufunc -index: np.ufunc -isalnum: np.ufunc -isalpha: np.ufunc -isdecimal: np.ufunc -isdigit: np.ufunc -islower: np.ufunc -isnumeric: np.ufunc -isspace: np.ufunc -istitle: np.ufunc -isupper: np.ufunc -rfind: np.ufunc -rindex: np.ufunc -startswith: np.ufunc -str_len: np.ufunc @final class flagsobj: @@ -1596,7 +1713,6 @@ def c_einsum( ### -# @overload def scalar(dtype: np.dtype[np.object_], obj: object) -> Any: ... @overload @@ -1606,7 +1722,8 @@ def scalar(dtype: np.dtype[_SafeScalarT]) -> _SafeScalarT: ... @overload def scalar(dtype: np.dtype[_SafeScalarT], obj: bytes) -> _SafeScalarT: ... -# +### + @overload def compare_chararrays( a1: _ArrayLikeStr_co, @@ -1622,29 +1739,20 @@ def compare_chararrays( rstrip: bool, ) -> _Array[np.bool]: ... -# +### + def add_docstring(obj: Callable[..., object] | _HasDoc, docstring: str, /) -> None: ... # +@deprecated("_add_newdoc_ufunc is deprecated. Use `ufunc.__doc__ = newdoc` instead.") +def _add_newdoc_ufunc(ufunc: np.ufunc, new_docstring: str, /) -> None: ... + +### + def dragon4_positional(*args: Incomplete, **kwargs: Incomplete) -> Incomplete: ... def dragon4_scientific(*args: Incomplete, **kwargs: Incomplete) -> Incomplete: ... def format_longfloat(*args: Incomplete, **kwargs: Incomplete) -> Incomplete: ... -# -def _get_madvise_hugepage() -> bool: ... -def _set_madvise_hugepage(enabled: bool, /) -> bool: ... -def _get_ndarray_c_version() -> int: ... - -# -def _monotonicity(x: _ArrayLikeFloat_co) -> L[0, 1]: ... -def _place(input: npt.ArrayLike, mask: _ArrayLikeBool_co, vals: npt.ArrayLike) -> None: ... -def _reconstruct( - subtype: type[_Array], - shape: _AnyShapeT, - dtype: _DTypeT, -) -> np.ndarray[_AnyShapeT, _DTypeT]: ... -def _vec_string(a: _ArrayLikeAnyString_co, dtype: npt.DTypeLike, attr: str, /) -> _Array: ... - ### # NOTE: We can't use e.g. `Concatenate[Any, ...]`, as that causes mypy to reject every function... @@ -1684,3 +1792,23 @@ def frompyfunc( def frompyfunc(f: Callable[..., Any], /, nin: SupportsIndex, nout: SupportsIndex, *, identity: object = None) -> np.ufunc: ... ### + +# +def _get_madvise_hugepage() -> bool: ... +def _set_madvise_hugepage(enabled: bool, /) -> bool: ... +def _get_ndarray_c_version() -> int: ... + +# + +def _get_extobj_dict() -> _ExtObjDict: ... +def _make_extobj() -> CapsuleType: ... + +# +def _monotonicity(x: _ArrayLikeFloat_co) -> L[0, 1]: ... +def _place(input: npt.ArrayLike, mask: _ArrayLikeBool_co, vals: npt.ArrayLike) -> None: ... +def _reconstruct( + subtype: type[_Array], + shape: _AnyShapeT, + dtype: _DTypeT, +) -> np.ndarray[_AnyShapeT, _DTypeT]: ... +def _vec_string(a: _ArrayLikeAnyString_co, dtype: npt.DTypeLike, attr: str, /) -> _Array: ... diff --git a/src/numpy-stubs/_core/umath.pyi b/src/numpy-stubs/_core/umath.pyi new file mode 100644 index 00000000..f29b54d9 --- /dev/null +++ b/src/numpy-stubs/_core/umath.pyi @@ -0,0 +1,221 @@ +from . import _multiarray_umath as _multiarray_umath +from ._multiarray_umath import ( + _UFUNC_API as _UFUNC_API, + _add_newdoc_ufunc as _add_newdoc_ufunc, + _center as _center, + _expandtabs as _expandtabs, + _expandtabs_length as _expandtabs_length, + _extobj_contextvar as _extobj_contextvar, + _get_extobj_dict as _get_extobj_dict, + _ljust as _ljust, + _lstrip_chars as _lstrip_chars, + _lstrip_whitespace as _lstrip_whitespace, + _make_extobj as _make_extobj, + _ones_like as _ones_like, + _partition as _partition, + _partition_index as _partition_index, + _replace as _replace, + _rjust as _rjust, + _rpartition as _rpartition, + _rpartition_index as _rpartition_index, + _rstrip_chars as _rstrip_chars, + _rstrip_whitespace as _rstrip_whitespace, + _strip_chars as _strip_chars, + _strip_whitespace as _strip_whitespace, + _zfill as _zfill, + absolute, + add, + arccos, + arccosh, + arcsin, + arcsinh, + arctan, + arctan2, + arctanh, + bitwise_and, + bitwise_count, + bitwise_or, + bitwise_xor, + cbrt, + ceil, + conj, + conjugate, + copysign, + cos, + cosh, + deg2rad, + degrees, + divide, + divmod, + e, + equal, + euler_gamma, + exp, + exp2, + expm1, + fabs, + float_power, + floor, + floor_divide, + fmax, + fmin, + fmod, + frexp, + frompyfunc, + gcd, + greater, + greater_equal, + heaviside, + hypot, + invert, + isfinite, + isinf, + isnan, + isnat, + lcm, + ldexp, + left_shift, + less, + less_equal, + log, + log1p, + log2, + log10, + logaddexp, + logaddexp2, + logical_and, + logical_not, + logical_or, + logical_xor, + matvec, + maximum, + minimum, + mod, + modf, + multiply, + negative, + nextafter, + not_equal, + pi, + positive, + power, + rad2deg, + radians, + reciprocal, + remainder, + right_shift, + rint, + sign, + signbit, + sin, + sinh, + spacing, + sqrt, + square, + subtract, + tan, + tanh, + true_divide, + trunc, + vecdot, + vecmat, +) + +__all__ = [ + "absolute", + "add", + "arccos", + "arccosh", + "arcsin", + "arcsinh", + "arctan", + "arctan2", + "arctanh", + "bitwise_and", + "bitwise_count", + "bitwise_or", + "bitwise_xor", + "cbrt", + "ceil", + "conj", + "conjugate", + "copysign", + "cos", + "cosh", + "deg2rad", + "degrees", + "divide", + "divmod", + "e", + "equal", + "euler_gamma", + "exp", + "exp2", + "expm1", + "fabs", + "float_power", + "floor", + "floor_divide", + "fmax", + "fmin", + "fmod", + "frexp", + "frompyfunc", + "gcd", + "greater", + "greater_equal", + "heaviside", + "hypot", + "invert", + "isfinite", + "isinf", + "isnan", + "isnat", + "lcm", + "ldexp", + "left_shift", + "less", + "less_equal", + "log", + "log1p", + "log2", + "log10", + "logaddexp", + "logaddexp2", + "logical_and", + "logical_not", + "logical_or", + "logical_xor", + "matvec", + "maximum", + "minimum", + "mod", + "modf", + "multiply", + "negative", + "nextafter", + "not_equal", + "pi", + "positive", + "power", + "rad2deg", + "radians", + "reciprocal", + "remainder", + "right_shift", + "rint", + "sign", + "signbit", + "sin", + "sinh", + "spacing", + "sqrt", + "square", + "subtract", + "tan", + "tanh", + "true_divide", + "trunc", + "vecdot", + "vecmat", +]