Skip to content

Commit dbc2fb4

Browse files
committed
TYP: Add missing real and imag properties to numpy.generic
1 parent b90c3c1 commit dbc2fb4

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

numpy/__init__.pyi

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,10 @@ _SortSide: TypeAlias = L["left", "right"]
13771377

13781378
@type_check_only
13791379
class _ArrayOrScalarCommon:
1380+
@property
1381+
def real(self, /) -> Any: ...
1382+
@property
1383+
def imag(self, /) -> Any: ...
13801384
@property
13811385
def T(self) -> Self: ...
13821386
@property
@@ -3258,10 +3262,6 @@ class generic(_ArrayOrScalarCommon):
32583262
def dtype(self) -> _dtype[Self]: ...
32593263

32603264
class number(generic, Generic[_NBit1]): # type: ignore
3261-
@property
3262-
def real(self) -> Self: ...
3263-
@property
3264-
def imag(self) -> Self: ...
32653265
def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
32663266
def __neg__(self) -> Self: ...
32673267
def __pos__(self) -> Self: ...
@@ -3284,16 +3284,18 @@ class number(generic, Generic[_NBit1]): # type: ignore
32843284
__gt__: _ComparisonOpGT[_NumberLike_co, _ArrayLikeNumber_co]
32853285
__ge__: _ComparisonOpGE[_NumberLike_co, _ArrayLikeNumber_co]
32863286

3287-
class bool(generic):
3288-
def __init__(self, value: object = ..., /) -> None: ...
3289-
def item(
3290-
self, args: L[0] | tuple[()] | tuple[L[0]] = ..., /,
3291-
) -> builtins.bool: ...
3292-
def tolist(self) -> builtins.bool: ...
3287+
@type_check_only
3288+
class _RealMixin:
32933289
@property
32943290
def real(self) -> Self: ...
32953291
@property
32963292
def imag(self) -> Self: ...
3293+
3294+
class bool(_RealMixin, generic):
3295+
def __init__(self, value: object = ..., /) -> None: ...
3296+
def item(self, args: L[0] | tuple[()] | tuple[L[0]] = ..., /) -> builtins.bool: ...
3297+
def tolist(self) -> builtins.bool: ...
3298+
32973299
def __abs__(self) -> Self: ...
32983300
__add__: _BoolOp[np.bool]
32993301
__radd__: _BoolOp[np.bool]
@@ -3330,13 +3332,12 @@ class bool(generic):
33303332
bool_: TypeAlias = bool
33313333

33323334
_StringType = TypeVar("_StringType", bound=str | bytes)
3333-
_ShapeType = TypeVar("_ShapeType", bound=_Shape)
33343335
_ObjectType = TypeVar("_ObjectType", bound=object)
33353336

33363337
# The `object_` constructor returns the passed object, so instances with type
33373338
# `object_` cannot exists (at runtime).
33383339
@final
3339-
class object_(generic):
3340+
class object_(_RealMixin, generic):
33403341
@overload
33413342
def __new__(cls, nothing_to_see_here: None = ..., /) -> None: ...
33423343
@overload
@@ -3351,11 +3352,6 @@ class object_(generic):
33513352
@overload
33523353
def __new__(cls, value: Any = ..., /) -> object | NDArray[object_]: ...
33533354

3354-
@property
3355-
def real(self) -> Self: ...
3356-
@property
3357-
def imag(self) -> Self: ...
3358-
33593355
if sys.version_info >= (3, 12):
33603356
def __release_buffer__(self, buffer: memoryview, /) -> None: ...
33613357

@@ -3372,7 +3368,7 @@ class _DatetimeScalar(Protocol):
33723368

33733369
# TODO: `item`/`tolist` returns either `dt.date`, `dt.datetime` or `int`
33743370
# depending on the unit
3375-
class datetime64(generic):
3371+
class datetime64(_RealMixin, generic):
33763372
@overload
33773373
def __init__(
33783374
self,
@@ -3411,19 +3407,21 @@ _ComplexValue: TypeAlias = (
34113407
)
34123408

34133409
@type_check_only
3414-
class _Roundable:
3410+
class _RoundMixin:
34153411
@overload
34163412
def __round__(self, /, ndigits: None = None) -> int: ...
34173413
@overload
34183414
def __round__(self, /, ndigits: SupportsIndex) -> Self: ...
34193415

3420-
class integer(_Roundable, number[_NBit1]): # type: ignore
3416+
@type_check_only
3417+
class _IntegralMixin(_RealMixin):
34213418
@property
34223419
def numerator(self) -> Self: ...
34233420
@property
34243421
def denominator(self) -> L[1]: ...
3425-
def is_integer(self, /) -> L[True]: ...
34263422

3423+
class integer(_IntegralMixin, _RoundMixin, number[_NBit1]): # type: ignore
3424+
def is_integer(self, /) -> L[True]: ...
34273425
def item(self, args: L[0] | tuple[()] | tuple[L[0]] = ..., /) -> int: ...
34283426
def tolist(self) -> int: ...
34293427

@@ -3490,17 +3488,13 @@ longlong = signedinteger[_NBitLongLong]
34903488

34913489
# TODO: `item`/`tolist` returns either `dt.timedelta` or `int`
34923490
# depending on the unit
3493-
class timedelta64(generic):
3491+
class timedelta64(_IntegralMixin, generic):
34943492
def __init__(
34953493
self,
34963494
value: None | int | _CharLike_co | dt.timedelta | timedelta64 = ...,
34973495
format: _CharLike_co | tuple[_CharLike_co, _IntLike_co] = ...,
34983496
/,
34993497
) -> None: ...
3500-
@property
3501-
def numerator(self) -> Self: ...
3502-
@property
3503-
def denominator(self) -> L[1]: ...
35043498

35053499
# NOTE: Only a limited number of units support conversion
35063500
# to builtin scalar types: `Y`, `M`, `ns`, `ps`, `fs`, `as`
@@ -3569,9 +3563,7 @@ ulonglong: TypeAlias = unsignedinteger[_NBitLongLong]
35693563

35703564
class inexact(number[_NBit1]): ... # type: ignore[misc]
35713565

3572-
_IntType = TypeVar("_IntType", bound=integer[Any])
3573-
3574-
class floating(_Roundable, inexact[_NBit1]):
3566+
class floating(_RealMixin, _RoundMixin, inexact[_NBit1]):
35753567
def __init__(self, value: _FloatValue = ..., /) -> None: ...
35763568
def item(self, args: L[0] | tuple[()] | tuple[L[0]] = ..., /) -> float: ...
35773569
def tolist(self) -> float: ...
@@ -3608,7 +3600,7 @@ class float64(floating[_64Bit], float): # type: ignore[misc]
36083600
def __getformat__(self, typestr: L["double", "float"], /) -> str: ...
36093601
def __getnewargs__(self, /) -> tuple[float]: ...
36103602

3611-
# overrides for `floating` and `builtins.float` compatibility
3603+
# overrides for `floating` and `builtins.float` compatibility (`_RealMixin` doesn't work)
36123604
@property
36133605
def real(self) -> Self: ...
36143606
@property
@@ -3869,7 +3861,7 @@ csingle: TypeAlias = complexfloating[_NBitSingle, _NBitSingle]
38693861
cdouble: TypeAlias = complexfloating[_NBitDouble, _NBitDouble]
38703862
clongdouble: TypeAlias = complexfloating[_NBitLongDouble, _NBitLongDouble]
38713863

3872-
class flexible(generic): ... # type: ignore
3864+
class flexible(_RealMixin, generic): ... # type: ignore
38733865

38743866
# TODO: `item`/`tolist` returns either `bytes` or `tuple`
38753867
# depending on whether or not it's used as an opaque bytes sequence
@@ -3879,13 +3871,7 @@ class void(flexible):
38793871
def __init__(self, value: _IntLike_co | bytes, /, dtype : None = ...) -> None: ...
38803872
@overload
38813873
def __init__(self, value: Any, /, dtype: _DTypeLikeVoid) -> None: ...
3882-
@property
3883-
def real(self) -> Self: ...
3884-
@property
3885-
def imag(self) -> Self: ...
3886-
def setfield(
3887-
self, val: ArrayLike, dtype: DTypeLike, offset: int = ...
3888-
) -> None: ...
3874+
def setfield(self, val: ArrayLike, dtype: DTypeLike, offset: int = ...) -> None: ...
38893875
@overload
38903876
def __getitem__(self, key: str | SupportsIndex, /) -> Any: ...
38913877
@overload

0 commit comments

Comments
 (0)