Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAINT: Annotate missing attributes of np.number subclasses #19350

Merged
merged 2 commits into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion numpy/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,15 @@ else:
]

class integer(number[_NBit1]): # type: ignore
@property
def numerator(self: _ScalarType) -> _ScalarType: ...
@property
def denominator(self) -> L[1]: ...
@overload
def __round__(self, ndigits: None = ...) -> int: ...
@overload
def __round__(self: _ScalarType, ndigits: SupportsIndex) -> _ScalarType: ...

# NOTE: `__index__` is technically defined in the bottom-most
# sub-classes (`int64`, `uint32`, etc)
def item(
Expand Down Expand Up @@ -3145,6 +3154,10 @@ class timedelta64(generic):
__value: Union[None, int, _CharLike_co, dt.timedelta, timedelta64] = ...,
__format: Union[_CharLike_co, Tuple[_CharLike_co, _IntLike_co]] = ...,
) -> None: ...
@property
def numerator(self: _ScalarType) -> _ScalarType: ...
@property
def denominator(self) -> L[1]: ...

# NOTE: Only a limited number of units support conversion
# to builtin scalar types: `Y`, `M`, `ns`, `ps`, `fs`, `as`
Expand Down Expand Up @@ -3214,7 +3227,8 @@ uint0 = unsignedinteger[_NBitIntP]
uint = unsignedinteger[_NBitInt]
ulonglong = unsignedinteger[_NBitLongLong]

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

_IntType = TypeVar("_IntType", bound=integer)
_FloatType = TypeVar('_FloatType', bound=floating)
Expand All @@ -3226,6 +3240,21 @@ class floating(inexact[_NBit1]):
__args: Union[L[0], Tuple[()], Tuple[L[0]]] = ...,
) -> float: ...
def tolist(self) -> float: ...
def is_integer(self: float64) -> bool: ...
def hex(self: float64) -> str: ...
@classmethod
def fromhex(cls: Type[float64], __string: str) -> float64: ...
def as_integer_ratio(self) -> Tuple[int, int]: ...
if sys.version_info >= (3, 9):
def __ceil__(self: float64) -> int: ...
def __floor__(self: float64) -> int: ...
def __trunc__(self: float64) -> int: ...
def __getnewargs__(self: float64) -> Tuple[float]: ...
def __getformat__(self: float64, __typestr: L["double", "float"]) -> str: ...
@overload
def __round__(self, ndigits: None = ...) -> int: ...
@overload
def __round__(self: _ScalarType, ndigits: SupportsIndex) -> _ScalarType: ...
__add__: _FloatOp[_NBit1]
__radd__: _FloatOp[_NBit1]
__sub__: _FloatOp[_NBit1]
Expand Down Expand Up @@ -3270,6 +3299,9 @@ class complexfloating(inexact[_NBit1], Generic[_NBit1, _NBit2]):
@property
def imag(self) -> floating[_NBit2]: ... # type: ignore[override]
def __abs__(self) -> floating[_NBit1]: ... # type: ignore[override]
def __getnewargs__(self: complex128) -> Tuple[float, float]: ...
# NOTE: Deprecated
# def __round__(self, ndigits=...): ...
__add__: _ComplexOp[_NBit1]
__radd__: _ComplexOp[_NBit1]
__sub__: _ComplexOp[_NBit1]
Expand Down
12 changes: 12 additions & 0 deletions numpy/typing/tests/data/fail/scalars.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import sys
import numpy as np

f2: np.float16
f8: np.float64
c8: np.complex64

# Construction

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

func(f2) # E: incompatible type
func(f8) # E: incompatible type

round(c8) # E: No overload variant

c8.__getnewargs__() # E: Invalid self argument
f2.__getnewargs__() # E: Invalid self argument
f2.is_integer() # E: Invalid self argument
f2.hex() # E: Invalid self argument
np.float16.fromhex("0x0.0p+0") # E: Invalid self argument
f2.__trunc__() # E: Invalid self argument
f2.__getformat__("float") # E: Invalid self argument
30 changes: 30 additions & 0 deletions numpy/typing/tests/data/reveal/scalars.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import numpy as np

b: np.bool_
Expand All @@ -6,6 +7,7 @@
f8: np.float64
c8: np.complex64
c16: np.complex128
m: np.timedelta64
U: np.str_
S: np.bytes_

Expand Down Expand Up @@ -114,3 +116,31 @@
reveal_type(c16.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[{complex128}]]
reveal_type(U.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.str_]]
reveal_type(S.reshape(1)) # E: numpy.ndarray[Any, numpy.dtype[numpy.bytes_]]

reveal_type(f8.as_integer_ratio()) # E: Tuple[builtins.int, builtins.int]
reveal_type(f8.is_integer()) # E: bool
reveal_type(f8.__trunc__()) # E: int
reveal_type(f8.__getformat__("float")) # E: str
reveal_type(f8.hex()) # E: str
reveal_type(np.float64.fromhex("0x0.0p+0")) # E: {float64}

reveal_type(f8.__getnewargs__()) # E: Tuple[builtins.float]
reveal_type(c16.__getnewargs__()) # E: Tuple[builtins.float, builtins.float]

reveal_type(i8.numerator) # E: {int64}
reveal_type(i8.denominator) # E: Literal[1]
reveal_type(u8.numerator) # E: {uint64}
reveal_type(u8.denominator) # E: Literal[1]
reveal_type(m.numerator) # E: numpy.timedelta64
reveal_type(m.denominator) # E: Literal[1]

reveal_type(round(i8)) # E: int
reveal_type(round(i8, 3)) # E: {int64}
reveal_type(round(u8)) # E: int
reveal_type(round(u8, 3)) # E: {uint64}
reveal_type(round(f8)) # E: int
reveal_type(round(f8, 3)) # E: {float64}

if sys.version_info >= (3, 9):
reveal_type(f8.__ceil__()) # E: int
reveal_type(f8.__floor__()) # E: int