Skip to content

Commit 55b0550

Browse files
authored
Merge pull request numpy#19344 from BvB93/generic-attr
MAINT: Annotate missing attributes of `np.number` subclasses
2 parents f4bd901 + b0c37b8 commit 55b0550

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

numpy/__init__.pyi

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,15 @@ else:
31593159
]
31603160

31613161
class integer(number[_NBit1]): # type: ignore
3162+
@property
3163+
def numerator(self: _ScalarType) -> _ScalarType: ...
3164+
@property
3165+
def denominator(self) -> L[1]: ...
3166+
@overload
3167+
def __round__(self, ndigits: None = ...) -> int: ...
3168+
@overload
3169+
def __round__(self: _ScalarType, ndigits: SupportsIndex) -> _ScalarType: ...
3170+
31623171
# NOTE: `__index__` is technically defined in the bottom-most
31633172
# sub-classes (`int64`, `uint32`, etc)
31643173
def item(
@@ -3232,6 +3241,10 @@ class timedelta64(generic):
32323241
__value: Union[None, int, _CharLike_co, dt.timedelta, timedelta64] = ...,
32333242
__format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] = ...,
32343243
) -> None: ...
3244+
@property
3245+
def numerator(self: _ScalarType) -> _ScalarType: ...
3246+
@property
3247+
def denominator(self) -> L[1]: ...
32353248

32363249
# NOTE: Only a limited number of units support conversion
32373250
# to builtin scalar types: `Y`, `M`, `ns`, `ps`, `fs`, `as`
@@ -3301,7 +3314,8 @@ uint0 = unsignedinteger[_NBitIntP]
33013314
uint = unsignedinteger[_NBitInt]
33023315
ulonglong = unsignedinteger[_NBitLongLong]
33033316

3304-
class inexact(number[_NBit1]): ... # type: ignore
3317+
class inexact(number[_NBit1]): # type: ignore
3318+
def __getnewargs__(self: inexact[_64Bit]) -> Tuple[float, ...]: ...
33053319

33063320
_IntType = TypeVar("_IntType", bound=integer)
33073321
_FloatType = TypeVar('_FloatType', bound=floating)
@@ -3313,6 +3327,21 @@ class floating(inexact[_NBit1]):
33133327
__args: Union[L[0], Tuple[()], Tuple[L[0]]] = ...,
33143328
) -> float: ...
33153329
def tolist(self) -> float: ...
3330+
def is_integer(self: float64) -> bool: ...
3331+
def hex(self: float64) -> str: ...
3332+
@classmethod
3333+
def fromhex(cls: Type[float64], __string: str) -> float64: ...
3334+
def as_integer_ratio(self) -> Tuple[int, int]: ...
3335+
if sys.version_info >= (3, 9):
3336+
def __ceil__(self: float64) -> int: ...
3337+
def __floor__(self: float64) -> int: ...
3338+
def __trunc__(self: float64) -> int: ...
3339+
def __getnewargs__(self: float64) -> Tuple[float]: ...
3340+
def __getformat__(self: float64, __typestr: L["double", "float"]) -> str: ...
3341+
@overload
3342+
def __round__(self, ndigits: None = ...) -> int: ...
3343+
@overload
3344+
def __round__(self: _ScalarType, ndigits: SupportsIndex) -> _ScalarType: ...
33163345
__add__: _FloatOp[_NBit1]
33173346
__radd__: _FloatOp[_NBit1]
33183347
__sub__: _FloatOp[_NBit1]
@@ -3357,6 +3386,9 @@ class complexfloating(inexact[_NBit1], Generic[_NBit1, _NBit2]):
33573386
@property
33583387
def imag(self) -> floating[_NBit2]: ... # type: ignore[override]
33593388
def __abs__(self) -> floating[_NBit1]: ... # type: ignore[override]
3389+
def __getnewargs__(self: complex128) -> Tuple[float, float]: ...
3390+
# NOTE: Deprecated
3391+
# def __round__(self, ndigits=...): ...
33603392
__add__: _ComplexOp[_NBit1]
33613393
__radd__: _ComplexOp[_NBit1]
33623394
__sub__: _ComplexOp[_NBit1]

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import sys
12
import numpy as np
23

34
f2: np.float16
45
f8: np.float64
6+
c8: np.complex64
57

68
# Construction
79

@@ -80,3 +82,13 @@ def func(a: np.float32) -> None: ...
8082

8183
func(f2) # E: incompatible type
8284
func(f8) # E: incompatible type
85+
86+
round(c8) # E: No overload variant
87+
88+
c8.__getnewargs__() # E: Invalid self argument
89+
f2.__getnewargs__() # E: Invalid self argument
90+
f2.is_integer() # E: Invalid self argument
91+
f2.hex() # E: Invalid self argument
92+
np.float16.fromhex("0x0.0p+0") # E: Invalid self argument
93+
f2.__trunc__() # E: Invalid self argument
94+
f2.__getformat__("float") # E: Invalid self argument

numpy/typing/tests/data/reveal/scalars.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
import numpy as np
23

34
b: np.bool_
@@ -6,6 +7,7 @@
67
f8: np.float64
78
c8: np.complex64
89
c16: np.complex128
10+
m: np.timedelta64
911
U: np.str_
1012
S: np.bytes_
1113

@@ -126,3 +128,31 @@
126128
reveal_type(i8.getfield(float)) # E: Any
127129
reveal_type(i8.getfield(np.float64)) # E: {float64}
128130
reveal_type(i8.getfield(np.float64, 8)) # E: {float64}
131+
132+
reveal_type(f8.as_integer_ratio()) # E: Tuple[builtins.int, builtins.int]
133+
reveal_type(f8.is_integer()) # E: bool
134+
reveal_type(f8.__trunc__()) # E: int
135+
reveal_type(f8.__getformat__("float")) # E: str
136+
reveal_type(f8.hex()) # E: str
137+
reveal_type(np.float64.fromhex("0x0.0p+0")) # E: {float64}
138+
139+
reveal_type(f8.__getnewargs__()) # E: Tuple[builtins.float]
140+
reveal_type(c16.__getnewargs__()) # E: Tuple[builtins.float, builtins.float]
141+
142+
reveal_type(i8.numerator) # E: {int64}
143+
reveal_type(i8.denominator) # E: Literal[1]
144+
reveal_type(u8.numerator) # E: {uint64}
145+
reveal_type(u8.denominator) # E: Literal[1]
146+
reveal_type(m.numerator) # E: numpy.timedelta64
147+
reveal_type(m.denominator) # E: Literal[1]
148+
149+
reveal_type(round(i8)) # E: int
150+
reveal_type(round(i8, 3)) # E: {int64}
151+
reveal_type(round(u8)) # E: int
152+
reveal_type(round(u8, 3)) # E: {uint64}
153+
reveal_type(round(f8)) # E: int
154+
reveal_type(round(f8, 3)) # E: {float64}
155+
156+
if sys.version_info >= (3, 9):
157+
reveal_type(f8.__ceil__()) # E: int
158+
reveal_type(f8.__floor__()) # E: int

0 commit comments

Comments
 (0)