8000 TYP: Type ``MaskedArray.{imag, real, baseclass, mT}`` by MarcoGorelli · Pull Request #28868 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

TYP: Type MaskedArray.{imag, real, baseclass, mT} #28868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions numpy/ma/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ from collections.abc import Sequence
from typing import Any, Literal, SupportsIndex, TypeAlias, TypeVar, overload

from _typeshed import Incomplete
from typing_extensions import TypeIs, deprecated
from typing_extensions import Self, TypeIs, deprecated

import numpy as np
from numpy import (
_HasDTypeWithRealAndImag,
_ModeKind,
_OrderKACF,
_PartitionKind,
Expand Down Expand Up @@ -401,10 +402,6 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
def __getitem__(self, indx): ...
def __setitem__(self, indx, value): ...
@property
def dtype(self) -> _DTypeT_co: ...
@dtype.setter
def dtype(self: MaskedArray[Any, _DTypeT], dtype: _DTypeT, /) -> None: ...
@property
def shape(self) -> _ShapeT_co: ...
@shape.setter
def shape(self: MaskedArray[_ShapeT, Any], shape: _ShapeT, /) -> None: ...
Expand All @@ -426,7 +423,7 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
def sharedmask(self): ...
def shrink_mask(self): ...
@property
def baseclass(self): ...
def baseclass(self) -> type[NDArray[Any]]: ...
data: Any
@property
def flat(self): ...
Expand Down Expand Up @@ -468,10 +465,10 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
def __itruediv__(self, other): ...
def __ipow__(self, other): ...
@property # type: ignore[misc]
def imag(self): ...
def imag(self: _HasDTypeWithRealAndImag[object, _ScalarT], /) -> MaskedArray[_ShapeT_co, dtype[_ScalarT]]: ...
get_imag: Any
@property # type: ignore[misc]
def real(self): ...
def real(self: _HasDTypeWithRealAndImag[_ScalarT, object], /) -> MaskedArray[_ShapeT_co, dtype[_ScalarT]]: ...
get_real: Any

# keep in sync with `np.ma.count`
Expand Down Expand Up @@ -798,7 +795,7 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
transpose: Any

@property # type: ignore[misc]
def mT(self): ...
def mT(self) -> Self: ...

#
def toflex(self) -> Incomplete: ...
Expand All @@ -813,6 +810,12 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
def __reduce__(self): ...
def __deepcopy__(self, memo=...): ...

# Keep `dtype` at the bottom to avoid name conflicts with `np.dtype`
@property
def dtype(self) -> _DTypeT_co: ...
@dtype.setter
def dtype(self: MaskedArray[Any, _DTypeT], dtype: _DTypeT, /) -> None: ...

class mvoid(MaskedArray[_ShapeT_co, _DTypeT_co]):
def __new__(
self, # pyright: ignore[reportSelfClsParameterName]
Expand Down
8 changes: 8 additions & 0 deletions numpy/typing/tests/data/reveal/ma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ AR_dt64: NDArray[np.datetime64]
AR_td64: NDArray[np.timedelta64]
AR_o: NDArray[np.timedelta64]

MAR_c16: MaskedArray[np.complex128]
MAR_b: MaskedArray[np.bool]
MAR_f4: MaskedArray[np.float32]
MAR_f8: MaskedArray[np.float64]
Expand Down Expand Up @@ -308,6 +309,13 @@ def func(x: object) -> None:
else:
assert_type(x, object)

assert_type(MAR_2d_f4.mT, np.ma.MaskedArray[tuple[int, int], np.dtype[np.float32]])

assert_type(MAR_c16.real, MaskedArray[np.float64])
assert_type(MAR_c16.imag, MaskedArray[np.float64])

assert_type(MAR_2d_f4.baseclass, type[NDArray[Any]])

assert_type(np.ma.nomask, np.bool[Literal[False]])
# https://github.com/python/mypy/issues/18974
assert_type(np.ma.MaskType, type[np.bool]) # type: ignore[assert-type]
Loading
0