8000 ENH: typing: Make `np.complexfloating` generic w.r.t. `np.floating` (… · numpy/numpy@3fbc84a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fbc84a

Browse files
authored
ENH: typing: Make np.complexfloating generic w.r.t. np.floating (#17172)
1 parent 263b293 commit 3fbc84a

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

numpy/__init__.pyi

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ class uint64(unsignedinteger):
462462
class inexact(number): ... # type: ignore
463463
class floating(inexact, _real_generic): ... # type: ignore
464464

465+
_FloatType = TypeVar('_FloatType', bound=floating)
466+
465467
class float16(floating):
466468
def __init__(self, __value: Optional[SupportsFloat] = ...) -> None: ...
467469

@@ -471,27 +473,24 @@ class float32(floating):
471473
class float64(floating):
472474
def __init__(self, __value: Optional[SupportsFloat] = ...) -> None: ...
473475

474-
class complexfloating(inexact): ... # type: ignore
476+
class complexfloating(inexact, Generic[_FloatType]): # type: ignore
477+
@property
478+
def real(self) -> _FloatType: ...
479+
@property
480+
def imag(self) -> _FloatType: ...
481+
def __abs__(self) -> _FloatType: ... # type: ignore[override]
475482

476-
class complex64(complexfloating):
483+
class complex64(complexfloating[float32]):
477484
def __init__(
478485
self,
479486
__value: Union[None, SupportsInt, SupportsFloat, SupportsComplex] = ...
480487
) -> None: ...
481-
@property
482-
def real(self) -> float32: ...
483-
@property
484-
def imag(self) -> float32: ...
485488

486-
class complex128(complexfloating):
489+
class complex128(complexfloating[float64]):
487490
def __init__(
488491
self,
489492
__value: Union[None, SupportsInt, SupportsFloat, SupportsComplex] = ...
490493
) -> None: ...
491-
@property
492-
def real(self) -> float64: ...
493-
@property
494-
def imag(self) -> float64: ...
495494

496495
class flexible(_real_generic): ... # type: ignore
497496

numpy/tests/typing/reveal/scalars.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@
2828
reveal_type(td / 1.0) # E: numpy.timedelta64
2929
reveal_type(td / td) # E: float
3030
reveal_type(td % td) # E: numpy.timedelta64
31+
32+
reveal_type(np.complex64().real) # E: numpy.float32
33+
reveal_type(np.complex128().imag) # E: numpy.float64

0 commit comments

Comments
 (0)
0