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
Next Next commit
TYP: Type MaskedArray.sort
  • Loading branch information
MarcoGorelli committed Apr 7, 2025
commit 36ab49b5174d863a503ad8aeec607dbaa4f51561
26 changes: 23 additions & 3 deletions numpy/ma/core.pyi
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# pyright: reportIncompatibleMethodOverride=false
# ruff: noqa: ANN001, ANN002, ANN003, ANN201, ANN202 ANN204

from typing import Any, Literal, SupportsIndex, TypeVar, overload, TypeAlias
from typing import Any, Literal, SupportsIndex, TypeVar, overload, TypeAlias, Sequence

from _typeshed import Incomplete
from typing_extensions import deprecated

from numpy import (
intp,
_SortKind,
_OrderKACF,
amax,
amin,
Expand Down Expand Up @@ -216,6 +217,7 @@ _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]]
_MaskedArrayType = TypeVar("_MaskedArrayType", bound=MaskedArray[Any, Any])

MaskType = bool
nomask: bool
Expand Down Expand Up @@ -544,7 +546,16 @@ class MaskedArray(ndarray[_ShapeType_co, _DType_co]):
) -> _ArrayType: ...

#
def sort(self, axis=..., kind=..., order=..., endwith=..., fill_value=..., *, stable=...): ...
def sort(
self,
axis: SupportsIndex | None = None,
kind: _SortKind | None = None,
order: str | Sequence[str] | None = None,
endwith: bool | None = None,
fill_value: _ScalarLike_co | None = None,
*,
stable: bool | None = None,
) -> None: ...
@overload
def min( # type: ignore[override]
self: _MaskedArray[_SCT],
Expand Down Expand Up @@ -959,7 +970,16 @@ 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=...): ...
def sort(a, axis=..., kind=..., order=..., endwith=..., fill_value=..., *, stable=...): ...
def sort(
a: _MaskedArrayType,
axis: SupportsIndex | None = None,
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: ...
def compressed(x): ...
def concatenate(arrays, axis=...): ...
def diag(v, k=...): ...
Expand Down
7 changes: 7 additions & 0 deletions numpy/typing/tests/data/fail/ma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ np.ma.argmax(m, axis=(0,)) # E: No overload variant
np.ma.argmax(m, keepdims=1.0) # E: No overload variant
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(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
6 changes: 6 additions & 0 deletions numpy/typing/tests/data/reveal/ma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@ assert_type(np.ma.argmax(MAR_f4, axis=0), Any)
assert_type(np.ma.argmax(MAR_b, keepdims=True), Any)
assert_type(np.ma.argmax(MAR_f4, out=MAR_subclass), MaskedNDArraySubclass)
assert_type(np.ma.argmax(MAR_f4, None, None, out=MAR_subclass), MaskedNDArraySubclass)

assert_type(MAR_f4.sort(), None)
assert_type(MAR_f4.sort(axis=0, kind='quicksort', order='K', endwith=False, fill_value=42., stable=False), None)

assert_type(np.ma.sort(MAR_f4), MaskedNDArray[np.float32])
assert_type(np.ma.sort(MAR_subclass), MaskedNDArraySubclass)
Loading
0