Skip to content

Commit f702380

Browse files
authored
TYP: Type np.ma.is_mask (numpy#28833)
1 parent fb50a12 commit f702380

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

numpy/ma/core.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from collections.abc import Sequence
55
from typing import Any, Literal, SupportsIndex, TypeAlias, TypeVar, overload
66

77
from _typeshed import Incomplete
8-
from typing_extensions import deprecated
8+
from typing_extensions import TypeIs, deprecated
99

1010
import numpy as np
1111
from numpy import (
@@ -341,7 +341,11 @@ def getmask(a): ...
341341
get_mask = getmask
342342

343343
def getmaskarray(arr): ...
344-
def is_mask(m): ...
344+
345+
# It's sufficient for `m` to have dtype with type: `type[np.bool_]`,
346+
# which isn't necessarily a ndarray. Please open an issue if this causes issues.
347+
def is_mask(m: object) -> TypeIs[NDArray[bool_]]: ...
348+
345349
def make_mask(m, copy=..., shrink=..., dtype=...): ...
346350
def make_mask_none(newshape, dtype=...): ...
347351
def mask_or(m1, m2, copy=..., shrink=...): ...

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ MaskedNDArray: TypeAlias = np.ma.MaskedArray[_Shape, dtype[_ScalarT_co]]
1010

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

13+
AR_b: NDArray[np.bool]
1314
AR_f4: NDArray[np.float32]
1415
AR_dt64: NDArray[np.datetime64]
1516
AR_td64: NDArray[np.timedelta64]
@@ -281,6 +282,15 @@ assert_type(np.ma.allclose(AR_f4, MAR_f4), bool)
281282
assert_type(np.ma.allclose(AR_f4, MAR_f4, masked_equal=False), bool)
282283
assert_type(np.ma.allclose(AR_f4, MAR_f4, rtol=.4, atol=.3), bool)
283284

285+
assert_type(np.ma.is_mask(MAR_1d), bool)
286+
assert_type(np.ma.is_mask(AR_b), bool)
287+
288+
def func(x: object) -> None:
289+
if np.ma.is_mask(x):
290+
assert_type(x, NDArray[np.bool])
291+
else:
292+
assert_type(x, object)
293+
284294
assert_type(np.ma.nomask, np.bool[Literal[False]])
285295
# https://github.com/python/mypy/issues/18974
286296
assert_type(np.ma.MaskType, type[np.bool]) # type: ignore[assert-type]

0 commit comments

Comments
 (0)