Skip to content

Commit 9389862

Browse files
authored
Merge pull request numpy#28591 from MarcoGorelli/type-ma-int-float-shape
TYP: Type masked array shape, dtype, __int__, and __float__
2 parents aca10dc + f9d311a commit 9389862

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

numpy/ma/core.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ __all__ = [
190190
"zeros_like",
191191
]
192192

193+
_ShapeType = TypeVar("_ShapeType", bound=tuple[int, ...])
193194
_ShapeType_co = TypeVar("_ShapeType_co", bound=tuple[int, ...], covariant=True)
195+
_DType = TypeVar("_DType", bound=dtype[Any])
194196
_DType_co = TypeVar("_DType_co", bound=dtype[Any], covariant=True)
195197

196198
MaskType = bool
@@ -347,13 +349,13 @@ class MaskedArray(ndarray[_ShapeType_co, _DType_co]):
347349
def __getitem__(self, indx): ...
348350
def __setitem__(self, indx, value): ...
349351
@property
350-
def dtype(self): ...
352+
def dtype(self) -> _DType_co: ...
351353
@dtype.setter
352-
def dtype(self, dtype): ...
354+
def dtype(self: MaskedArray[Any, _DType], dtype: _DType, /) -> None: ...
353355
@property
354-
def shape(self): ...
356+
def shape(self) -> _ShapeType_co: ...
355357
@shape.setter
356-
def shape(self, shape): ...
358+
def shape(self: MaskedArray[_ShapeType, Any], shape: _ShapeType, /) -> None: ...
357359
def __setmask__(self, mask, copy=...): ...
358360
@property
359361
def mask(self): ...
@@ -413,8 +415,6 @@ class MaskedArray(ndarray[_ShapeType_co, _DType_co]):
413415
def __ifloordiv__(self, other): ...
414416
def __itruediv__(self, other): ...
415417
def __ipow__(self, other): ...
416-
def __float__(self): ...
417-
def __int__(self): ...
418418
@property # type: ignore[misc]
419419
def imag(self): ...
420420
get_imag: Any
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from typing import Any
2+
3+
import numpy as np
4+
import numpy.ma
5+
6+
m: np.ma.MaskedArray[tuple[int], np.dtype[np.float64]]
7+
8+
m.shape = (3, 1) # E: Incompatible types in assignment
9+
m.dtype = np.bool # E: Incompatible types in assignment
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
from typing_extensions import assert_type
3+
m: np.ma.MaskedArray[tuple[int], np.dtype[np.float64]]
4+
5+
assert_type(m.shape, tuple[int])
6+
7+
assert_type(m.dtype, np.dtype[np.float64])
8+
9+
assert_type(int(m), int)
10+
assert_type(float(m), float)

0 commit comments

Comments
 (0)