Skip to content

Commit ca09a65

Browse files
committed
TYP: Fix ndarray.real and .imag
1 parent 060c28a commit ca09a65

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

numpy/__init__.pyi

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,9 @@ else:
17821782
_T = TypeVar("_T")
17831783
_T_co = TypeVar("_T_co", covariant=True)
17841784
_T_contra = TypeVar("_T_contra", contravariant=True)
1785+
_RealT_co = TypeVar("_RealT_co", covariant=True)
1786+
_ImagT_co = TypeVar("_ImagT_co", covariant=True)
1787+
17851788
_2Tuple: TypeAlias = tuple[_T, _T]
17861789
_CastingKind: TypeAlias = L["no", "equiv", "safe", "same_kind", "unsafe"]
17871790

@@ -1802,35 +1805,39 @@ class _SupportsItem(Protocol[_T_co]):
18021805
def item(self, args: Any, /) -> _T_co: ...
18031806

18041807
@type_check_only
1805-
class _SupportsReal(Protocol[_T_co]):
1808+
class _HasRealAndImag(Protocol[_RealT_co, _ImagT_co]):
1809+
@property
1810+
def real(self, /) -> _RealT_co: ...
18061811
@property
1807-
def real(self) -> _T_co: ...
1812+
def imag(self, /) -> _ImagT_co: ...
18081813

18091814
@type_check_only
1810-
class _SupportsImag(Protocol[_T_co]):
1815+
class _HasTypeWithRealAndImag(Protocol[_RealT_co, _ImagT_co]):
18111816
@property
1812-
def imag(self) -> _T_co: ...
1817+
def type(self, /) -> type[_HasRealAndImag[_RealT_co, _ImagT_co]]: ...
1818+
1819+
@type_check_only
1820+
class _HasDTypeWithRealAndImag(Protocol[_RealT_co, _ImagT_co]):
1821+
@property
1822+
def dtype(self, /) -> _HasTypeWithRealAndImag[_RealT_co, _ImagT_co]: ...
18131823

18141824
class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
1815-
__hash__: ClassVar[None]
1825+
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]
18161826
@property
18171827
def base(self) -> None | NDArray[Any]: ...
18181828
@property
18191829
def ndim(self) -> int: ...
18201830
@property
18211831
def size(self) -> int: ...
18221832
@property
1823-
def real(
1824-
self: ndarray[_ShapeType_co, dtype[_SupportsReal[_ScalarType]]], # type: ignore[type-var]
1825-
) -> ndarray[_ShapeType_co, _dtype[_ScalarType]]: ...
1833+
def real(self: _HasDTypeWithRealAndImag[_SCT, object], /) -> ndarray[_ShapeType_co, dtype[_SCT]]: ...
18261834
@real.setter
1827-
def real(self, value: ArrayLike) -> None: ...
1835+
def real(self, value: ArrayLike, /) -> None: ...
18281836
@property
1829-
def imag(
1830-
self: ndarray[_ShapeType_co, dtype[_SupportsImag[_ScalarType]]], # type: ignore[type-var]
1831-
) -> ndarray[_ShapeType_co, _dtype[_ScalarType]]: ...
1837+
def imag(self: _HasDTypeWithRealAndImag[object, _SCT], /) -> ndarray[_ShapeType_co, dtype[_SCT]]: ...
18321838
@imag.setter
1833-
def imag(self, value: ArrayLike) -> None: ...
1839+
def imag(self, value: ArrayLike, /) -> None: ...
1840+
18341841
def __new__(
18351842
cls,
18361843
shape: _ShapeLike,

numpy/lib/_type_check_impl.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ from typing import Literal as L, Any, overload, TypeVar
33

44
import numpy as np
55
from numpy import (
6-
_SupportsImag,
7-
_SupportsReal,
6+
_HasRealAndImag,
87
dtype,
98
generic,
109
floating,
@@ -50,12 +49,12 @@ def mintypecode(
5049
) -> str: ...
5150

5251
@overload
53-
def real(val: _SupportsReal[_T]) -> _T: ...
52+
def real(val: _HasRealAndImag[_T, Any]) -> _T: ...
5453
@overload
5554
def real(val: ArrayLike) -> NDArray[Any]: ...
5655

5756
@overload
58-
def imag(val: _SupportsImag[_T]) -> _T: ...
57+
def imag(val: _HasRealAndImag[Any, _T]) -> _T: ...
5958
@overload
6059
def imag(val: ArrayLike) -> NDArray[Any]: ...
6160

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ AR_c16: npt.NDArray[np.complex128]
2020

2121
AR_LIKE_f: list[float]
2222

23-
class RealObj:
23+
class ComplexObj:
2424
real: slice
25-
26-
class ImagObj:
2725
imag: slice
2826

2927
assert_type(np.mintypecode(["f8"], typeset="qfQF"), str)
3028

31-
assert_type(np.real(RealObj()), slice)
29+
assert_type(np.real(ComplexObj()), slice)
3230
assert_type(np.real(AR_f8), npt.NDArray[np.float64])
3331
assert_type(np.real(AR_c16), npt.NDArray[np.float64])
3432
assert_type(np.real(AR_LIKE_f), npt.NDArray[Any])
3533

36-
assert_type(np.imag(ImagObj()), slice)
34+
assert_type(np.imag(ComplexObj()), slice)
3735
assert_type(np.imag(AR_f8), npt.NDArray[np.float64])
3836
assert_type(np.imag(AR_c16), npt.NDArray[np.float64])
3937
assert_type(np.imag(AR_LIKE_f), npt.NDArray[Any])

0 commit comments

Comments
 (0)