8000 ✨ dtype typar default for `ndarray` by jorenham · Pull Request #555 · numpy/numtype · GitHub
[go: up one dir, main page]

Skip to content

✨ dtype typar default for ndarray #555

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 2 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/numpy-stubs/@test/runtime/legacy/literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
CF = frozenset({None, "C", "F"})

order_list: list[tuple[frozenset[str | None], Callable[..., Any]]] = [
(KACF, partial(np.ndarray, 1)),
(KACF, partial(np.ndarray.__call__, 1)),
(KACF, AR.tobytes),
(KACF, partial(AR.astype, int)),
(KACF, AR.copy),
Expand Down
81 changes: 46 additions & 35 deletions src/numpy-stubs/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ _RealT_co = TypeVar("_RealT_co", covariant=True)
_ImagT_co = TypeVar("_ImagT_co", covariant=True)

_DTypeT = TypeVar("_DTypeT", bound=dtype)
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, covariant=True)
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype, default=dtype, covariant=True)
_FlexDTypeT = TypeVar("_FlexDTypeT", bound=dtype[flexible])

_ArrayT = TypeVar("_ArrayT", bound=_nt.Array)
Expand All @@ -628,6 +628,7 @@ _NumericArrayT = TypeVar("_NumericArrayT", bound=_nt.Array[number | timedelta64

_ShapeT = TypeVar("_ShapeT", bound=_nt.Shape)
_ShapeT_co = TypeVar("_ShapeT_co", bound=_nt.Shape, covariant=True)
_ShapeT0_co = TypeVar("_ShapeT0_co", bound=_nt.Shape, default=_nt.Shape, covariant=True)
_Shape1NDT = TypeVar("_Shape1NDT", bound=_nt.Shape1N)

_ScalarT = TypeVar("_ScalarT", bound=generic)
Expand Down Expand Up @@ -998,9 +999,11 @@ _HasTypeWithReal: TypeAlias = _HasType[_HasReal[_T]]
_HasTypeWithImag: TypeAlias = _HasType[_HasImag[_T]]

@type_check_only
class _HasDType(Protocol[_T_co]):
class _HasDType(Protocol[_T_co, _ShapeT0_co]):
@property
def dtype(self, /) -> _T_co: ...
@property
def shape(self, /) -> _ShapeT0_co: ...

_HasDTypeWithItem: TypeAlias = _HasDType[_HasTypeWithItem[_T]]
_HasDTypeWithReal: TypeAlias = _HasDType[_HasTypeWithReal[_T]]
Expand Down Expand Up @@ -2117,16 +2120,16 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
def __ge__(self, other: _ArrayLikeObject_co, /) -> _nt.Array[bool_]: ...

#
def __abs__(self: _HasDType[_HasType[SupportsAbs[_ScalarT]]], /) -> _nt.Array[_ScalarT, _ShapeT_co]: ...
def __abs__(self: _HasDType[_HasType[SupportsAbs[_ScalarT]], _ShapeT], /) -> _nt.Array[_ScalarT, _ShapeT]: ...
def __neg__(self: _NumericArrayT, /) -> _NumericArrayT: ... # noqa: PYI019
def __pos__(self: _NumericArrayT, /) -> _NumericArrayT: ... # noqa: PYI019
def __invert__(self: _IntegralArrayT, /) -> _IntegralArrayT: ... # noqa: PYI019

#
@overload
def __add__(self: _nt.Array[_ScalarT], x: _nt.Casts[_ScalarT], /) -> _nt.Array[_ScalarT]: ... # type: ignore[overload-overlap]
def __add__(self: _nt.Array[_ScalarT], x: _nt.Casts[_ScalarT], /) -> _nt.Array[_ScalarT]: ...
@overload
def __add__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ... # type: ignore[overload-overlap]
def __add__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ...
@overload
def __add__(self: _nt.CastsWithBuiltin[_T, _ScalarT], x: _nt.SequenceND[_T], /) -> _nt.Array[_ScalarT]: ...
@overload
Expand All @@ -2138,21 +2141,21 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload
def __add__(self: _nt.Array[datetime64], x: _nt.CoTimeDelta_nd, /) -> _nt.Array[datetime64]: ...
@overload
def __add__(self: _nt.Array[_nt.co_timedelta], x: _nt.ToDateTime_nd, /) -> _nt.Array[datetime64]: ...
def __add__(self: _nt.Array[_nt.co_timedelta, Any], x: _nt.ToDateTime_nd, /) -> _nt.Array[datetime64]: ...
@overload
def __add__(self: _nt.Array[object_], x: object, /) -> _nt.Array[object_]: ... # type: ignore[overload-cannot-match]
def __add__(self: _nt.Array[object_, Any], x: object, /) -> _nt.Array[object_]: ...
@overload
def __add__(self: _nt.Array[str_], x: _nt.ToString_nd[_T], /) -> _nt.StringArrayND[_T]: ...
def __add__(self: _nt.Array[str_, Any], x: _nt.ToString_nd[_T], /) -> _nt.StringArrayND[_T]: ...
@overload
def __add__(self: _nt.StringArrayND[_T], x: _nt.ToString_nd[_T] | _nt.ToStr_nd, /) -> _nt.StringArrayND[_T]: ...
@overload
def __add__(self: _nt.Array[generic[_T]], x: _nt.Sequence1ND[_nt.op.CanRAdd[_T]], /) -> _nt.Array[Incomplete]: ...

#
@overload
def __radd__(self: _nt.Array[_ScalarT], x: _nt.Casts[_ScalarT], /) -> _nt.Array[_ScalarT]: ... # type: ignore[overload-overlap]
def __radd__(self: _nt.Array[_ScalarT], x: _nt.Casts[_ScalarT], /) -> _nt.Array[_ScalarT]: ...
@overload
def __radd__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ... # type: ignore[overload-overlap]
def __radd__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ...
@overload
def __radd__(self: _nt.CastsWithBuiltin[_T, _ScalarT], x: _nt.SequenceND[_T], /) -> _nt.Array[_ScalarT]: ...
@overload
Expand All @@ -2164,11 +2167,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload
def __radd__(self: _nt.Array[datetime64], x: _nt.CoTimeDelta_nd, /) -> _nt.Array[datetime64]: ...
@overload
def __radd__(self: _nt.Array[_nt.co_timedelta], x: _nt.ToDateTime_nd, /) -> _nt.Array[datetime64]: ...
def __radd__(self: _nt.Array[_nt.co_timedelta, Any], x: _nt.ToDateTime_nd, /) -> _nt.Array[datetime64]: ...
@overload
def __radd__(self: _nt.Array[object_], x: object, /) -> _nt.Array[object_]: ... # type: ignore[overload-cannot-match]
def __radd__(self: _nt.Array[object_, Any], x: object, /) -> _nt.Array[object_]: ...
@overload
def __radd__(self: _nt.Array[str_], x: _nt.ToString_nd[_T], /) -> _nt.StringArrayND[_T]: ...
def __radd__(self: _nt.Array[str_, Any], x: _nt.ToString_nd[_T], /) -> _nt.StringArrayND[_T]: ...
@overload
def __radd__(self: _nt.StringArrayND[_T], x: _nt.ToString_nd[_T] | _nt.ToStr_nd, /) -> _nt.StringArrayND[_T]: ...
@overload
Expand Down Expand Up @@ -2268,7 +2271,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload
def __mul__(self: _nt.Array[_CoNumberT], x: _nt.Casts[_CoNumberT], /) -> _nt.Array[_CoNumberT]: ...
@overload
def __mul__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ... # type: ignore[overload-overlap]
def __mul__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ...
@overload
def __mul__(self: _nt.CastsWithBuiltin[_T, _ScalarT], x: _nt.SequenceND[_T], /) -> _nt.Array[_ScalarT]: ...
@overload
Expand All @@ -2278,11 +2281,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload
def __mul__(self: _nt.CastsWithComplex[_ScalarT], x: _PyComplexND, /) -> _nt.Array[_ScalarT]: ...
@overload
def __mul__(self: _nt.Array[timedelta64], x: _nt.ToFloating_nd, /) -> _nt.Array[timedelta64]: ...
def __mul__(self: _nt.Array[timedelta64, Any], x: _nt.ToFloating_nd, /) -> _nt.Array[timedelta64]: ...
@overload
def __mul__(self: _nt.Array[object_], x: object, /) -> _nt.Array[object_]: ... # type: ignore[overload-cannot-match]
def __mul__(self: _nt.Array[object_, Any], x: object, /) -> _nt.Array[object_]: ...
@overload
def __mul__(self: _nt.Array[integer], x: _nt.ToString_nd, /) -> _nt.StringArrayND[_T]: ...
def __mul__(self: _nt.Array[integer, Any], x: _nt.ToString_nd, /) -> _nt.StringArrayND[_T]: ...
@overload
def __mul__(self: _nt.StringArrayND[_T], x: _nt.ToInteger_nd, /) -> _nt.StringArrayND[_T]: ...
@overload
Expand All @@ -2292,7 +2295,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload
def __rmul__(self: _nt.Array[_CoNumberT], x: _nt.Casts[_CoNumberT], /) -> _nt.Array[_CoNumberT]: ...
@overload
def __rmul__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ... # type: ignore[overload-overlap]
def __rmul__(self: _nt.Array[_SelfScalarT], x: _nt.CastsWith[_SelfScalarT, _ScalarT], /) -> _nt.Array[_ScalarT]: ...
@overload
def __rmul__(self: _nt.CastsWithBuiltin[_T, _ScalarT], x: _nt.SequenceND[_T], /) -> _nt.Array[_ScalarT]: ...
@overload
Expand All @@ -2302,11 +2305,11 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload
def __rmul__(self: _nt.CastsWithComplex[_ScalarT], x: _PyComplexND, /) -> _nt.Array[_ScalarT]: ...
@overload
def __rmul__(self: _nt.Array[timedelta64], x: _nt.ToFloating_nd, /) -> _nt.Array[timedelta64]: ...
def __rmul__(self: _nt.Array[timedelta64, Any], x: _nt.ToFloating_nd, /) -> _nt.Array[timedelta64]: ...
@overload
def __rmul__(self: _nt.Array[object_], x: object, /) -> _nt.Array[object_]: ... # type: ignore[overload-cannot-match]
def __rmul__(self: _nt.Array[object_, Any], x: object, /) -> _nt.Array[object_]: ...
@overload
def __rmul__(self: _nt.Array[integer], x: _nt.ToString_nd, /) -> _nt.StringArrayND[_T]: ...
def __rmul__(self: _nt.Array[integer, Any], x: _nt.ToString_nd, /) -> _nt.StringArrayND[_T]: ...
@overload
def __rmul__(self: _nt.StringArrayND[_T], x: _nt.ToInteger_nd, /) -> _nt.StringArrayND[_T]: ...
@overload
Expand Down Expand Up @@ -2666,47 +2669,55 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):

#
@overload
def __divmod__(self: _nt.Array[bool_], x: _nt.ToBool_nd, /) -> _2Tuple[_nt.Array[int8]]: ...
def __divmod__(self: _nt.Array[bool_, _nt.Shape], x: _nt.ToBool_nd, /) -> _2Tuple[_nt.Array[int8]]: ...
@overload
def __divmod__(
self: _nt.Array[_RealNumberT], x: _nt.Casts[_RealNumberT] | _nt.ToBool_nd, /
self: _nt.Array[_RealNumberT, _nt.Shape], x: _nt.Casts[_RealNumberT, _nt.Shape] | _nt.ToBool_nd, /
) -> _2Tuple[_nt.Array[_RealNumberT]]: ...
@overload
def __divmod__(
self: _nt.Array[_CoFloatingT], x: _nt.CastsWith[_CoFloatingT, _RealScalarT], /
self: _nt.Array[_CoFloatingT, _nt.Shape], x: _nt.CastsWith[_CoFloatingT, _RealScalarT, _nt.Shape], /
) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
@overload
def __divmod__(self: _nt.CastsWithInt[_RealScalarT], x: _PyIntND, /) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
def __divmod__(
self: _nt.CastsWithInt[_RealScalarT, _nt.Shape], x: _PyIntND, /
) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
@overload
def __divmod__(self: _nt.CastsWithFloat[_RealScalarT], x: _PyFloatND, /) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
def __divmod__(
self: _nt.CastsWithFloat[_RealScalarT, _nt.Shape], x: _PyFloatND, /
) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
@overload
def __divmod__(
self: _nt.Array[timedelta64], x: _nt.ToTimeDelta_nd, /
self: _nt.Array[timedelta64, _nt.Shape], x: _nt.ToTimeDelta_nd, /
) -> tuple[_nt.Array[int64], _nt.Array[timedelta64]]: ...
@overload
def __divmod__(self: _nt.Array[object_], x: object, /) -> _2Tuple[_nt.Array[object_]]: ...
def __divmod__(self: _nt.Array[object_, _nt.Shape], x: object, /) -> _2Tuple[_nt.Array[object_]]: ...

#
@overload
def __rdivmod__(self: _nt.Array[bool_], x: _nt.ToBool_nd, /) -> _2Tuple[_nt.Array[int8]]: ...
def __rdivmod__(self: _nt.Array[bool_, _nt.Shape], x: _nt.ToBool_nd, /) -> _2Tuple[_nt.Array[int8]]: ...
@overload
def __rdivmod__(
self: _nt.Array[_RealNumberT], x: _nt.Casts[_RealNumberT] | _nt.ToBool_nd, /
self: _nt.Array[_RealNumberT, _nt.Shape], x: _nt.Casts[_RealNumberT, _nt.Shape] | _nt.ToBool_nd, /
) -> _2Tuple[_nt.Array[_RealNumberT]]: ...
@overload
def __rdivmod__(
self: _nt.Array[_CoFloatingT], x: _nt.CastsWith[_CoFloatingT, _RealScalarT], /
self: _nt.Array[_CoFloatingT, _nt.Shape], x: _nt.CastsWith[_CoFloatingT, _RealScalarT, _nt.Shape], /
) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
@overload
def __rdivmod__(self: _nt.CastsWithInt[_RealScalarT], x: _PyIntND, /) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
def __rdivmod__(
self: _nt.CastsWithInt[_RealScalarT, _nt.Shape], x: _PyIntND, /
) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
@overload
def __rdivmod__(self: _nt.CastsWithFloat[_RealScalarT], x: _PyFloatND, /) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
def __rdivmod__(
self: _nt.CastsWithFloat[_RealScalarT, _nt.Shape], x: _PyFloatND, /
) -> _2Tuple[_nt.Array[_RealScalarT]]: ...
@overload
def __rdivmod__(
self: _nt.Array[timedelta64], x: _nt.ToTimeDelta_nd, /
self: _nt.Array[timedelta64, _nt.Shape], x: _nt.ToTimeDelta_nd, /
) -> tuple[_nt.Array[int64], _nt.Array[timedelta64]]: ...
@overload
def __rdivmod__(self: _nt.Array[object_], x: object, /) -> _2Tuple[_nt.Array[object_]]: ...
def __rdivmod__(self: _nt.Array[object_, _nt.Shape], x: object, /) -> _2Tuple[_nt.Array[object_]]: ...

#
@overload
Expand Down
0