Skip to content

Commit 1923b38

Browse files
authored
TYP: Type np.ma.getmask (numpy#28832)
1 parent 94b232f commit 1923b38

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

numpy/ma/core.pyi

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,14 @@ fmod: _DomainedBinaryOperation
337337
mod: _DomainedBinaryOperation
338338

339339
def make_mask_descr(ndtype): ...
340-
def getmask(a): ...
340+
341+
@overload
342+
def getmask(a: _ScalarLike_co) -> bool_: ...
343+
@overload
344+
def getmask(a: MaskedArray[_ShapeT_co, Any]) -> np.ndarray[_ShapeT_co, dtype[bool_]] | bool_: ...
345+
@overload
346+
def getmask(a: ArrayLike) -> NDArray[bool_] | bool_: ...
347+
341348
get_mask = getmask
342349

343350
def getmaskarray(arr): ...

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ MAR_V: MaskedNDArray[np.void]
3030
MAR_subclass: MaskedNDArraySubclass
3131

3232
MAR_1d: np.ma.MaskedArray[tuple[int], np.dtype]
33+
MAR_2d_f4: np.ma.MaskedArray[tuple[int, int], np.dtype[np.float32]]
3334

3435
b: np.bool
3536
f4: np.float32
@@ -282,6 +283,14 @@ assert_type(np.ma.allclose(AR_f4, MAR_f4), bool)
282283
assert_type(np.ma.allclose(AR_f4, MAR_f4, masked_equal=False), bool)
283284
assert_type(np.ma.allclose(AR_f4, MAR_f4, rtol=.4, atol=.3), bool)
284285

286+
assert_type(np.ma.getmask(MAR_f4), NDArray[np.bool] | np.bool)
287+
# PyRight detects this one correctly, but mypy doesn't:
288+
# `Revealed type is "Union[numpy.ndarray[Any, Any], numpy.bool[Any]]"`
289+
assert_type(np.ma.getmask(MAR_1d), np.ndarray[tuple[int], np.dtype[np.bool]] | np.bool) # type: ignore[assert-type]
290+
assert_type(np.ma.getmask(MAR_2d_f4), np.ndarray[tuple[int, int], np.dtype[np.bool]] | np.bool)
291+
assert_type(np.ma.getmask([1,2]), NDArray[np.bool] | np.bool)
292+
assert_type(np.ma.getmask(np.int64(1)), np.bool)
293+
285294
assert_type(np.ma.is_mask(MAR_1d), bool)
286295
assert_type(np.ma.is_mask(AR_b), bool)
287296

0 commit comments

Comments
 (0)