Skip to content

TYP: Type MaskedArray.view #29383

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 12 commits into from
Jul 23, 2025
Merged
4 changes: 2 additions & 2 deletions numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2575,14 +2575,14 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeT_co, _DTypeT_co]):
@overload # (dtype: T)
def view(self, /, dtype: _DTypeT | _HasDType[_DTypeT]) -> ndarray[_ShapeT_co, _DTypeT]: ...
@overload # (dtype: dtype[T])
def view(self, /, dtype: _DTypeLike[_ScalarT]) -> NDArray[_ScalarT]: ...
def view(self, /, dtype: _DTypeLike[_ScalarT]) -> ndarray[_ShapeT_co, dtype[_ScalarT]]: ...
@overload # (type: T)
def view(self, /, *, type: type[_ArrayT]) -> _ArrayT: ...
@overload # (_: T)
def view(self, /, dtype: type[_ArrayT]) -> _ArrayT: ...
@overload # (dtype: ?)
def view(self, /, dtype: DTypeLike) -> ndarray[_ShapeT_co, dtype]: ...
@overload # (dtype: ?, type: type[T])
@overload # (dtype: ?, type: T)
def view(self, /, dtype: DTypeLike, type: type[_ArrayT]) -> _ArrayT: ...

def setfield(self, /, val: ArrayLike, dtype: DTypeLike, offset: SupportsIndex = 0) -> None: ...
Expand Down
21 changes: 20 additions & 1 deletion numpy/ma/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from typing_extensions import TypeIs, TypeVar

import numpy as np
from numpy import (
_HasDType,
_HasDTypeWithRealAndImag,
_ModeKind,
_OrderKACF,
Expand Down Expand Up @@ -76,6 +77,7 @@ from numpy._typing import (
_ArrayLikeString_co,
_ArrayLikeTD64_co,
_ArrayLikeUInt_co,
_DTypeLike,
_DTypeLikeBool,
_IntLike_co,
_ScalarLike_co,
Expand Down Expand Up @@ -453,7 +455,24 @@ class MaskedArray(ndarray[_ShapeT_co, _DTypeT_co]):
def __new__(cls, data=..., mask=..., dtype=..., copy=..., subok=..., ndmin=..., fill_value=..., keep_mask=..., hard_mask=..., shrink=..., order=...): ...
def __array_finalize__(self, obj): ...
def __array_wrap__(self, obj, context=..., return_scalar=...): ...
def view(self, dtype=..., type=..., fill_value=...): ...

@overload # ()
def view(self, /, dtype: None = None, type: None = None, fill_value: _ScalarLike_co | None = None) -> Self: ...
@overload # (dtype: DTypeT)
def view(self, /, dtype: _DTypeT | _HasDType[_DTypeT], type: None = None, fill_value: _ScalarLike_co | None = None) -> MaskedArray[_ShapeT_co, _DTypeT]: ...
@overload # (dtype: dtype[ScalarT])
def view(self, /, dtype: _DTypeLike[_ScalarT], type: None = None, fill_value: _ScalarLike_co | None = None) -> MaskedArray[_ShapeT_co, dtype[_ScalarT]]: ...
@overload # ([dtype: _, ]*, type: ArrayT)
def view(self, /, dtype: DTypeLike | None = None, *, type: type[_ArrayT], fill_value: _ScalarLike_co | None = None) -> _ArrayT: ...
@overload # (dtype: _, type: ArrayT)
def view(self, /, dtype: DTypeLike | None, type: type[_ArrayT], fill_value: _ScalarLike_co | None = None) -> _ArrayT: ...
@overload # (dtype: ArrayT, /)
def view(self, /, dtype: type[_ArrayT], type: None = None, fill_value: _ScalarLike_co | None = None) -> _ArrayT: ...
@overload # (dtype: ?)
def view(self, /, dtype: DTypeLike, type: None = None, fill_value: _ScalarLike_co | None = None) -> MaskedArray[_ShapeT_co, dtype]: ...
@overload # (dtype: ?, type: ArrayT)
def view(self, /, dtype: DTypeLike, type: type[_ArrayT], fill_value: _ScalarLike_co | None = None) -> _ArrayT: ...

def __getitem__(self, indx): ...
def __setitem__(self, indx, value): ...
@property
Expand Down
12 changes: 12 additions & 0 deletions numpy/typing/tests/data/reveal/ma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ assert_type(MAR_f8.cumprod(out=MAR_subclass), MaskedArraySubclass)
assert_type(MAR_f8.cumsum(), MaskedArray[Any])
assert_type(MAR_f8.cumsum(out=MAR_subclass), MaskedArraySubclass)

assert_type(MAR_f8.view(), MaskedArray[np.float64])
assert_type(MAR_f8.view(dtype=np.float32), MaskedArray[np.float32])
assert_type(MAR_f8.view(dtype=np.dtype(np.float32)), MaskedArray[np.float32])
assert_type(MAR_f8.view(dtype=np.float32, fill_value=0), MaskedArray[np.float32])
assert_type(MAR_f8.view(type=np.ndarray), np.ndarray[Any, Any])
assert_type(MAR_f8.view(None, np.ndarray), np.ndarray[Any, Any])
assert_type(MAR_f8.view(dtype=np.ndarray), np.ndarray[Any, Any])
assert_type(MAR_f8.view(dtype='float32'), MaskedArray[Any])
assert_type(MAR_f8.view(dtype='float32', type=np.ndarray), np.ndarray[Any, Any])
assert_type(MAR_2d_f4.view(dtype=np.float16), np.ma.MaskedArray[tuple[int, int], np.dtype[np.float16]])
assert_type(MAR_2d_f4.view(dtype=np.dtype(np.float16)), np.ma.MaskedArray[tuple[int, int], np.dtype[np.float16]])

def invalid_resize() -> None:
assert_type(MAR_f8.resize((1,1)), NoReturn) # type: ignore[arg-type]

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy