Skip to content

Commit b90c3c1

Browse files
committed
TYP: Explicitly mark complexfloating.__round__ as deprecated
1 parent 50991f6 commit b90c3c1

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

numpy/__init__.pyi

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ from typing import (
210210
# library include `typing_extensions` stubs:
211211
# https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi
212212
from _typeshed import StrOrBytesPath, SupportsFlush, SupportsLenAndGetItem, SupportsWrite
213-
from typing_extensions import CapsuleType, Generic, LiteralString, Protocol, Self, TypeVar, overload
213+
from typing_extensions import CapsuleType, Generic, LiteralString, Protocol, Self, TypeVar, deprecated, overload
214214

215215
from numpy import (
216216
core,
@@ -3410,22 +3410,27 @@ _ComplexValue: TypeAlias = (
34103410
| complex # `complex` is not a subtype of `SupportsComplex`
34113411
)
34123412

3413-
class integer(number[_NBit1]): # type: ignore
3413+
@type_check_only
3414+
class _Roundable:
3415+
@overload
3416+
def __round__(self, /, ndigits: None = None) -> int: ...
3417+
@overload
3418+
def __round__(self, /, ndigits: SupportsIndex) -> Self: ...
3419+
3420+
class integer(_Roundable, number[_NBit1]): # type: ignore
34143421
@property
34153422
def numerator(self) -> Self: ...
34163423
@property
34173424
def denominator(self) -> L[1]: ...
3418-
@overload
3419-
def __round__(self, ndigits: None = ..., /) -> int: ...
3420-
@overload
3421-
def __round__(self, ndigits: SupportsIndex, /) -> Self: ...
3425+
def is_integer(self, /) -> L[True]: ...
34223426

34233427
def item(self, args: L[0] | tuple[()] | tuple[L[0]] = ..., /) -> int: ...
34243428
def tolist(self) -> int: ...
3425-
def is_integer(self) -> L[True]: ...
3426-
def bit_count(self) -> int: ...
3427-
# NOTE: `__index__` is technically defined in the bottom-most sub-classes (`int64`, `uint32`, etc)
3428-
def __index__(self) -> int: ...
3429+
3430+
# NOTE: `bit_count` and `__index__` are technically defined in the concrete subtypes
3431+
def bit_count(self, /) -> int: ...
3432+
def __index__(self, /) -> int: ...
3433+
34293434
__truediv__: _IntTrueDiv[_NBit1]
34303435
__rtruediv__: _IntTrueDiv[_NBit1]
34313436
def __mod__(self, value: _IntLike_co, /) -> integer[Any]: ...
@@ -3566,16 +3571,15 @@ class inexact(number[_NBit1]): ... # type: ignore[misc]
35663571

35673572
_IntType = TypeVar("_IntType", bound=integer[Any])
35683573

3569-
class floating(inexact[_NBit1]):
3574+
class floating(_Roundable, inexact[_NBit1]):
35703575
def __init__(self, value: _FloatValue = ..., /) -> None: ...
35713576
def item(self, args: L[0] | tuple[()] | tuple[L[0]] = ..., /) -> float: ...
35723577
def tolist(self) -> float: ...
3573-
def is_integer(self) -> builtins.bool: ...
3574-
def as_integer_ratio(self) -> tuple[int, int]: ...
3575-
@overload
3576-
def __round__(self, ndigits: None = ..., /) -> int: ...
3577-
@overload
3578-
def __round__(self, ndigits: SupportsIndex, /) -> Self: ...
3578+
3579+
# NOTE: `is_integer` and `as_integer_ratio` are technically defined in the concrete subtypes
3580+
def is_integer(self, /) -> builtins.bool: ...
3581+
def as_integer_ratio(self, /) -> tuple[int, int]: ...
3582+
35793583
__add__: _FloatOp[_NBit1]
35803584
__radd__: _FloatOp[_NBit1]
35813585
__sub__: _FloatOp[_NBit1]
@@ -3742,9 +3746,14 @@ class complexfloating(inexact[_NBit1], Generic[_NBit1, _NBit2]):
37423746
@property
37433747
def imag(self) -> floating[_NBit2]: ... # type: ignore[override]
37443748

3745-
# NOTE: `__complex__` is not defined here, but in each of the concrete subtypes
3749+
# NOTE: `__complex__` is technically defined in the concrete subtypes
37463750
def __complex__(self, /) -> complex: ...
37473751
def __abs__(self, /) -> floating[_NBit1 | _NBit2]: ... # type: ignore[override]
3752+
@deprecated(
3753+
"The Python built-in `round` is deprecated for complex scalars, and will raise a `TypeError` in a future release. "
3754+
"Use `np.round` or `scalar.round` instead."
3755+
)
3756+
def __round__(self, /, ndigits: SupportsIndex | None = None) -> Self: ...
37483757

37493758
@overload
37503759
def __add__(self, other: _Complex64_co, /) -> complexfloating[_NBit1, _NBit2]: ...

numpy/typing/tests/data/fail/scalars.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ def func(a: np.float32) -> None: ...
8282
func(f2) # E: incompatible type
8383
func(f8) # E: incompatible type
8484

85-
round(c8) # E: No overload variant
86-
8785
c8.__getnewargs__() # E: Invalid self argument
8886
f2.__getnewargs__() # E: Invalid self argument
8987
f2.hex() # E: Invalid self argument

0 commit comments

Comments
 (0)