From 35368a12b4511e1a2184392242b3fc4590e6de9f Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Tue, 22 Jul 2025 17:22:06 +0100 Subject: [PATCH 1/3] TYP: Type ``MaskedArray.__deepcopy__`` and ``MaskedArray.argsort`` --- numpy/ma/core.py | 2 +- numpy/ma/core.pyi | 13 +++++++++++-- numpy/typing/tests/data/fail/ma.pyi | 2 ++ numpy/typing/tests/data/reveal/ma.pyi | 6 ++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/numpy/ma/core.py b/numpy/ma/core.py index e7d8a20f6c6c..84e029439725 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -5628,7 +5628,7 @@ def argsort(self, axis=np._NoValue, kind=None, order=None, endwith=True, is used. kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional The sorting algorithm used. - order : list, optional + order : str or list of str, optional When `a` is an array with fields defined, this argument specifies which fields to compare first, second, etc. Not all fields need be specified. diff --git a/numpy/ma/core.pyi b/numpy/ma/core.pyi index a3d05556f7ce..ffcc02b1dc97 100644 --- a/numpy/ma/core.pyi +++ b/numpy/ma/core.pyi @@ -1398,7 +1398,16 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]): @overload def round(self, /, decimals: SupportsIndex = 0, *, out: _ArrayT) -> _ArrayT: ... - def argsort(self, axis=..., kind=..., order=..., endwith=..., fill_value=..., *, stable=...): ... + def argsort( + self, + axis: SupportsIndex | _NoValueType = ..., + kind: _SortKind | None = None, + order: str | Sequence[str] | None = None, + endwith: bool = True, + fill_value: _ScalarLike_co | None = None, + *, + stable: bool | None = None, + ) -> _MaskedArray[Any]: ... # Keep in-sync with np.ma.argmin @overload # type: ignore[override] @@ -1703,7 +1712,7 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]): # def __reduce__(self): ... - def __deepcopy__(self, memo=...): ... + def __deepcopy__(self, memo: dict[int, Any] | None = None) -> Self: ... # Keep `dtype` at the bottom to avoid name conflicts with `np.dtype` @property diff --git a/numpy/typing/tests/data/fail/ma.pyi b/numpy/typing/tests/data/fail/ma.pyi index 5dc6706ebf81..160d30f885e3 100644 --- a/numpy/typing/tests/data/fail/ma.pyi +++ b/numpy/typing/tests/data/fail/ma.pyi @@ -141,3 +141,5 @@ MAR_c //= 2 # type: ignore[misc] MAR_td64 **= 2 # type: ignore[misc] MAR_1d_f8.swapaxes(axis1=1, axis2=0) # type: ignore[call-arg] + +MAR_1d_f8.argsort(axis=(1,0)) # type: ignore[arg-type] diff --git a/numpy/typing/tests/data/reveal/ma.pyi b/numpy/typing/tests/data/reveal/ma.pyi index 1d44c3f8ba3c..ced456005691 100644 --- a/numpy/typing/tests/data/reveal/ma.pyi +++ b/numpy/typing/tests/data/reveal/ma.pyi @@ -405,6 +405,12 @@ assert_type(MAR_f8.cumprod(out=MAR_subclass), MaskedArraySubclass) assert_type(MAR_f8.cumsum(), MaskedArray[Any]) assert_type(MAR_f8.cumsum(out=MAR_subclass), MaskedArraySubclass) +assert_type(MAR_f8.__deepcopy__(), MaskedArray[np.float64]) + +assert_type(MAR_f8.argsort(), MaskedArray[Any]) +assert_type(MAR_f8.argsort(axis=0, kind='heap', order=('x', 'y')), MaskedArray[Any]) +assert_type(MAR_f8.argsort(endwith=True, fill_value=1.5, stable=False), MaskedArray[Any]) + def invalid_resize() -> None: assert_type(MAR_f8.resize((1,1)), NoReturn) # type: ignore[arg-type] From 913a1bb922dace3e1b879a02aa2f0cb9933404c9 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Wed, 23 Jul 2025 10:48:27 +0100 Subject: [PATCH 2/3] correct stable default --- numpy/ma/core.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy/ma/core.pyi b/numpy/ma/core.pyi index ffcc02b1dc97..139c8e5a2a7f 100644 --- a/numpy/ma/core.pyi +++ b/numpy/ma/core.pyi @@ -1406,7 +1406,7 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]): endwith: bool = True, fill_value: _ScalarLike_co | None = None, *, - stable: bool | None = None, + stable: bool = False, ) -> _MaskedArray[Any]: ... # Keep in-sync with np.ma.argmin From 03721be918e29a2e022782f7909c50fea3a40ebc Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Wed, 23 Jul 2025 13:21:19 +0100 Subject: [PATCH 3/3] the intportance of being precise --- numpy/__init__.pyi | 2 +- numpy/ma/core.pyi | 2 +- numpy/typing/tests/data/reveal/ma.pyi | 6 +++--- numpy/typing/tests/data/reveal/ndarray_misc.pyi | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/numpy/__init__.pyi b/numpy/__init__.pyi index d8c57c87cbbe..a196c000b6bc 100644 --- a/numpy/__init__.pyi +++ b/numpy/__init__.pyi @@ -1721,7 +1721,7 @@ class _ArrayOrScalarCommon: order: str | Sequence[str] | None = ..., *, stable: bool | None = ..., - ) -> NDArray[Any]: ... + ) -> NDArray[intp]: ... @overload # axis=None (default), out=None (default), keepdims=False (default) def argmax(self, /, axis: None = None, out: None = None, *, keepdims: L[False] = False) -> intp: ... diff --git a/numpy/ma/core.pyi b/numpy/ma/core.pyi index 139c8e5a2a7f..a4bfeb3ffab7 100644 --- a/numpy/ma/core.pyi +++ b/numpy/ma/core.pyi @@ -1407,7 +1407,7 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]): fill_value: _ScalarLike_co | None = None, *, stable: bool = False, - ) -> _MaskedArray[Any]: ... + ) -> _MaskedArray[intp]: ... # Keep in-sync with np.ma.argmin @overload # type: ignore[override] diff --git a/numpy/typing/tests/data/reveal/ma.pyi b/numpy/typing/tests/data/reveal/ma.pyi index ced456005691..9aae83eda366 100644 --- a/numpy/typing/tests/data/reveal/ma.pyi +++ b/numpy/typing/tests/data/reveal/ma.pyi @@ -407,9 +407,9 @@ assert_type(MAR_f8.cumsum(out=MAR_subclass), MaskedArraySubclass) assert_type(MAR_f8.__deepcopy__(), MaskedArray[np.float64]) -assert_type(MAR_f8.argsort(), MaskedArray[Any]) -assert_type(MAR_f8.argsort(axis=0, kind='heap', order=('x', 'y')), MaskedArray[Any]) -assert_type(MAR_f8.argsort(endwith=True, fill_value=1.5, stable=False), MaskedArray[Any]) +assert_type(MAR_f8.argsort(), MaskedArray[np.intp]) +assert_type(MAR_f8.argsort(axis=0, kind='heap', order=('x', 'y')), MaskedArray[np.intp]) +assert_type(MAR_f8.argsort(endwith=True, fill_value=1.5, stable=False), MaskedArray[np.intp]) def invalid_resize() -> None: assert_type(MAR_f8.resize((1,1)), NoReturn) # type: ignore[arg-type] diff --git a/numpy/typing/tests/data/reveal/ndarray_misc.pyi b/numpy/typing/tests/data/reveal/ndarray_misc.pyi index 7f0a214f8f52..b219f4e5bec2 100644 --- a/numpy/typing/tests/data/reveal/ndarray_misc.pyi +++ b/numpy/typing/tests/data/reveal/ndarray_misc.pyi @@ -70,8 +70,8 @@ assert_type(AR_f8.argmin(), np.intp) assert_type(AR_f8.argmin(axis=0), Any) assert_type(AR_f8.argmin(out=AR_i8), npt.NDArray[np.intp]) -assert_type(f8.argsort(), npt.NDArray[Any]) -assert_type(AR_f8.argsort(), npt.NDArray[Any]) +assert_type(f8.argsort(), npt.NDArray[np.intp]) +assert_type(AR_f8.argsort(), npt.NDArray[np.intp]) assert_type(f8.astype(np.int64).choose([()]), npt.NDArray[Any]) assert_type(AR_f8.choose([0]), npt.NDArray[Any])
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: