Skip to content

Commit e0eee7f

Browse files
authored
Merge pull request #27753 from jorenham/typing/ndarray.real-imag
TYP: Fix the annotations of ``ndarray.real`` and ``ndarray.imag``
2 parents 8c46459 + ca09a65 commit e0eee7f

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

@@ -1823,35 +1826,39 @@ class _HasShapeAndDTypeWithItem(Protocol[_ShapeType_co, _T_co]):
18231826
def dtype(self, /) -> _HashTypeWithItem[_T_co]: ...
18241827

18251828
@type_check_only
1826-
class _SupportsReal(Protocol[_T_co]):
1829+
class _HasRealAndImag(Protocol[_RealT_co, _ImagT_co]):
1830+
@property
1831+
def real(self, /) -> _RealT_co: ...
18271832
@property
1828-
def real(self) -> _T_co: ...
1833+
def imag(self, /) -> _ImagT_co: ...
18291834

18301835
@type_check_only
1831-
class _SupportsImag(Protocol[_T_co]):
1836+
class _HasTypeWithRealAndImag(Protocol[_RealT_co, _ImagT_co]):
18321837
@property
1833-
def imag(self) -> _T_co: ...
1838+
def type(self, /) -> type[_HasRealAndImag[_RealT_co, _ImagT_co]]: ...
1839+
1840+
@type_check_only
1841+
class _HasDTypeWithRealAndImag(Protocol[_RealT_co, _ImagT_co]):
1842+
@property
1843+
def dtype(self, /) -> _HasTypeWithRealAndImag[_RealT_co, _ImagT_co]: ...
18341844

18351845
class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType_co, _DType_co]):
1836-
__hash__: ClassVar[None]
1846+
__hash__: ClassVar[None] # type: ignore[assignment] # pyright: ignore[reportIncompatibleMethodOverride]
18371847
@property
18381848
def base(self) -> None | NDArray[Any]: ...
18391849
@property
18401850
def ndim(self) -> int: ...
18411851
@property
18421852
def size(self) -> int: ...
18431853
@property
1844-
def real(
1845-
self: ndarray[_ShapeType_co, dtype[_SupportsReal[_ScalarType]]], # type: ignore[type-var]
1846-
) -> ndarray[_ShapeType_co, _dtype[_ScalarType]]: ...
1854+
def real(self: _HasDTypeWithRealAndImag[_SCT, object], /) -> ndarray[_ShapeType_co, dtype[_SCT]]: ...
18471855
@real.setter
1848-
def real(self, value: ArrayLike) -> None: ...
1856+
def real(self, value: ArrayLike, /) -> None: ...
18491857
@property
1850-
def imag(
1851-
self: ndarray[_ShapeType_co, dtype[_SupportsImag[_ScalarType]]], # type: ignore[type-var]
1852-
) -> ndarray[_ShapeType_co, _dtype[_ScalarType]]: ...
1858+
def imag(self: _HasDTypeWithRealAndImag[object, _SCT], /) -> ndarray[_ShapeType_co, dtype[_SCT]]: ...
18531859
@imag.setter
1854-
def imag(self, value: ArrayLike) -> None: ...
1860+
def imag(self, value: ArrayLike, /) -> None: ...
1861+
18551862
def __new__(
18561863
cls,
18571864
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)