From b41054f0ba1011ed33c5e37f24b4ceeeb25ccd1a Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Sat, 11 Apr 2020 12:18:05 -0700 Subject: [PATCH] MAINT: move things in `numeric.pyi` to the top-level `__init__.pyi` In gh-38 some stubs were added for `core/numeric.py`. The problem is that these stubs are not picked up at all, as can be tested with ```python import numpy as np reveal_type(np.ones_like) ``` which on master gives ``` Revealed type is 'Any' ``` The issue is that the NumPy main repo importing from numeric doesn't automatically mean the type stubs do. We could add the relevant imports on the stubs level too, but there is no need for the structure of the stubs to mimic the structure of NumPy unless they are being used to type-check NumPy itself (which we aren't doing yet), and indeed having them mimic the internal structure just causes extra pain of trying to keep up with internal refactors that don't affect the public API. In fact, we already ignore NumPy's internal structure by defining `ndarray` in the top-level `__init__.pyi`, when it is actually exported by `numeric.py`. So instead, just fix the issue by moving the numeric stubs into the top level `__init__.pyi` (plus some minor bug fixes). On master the above script now correctly gives: ``` Revealed type is 'def [_ArrayLike] (a: _ArrayLike`-1, dtype: Union[numpy.dtype, None] =, order: builtins.str =, subok: builtins.bool =, shape: Union[builtins.int, typing.Sequence[builtins.int], None] =) -> numpy.ndarray ```` --- numpy-stubs/__init__.pyi | 85 +++++++++++++++++++++++++++ numpy-stubs/core/numeric.pyi | 111 ----------------------------------- 2 files changed, 85 insertions(+), 111 deletions(-) delete mode 100644 numpy-stubs/core/numeric.pyi diff --git a/numpy-stubs/__init__.pyi b/numpy-stubs/__init__.pyi index 0847317..b6c4b0d 100644 --- a/numpy-stubs/__init__.pyi +++ b/numpy-stubs/__init__.pyi @@ -7,6 +7,7 @@ from typing import ( Any, ByteString, Container, + Callable, Dict, IO, Iterable, @@ -485,6 +486,90 @@ def zeros( def ones( shape: _ShapeLike, dtype: _DtypeLike = ..., order: Optional[str] = ... ) -> ndarray: ... +def zeros_like( + a: _ArrayLike, + dtype: Optional[dtype] = ..., + order: str = ..., + subok: bool = ..., + shape: Optional[Union[int, Sequence[int]]] = ..., +) -> ndarray: ... +def ones_like( + a: _ArrayLike, + dtype: Optional[dtype] = ..., + order: str = ..., + subok: bool = ..., + shape: Optional[_ShapeLike] = ..., +) -> ndarray[int]: ... +def full( + shape: _ShapeLike, fill_value: Any, dtype: Optional[dtype] = ..., order: str = ... +) -> ndarray: ... +def full_like( + a: _ArrayLike, + fill_value: Any, + dtype: Optional[dtype] = ..., + order: str = ..., + subok: bool = ..., + shape: Optional[_ShapeLike] = ..., +) -> ndarray: ... +def count_nonzero( + a: _ArrayLike, axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = ... +) -> Union[int, ndarray]: ... +def isfortran(a: ndarray) -> bool: ... +def argwhere(a: _ArrayLike) -> ndarray: ... +def flatnonzero(a: _ArrayLike) -> ndarray: ... +def correlate(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ... +def convolve(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ... +def outer(a: _ArrayLike, b: _ArrayLike, out: ndarray = ...) -> ndarray: ... +def tensordot( + a: _ArrayLike, + b: _ArrayLike, + axes: Union[ + int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...] + ] = ..., +) -> ndarray: ... +def roll( + a: _ArrayLike, + shift: Union[int, Tuple[int, ...]], + axis: Optional[Union[int, Tuple[int, ...]]] = ..., +) -> ndarray: ... +def rollaxis(a: _ArrayLike, axis: int, start: int = ...) -> ndarray: ... +def moveaxis( + a: ndarray, + source: Union[int, Sequence[int]], + destination: Union[int, Sequence[int]], +) -> ndarray: ... +def cross( + a: _ArrayLike, + b: _ArrayLike, + axisa: int = ..., + axisb: int = ..., + axisc: int = ..., + axis: Optional[int] = ..., +) -> ndarray: ... +def indices( + dimensions: Sequence[int], dtype: dtype = ..., sparse: bool = ... +) -> Union[ndarray, Tuple[ndarray, ...]]: ... +def fromfunction(function: Callable, shape: Tuple[int, int], **kwargs) -> Any: ... +def isscalar(element: Any) -> bool: ... +def binary_repr(num: int, width: Optional[int] = ...) -> str: ... +def base_repr(number: int, base: int = ..., padding: int = ...) -> str: ... +def identity(n: int, dtype: Optional[dtype] = ...) -> ndarray: ... +def allclose( + a: _ArrayLike, + b: _ArrayLike, + rtol: float = ..., + atol: float = ..., + equal_nan: bool = ..., +) -> bool: ... +def isclose( + a: _ArrayLike, + b: _ArrayLike, + rtol: float = ..., + atol: float = ..., + equal_nan: bool = ..., +) -> Union[bool_, ndarray]: ... +def array_equal(a1: _ArrayLike, a2: _ArrayLike) -> bool: ... +def array_equiv(a1: _ArrayLike, a2: _ArrayLike) -> bool: ... # TODO(shoyer): remove when the full numpy namespace is defined def __getattr__(name: str) -> Any: ... diff --git a/numpy-stubs/core/numeric.pyi b/numpy-stubs/core/numeric.pyi deleted file mode 100644 index 1e43676..0000000 --- a/numpy-stubs/core/numeric.pyi +++ /dev/null @@ -1,111 +0,0 @@ -from types import ModuleType -from typing import ( - Sequence, - Optional, - TypeVar, - Union, - Tuple, - List, - Iterable, - Callable, - Any, -) - -from numpy import ndarray, dtype, _ArrayLike, _ShapeLike - -_T = TypeVar("_T") - -def zeros_like( - a: _ArrayLike, - dtype: Optional[dtype] = ..., - order: str = ..., - subok: bool = ..., - shape: Optional[Union[int, Sequence[int]]] = ..., -) -> ndarray[int]: ... -def ones( - shape: _ShapeLike, dtype: Optional[dtype] = ..., order: str = ... -) -> ndarray[int]: ... -def ones_like( - a: _ArrayLike, - dtype: Optional[dtype] = ..., - order: str = ..., - subok: bool = ..., - shape: Optional[_ShapeLike] = ..., -) -> ndarray[int]: ... -def full( - shape: _ShapeLike, fill_value: _T, dtype: Optional[dtype] = ..., order: str = ... -) -> ndarray[_T]: ... -def full_like( - a: _ArrayLike, - fill_value: _T, - dtype: Optional[dtype] = ..., - order: str = ..., - subok: bool = ..., - shape: Optional[_ShapeLike] = ..., -) -> ndarray[_T]: ... -def count_nonzero( - a: _ArrayLike, axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = ... -) -> Union[int, ndarray[int]]: ... -def isfortran(a: ndarray) -> bool: ... -def argwhere(a: _ArrayLike) -> ndarray: ... -def flatnonzero(a: _ArrayLike) -> ndarray: ... -def correlate(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ... -def convolve(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ... -def outer(a: _ArrayLike, b: _ArrayLike, out: ndarray = ...) -> ndarray: ... -def tensordot( - a: _ArrayLike, - b: _ArrayLike, - axes: Union[ - int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...] - ] = ..., -) -> ndarray: ... -def roll( - a: _ArrayLike, - shift: Union[int, Tuple[int, ...]], - axis: Optional[Union[int, Tuple[int, ...]]] = ..., -) -> _T: ... -def rollaxis(a: _ArrayLike, axis: int, start: int = ...) -> ndarray: ... -def normalize_axis_tuple( - axis: Union[int, Iterable[int]], - ndim: int, - argname: Optional[str] = ..., - allow_duplicate: bool = ..., -) -> Tuple[int, ...]: ... -def moveaxis( - a: ndarray, - source: Union[int, Sequence[int]], - destination: Union[int, Sequence[int]], -) -> ndarray: ... -def cross( - a: _ArrayLike, - b: _ArrayLike, - axisa: int = ..., - axisb: int = ..., - axisc: int = ..., - axis: Optional[int] = ..., -) -> ndarray: ... -def indices( - dimensions: Sequence[int], dtype: dtype = ..., sparse: bool = ... -) -> Union[ndarray, Tuple[ndarray, ...]]: ... -def fromfunction(function: Callable, shape: Tuple[int, int], **kwargs) -> Any: ... -def isscalar(element: Any) -> bool: ... -def binary_repr(num: int, width: Optional[int] = ...) -> str: ... -def base_repr(number: int, base: int = ..., padding: int = ...) -> str: ... -def identity(n: int, dtype: Optional[dtype] = ...) -> ndarray: ... -def allclose( - a: _ArrayLike, - b: _ArrayLike, - rtol: float = ..., - atol: float = ..., - equal_nan: bool = ..., -) -> bool: ... -def isclose( - a: _ArrayLike, - b: _ArrayLike, - rtol: float = ..., - atol: float = ..., - equal_nan: bool = ..., -) -> _ArrayLike: ... -def array_equal(a1: _ArrayLike, a2: _ArrayLike) -> bool: ... -def array_equiv(a1: _ArrayLike, a2: _ArrayLike) -> bool: ... -def extend_all(module: ModuleType): ...