8000 TYP: Type ``MaskedArray.filled`` and ``np.ma.filled`` (#28742) · VascoConceicao/numpy@7c1ba93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c1ba93

Browse files
MarcoGorelliVascoConceicao
authored andcommitted
TYP: Type MaskedArray.filled and np.ma.filled (numpy#28742)
1 parent 82ef182 commit 7c1ba93

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

numpy/ma/core.pyi

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ _DTypeT = TypeVar("_DTypeT", bound=dtype[Any])
224224
_DTypeT_co = TypeVar("_DTypeT_co", bound=dtype[Any], covariant=True)
225225
_ArrayT = TypeVar("_ArrayT", bound=ndarray[Any, Any])
226226
_ScalarT = TypeVar("_ScalarT", bound=generic)
227+
_ScalarT_co = TypeVar("_ScalarT_co", bound=generic)
227228
# A subset of `MaskedArray` that can be parametrized w.r.t. `np.generic`
228229
_MaskedArray: TypeAlias = MaskedArray[_Shape, dtype[_ScalarT]]
229230

@@ -239,7 +240,12 @@ def minimum_fill_value(obj): ...
239240
def maximum_fill_value(obj): ...
240241
def set_fill_value(a, fill_value): ...
241242
def common_fill_value(a, b): ...
242-
def filled(a, fill_value=...): ...
243+
@overload
244+
def filled(a: ndarray[_ShapeT_co, _DTypeT_co], fill_value: _ScalarLike_co | None = None) -> ndarray[_ShapeT_co, _DTypeT_co]: ...
245+
@overload
246+
def filled(a: _ArrayLike[_ScalarT_co], fill_value: _ScalarLike_co | None = None) -> NDArray[_ScalarT_co]: ...
247+
@overload
248+
def filled(a: ArrayLike, fill_value: _ScalarLike_co | None = None) -> NDArray[Any]: ...
243249
def getdata(a, subok=...): ...
244250
get_data = getdata
245251

@@ -418,7 +424,7 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
418424
def fill_value(self, value=...): ...
419425
get_fill_value: Any
420426
set_fill_value: Any
421-
def filled(self, fill_value=...): ...
427+
def filled(self, /, fill_value: _ScalarLike_co | None = None) -> ndarray[_ShapeT_co, _DTypeT_co]: ...
422428
def compressed(self): ...
423429
def compress(self, condition, axis=..., out=...): ...
424430
def __eq__(self, other): ...

numpy/typing/tests/data/reveal/ma.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,13 @@ assert_type(np.ma.count(MAR_b, axis=(0,1)), NDArray[np.int_])
247247
assert_type(np.ma.count(MAR_o, keepdims=True), NDArray[np.int_])
248248
assert_type(np.ma.count(MAR_o, axis=None, keepdims=True), NDArray[np.int_])
249249
assert_type(np.ma.count(MAR_o, None, True), NDArray[np.int_])
250+
251+
assert_type(MAR_f4.filled(float('nan')), NDArray[np.float32])
252+
assert_type(MAR_i8.filled(), NDArray[np.int64])
253+
assert_type(MAR_1d.filled(), np.ndarray[tuple[int], np.dtype[Any]])
254+
255+
assert_type(np.ma.filled(MAR_f4, float('nan')), NDArray[np.float32])
256+
assert_type(np.ma.filled([[1,2,3]]), NDArray[Any])
257+
# PyRight detects this one correctly, but mypy doesn't.
258+
# https://github.com/numpy/numpy/pull/28742#discussion_r2048968375
259+
assert_type(np.ma.filled(MAR_1d), np.ndarray[tuple[int], np.dtype[Any]]) # type: ignore[assert-type]

0 commit comments

Comments
 (0)
0