8000 TYP: Type ``MaskedArray.sort`` by MarcoGorelli · Pull Request #28664 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Type MaskedArray.sort #28664

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 5 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup return type when input is not maskedarray
  • Loading branch information
MarcoGorelli committed Apr 7, 2025
commit c8877959a91c4e0dbdafd1d07ac5170705c1c89f
32 changes: 24 additions & 8 deletions numpy/ma/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ from numpy import (
from numpy._globals import _NoValueType
from numpy._typing import (
ArrayLike,
NDArray,
_ArrayLike,
_DTypeLikeBool,
_Shape,
_ScalarLike_co,
_ShapeLike,
)
Expand Down Expand Up @@ -216,7 +218,7 @@ _DType_co = TypeVar("_DType_co", bound=dtype[Any], covariant=True)
_ArrayType = TypeVar("_ArrayType", bound=ndarray[Any, Any])
_SCT = TypeVar("_SCT", bound=generic)
# A subset of `MaskedArray` that can be parametrized w.r.t. `np.generic`
_MaskedArray: TypeAlias = MaskedArray[Any, dtype[_SCT]]
_MaskedArray: TypeAlias = MaskedArray[_Shape, dtype[_SCT]]
_MaskedArrayType = TypeVar("_MaskedArrayType", bound=MaskedArray[Any, Any])

MaskType = bool
Expand Down Expand Up @@ -548,14 +550,16 @@ class MaskedArray(ndarray[_ShapeType_co, _DType_co]):
#
def sort(
self,
axis: SupportsIndex | None = None,
axis: SupportsIndex = -1,
kind: _SortKind | None = None,
order: str | Sequence[str] | None = None,
endwith: bool | None = None,
endwith: bool | None = True,
fill_value: _ScalarLike_co | None = None,
*,
stable: bool | None = None,
stable: Literal[False] | None = False,
) -> None: ...

#
@overload
def min( # type: ignore[override]
self: _MaskedArray[_SCT],
Expand Down Expand Up @@ -970,16 +974,28 @@ maximum: _extrema_operation
def take(a, indices, axis=..., out=..., mode=...): ...
def power(a, b, third=...): ...
def argsort(a, axis=..., kind=..., order=..., endwith=..., fill_value=..., *, stable=...): ...
@overload
def sort(
a: _MaskedArrayType,
axis: SupportsIndex | None = None,
a: _ArrayType,
axis: SupportsIndex = -1,
kind: _SortKind | None = None,
order: str | Sequence[str] | None = None,
endwith: bool | None = None,
fill_value: _ScalarLike_co | None = None,
*,
stable: Literal[False] | None = False,
) -> _ArrayType: ...
@overload
def sort(
a: ArrayLike,
axis: SupportsIndex = -1,
kind: _SortKind | None = None,
order: str | Sequence[str] | None = None,
endwith: bool | None = None,
fill_value: _ScalarLike_co | None = None,
*,
stable: bool | None = None,
) -> _MaskedArrayType: ...
stable: Literal[False] | None = False,
) -> NDArray[Any]: ...
def compressed(x): ...
def concatenate(arrays, axis=...): ...
def diag(v, k=...): ...
Expand Down
2 changes: 2 additions & 0 deletions numpy/typing/tests/data/fail/ma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ np.ma.argmax(m, out=1.0) # E: No overload variant
np.ma.argmax(m, fill_value=lambda x: 27) # E: No overload variant

m.sort(axis=(0,1)) # E: No overload variant
m.sort(axis=None) # E: No overload variant
m.sort(kind='cabbage') # E: No overload variant
m.sort(order=lambda: 'cabbage') # E: No overload variant
m.sort(endwith='cabbage') # E: No overload variant
m.sort(fill_value=lambda: 'cabbage') # E: No overload variant
m.sort(stable='cabbage') # E: No overload variant
m.sort(stable=True) # E: No overload variant
6 changes: 5 additions & 1 deletion numpy/typing/tests/data/reveal/ma.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from typing_extensions import assert_type
from typing import Any, TypeAlias, TypeVar
from numpy._typing import _Shape
from numpy._typing import _Shape, NDArray
from numpy import dtype, generic


Expand All @@ -10,6 +10,8 @@ MaskedNDArray: TypeAlias = np.ma.MaskedArray[_Shape, dtype[_ScalarType_co]]

class MaskedNDArraySubclass(MaskedNDArray[np.complex128]): ...

AR_f4: NDArray[np.float32]

MAR_b: MaskedNDArray[np.bool]
MAR_f4: MaskedNDArray[np.float32]
MAR_i8: MaskedNDArray[np.int64]
Expand Down Expand Up @@ -124,3 +126,5 @@ assert_type(MAR_f4.sort(axis=0, kind='quicksort', order='K', endwith=False, fill

assert_type(np.ma.sort(MAR_f4), MaskedNDArray[np.float32])
assert_type(np.ma.sort(MAR_subclass), MaskedNDArraySubclass)
assert_type(np.ma.sort([[0, 1], [2, 3]]), NDArray[Any])
assert_type(np.ma.sort(AR_f4), NDArray[np.float32])
Loading
0