Skip to content
This repository was archived by the owner on Jun 10, 2020. It is now read-only.

Commit f335f16

Browse files
authored
MAINT: move things in numeric.pyi to the top-level __init__.pyi (#50)
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 ````
1 parent 2c8d8f2 commit f335f16

File tree

2 files changed

+85
-111
lines changed

2 files changed

+85
-111
lines changed

numpy-stubs/__init__.pyi

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ from typing import (
77
Any,
88
ByteString,
99
Container,
10+
Callable,
1011
Dict,
1112
IO,
1213
Iterable,
@@ -485,6 +486,90 @@ def zeros(
485486
def ones(
486487
shape: _ShapeLike, dtype: _DtypeLike = ..., order: Optional[str] = ...
487488
) -> ndarray: ...
489+
def zeros_like(
490+
a: _ArrayLike,
491+
dtype: Optional[dtype] = ...,
492+
order: str = ...,
493+
subok: bool = ...,
494+
shape: Optional[Union[int, Sequence[int]]] = ...,
495+
) -> ndarray: ...
496+
def ones_like(
497+
a: _ArrayLike,
498+
dtype: Optional[dtype] = ...,
499+
order: str = ...,
500+
subok: bool = ...,
501+
shape: Optional[_ShapeLike] = ...,
502+
) -> ndarray[int]: ...
503+
def full(
504+
shape: _ShapeLike, fill_value: Any, dtype: Optional[dtype] = ..., order: str = ...
505+
) -> ndarray: ...
506+
def full_like(
507+
a: _ArrayLike,
508+
fill_value: Any,
509+
dtype: Optional[dtype] = ...,
510+
order: str = ...,
511+
subok: bool = ...,
512+
shape: Optional[_ShapeLike] = ...,
513+
) -> ndarray: ...
514+
def count_nonzero(
515+
a: _ArrayLike, axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = ...
516+
) -> Union[int, ndarray]: ...
517+
def isfortran(a: ndarray) -> bool: ...
518+
def argwhere(a: _ArrayLike) -> ndarray: ...
519+
def flatnonzero(a: _ArrayLike) -> ndarray: ...
520+
def correlate(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ...
521+
def convolve(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ...
522+
def outer(a: _ArrayLike, b: _ArrayLike, out: ndarray = ...) -> ndarray: ...
523+
def tensordot(
524+
a: _ArrayLike,
525+
b: _ArrayLike,
526+
axes: Union[
527+
int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...]
528+
] = ...,
529+
) -> ndarray: ...
530+
def roll(
531+
a: _ArrayLike,
532+
shift: Union[int, Tuple[int, ...]],
533+
axis: Optional[Union[int, Tuple[int, ...]]] = ...,
534+
) -> ndarray: ...
535+
def rollaxis(a: _ArrayLike, axis: int, start: int = ...) -> ndarray: ...
536+
def moveaxis(
537+
a: ndarray,
538+
source: Union[int, Sequence[int]],
539+
destination: Union[int, Sequence[int]],
540+
) -> ndarray: ...
541+
def cross(
542+
a: _ArrayLike,
543+
b: _ArrayLike,
544+
axisa: int = ...,
545+
axisb: int = ...,
546+
axisc: int = ...,
547+
axis: Optional[int] = ...,
548+
) -> ndarray: ...
549+
def indices(
550+
dimensions: Sequence[int], dtype: dtype = ..., sparse: bool = ...
551+
) -> Union[ndarray, Tuple[ndarray, ...]]: ...
552+
def fromfunction(function: Callable, shape: Tuple[int, int], **kwargs) -> Any: ...
553+
def isscalar(element: Any) -> bool: ...
554+
def binary_repr(num: int, width: Optional[int] = ...) -> str: ...
555+
def base_repr(number: int, base: int = ..., padding: int = ...) -> str: ...
556+
def identity(n: int, dtype: Optional[dtype] = ...) -> ndarray: ...
557+
def allclose(
558+
a: _ArrayLike,
559+
b: _ArrayLike,
560+
rtol: float = ...,
561+
atol: float = ...,
562+
equal_nan: bool = ...,
563+
) -> bool: ...
564+
def isclose(
565+
a: _ArrayLike,
566+
b: _ArrayLike,
567+
rtol: float = ...,
568+
atol: float = ...,
569+
equal_nan: bool = ...,
570+
) -> Union[bool_, ndarray]: ...
571+
def array_equal(a1: _ArrayLike, a2: _ArrayLike) -> bool: ...
572+
def array_equiv(a1: _ArrayLike, a2: _ArrayLike) -> bool: ...
488573

489574
# TODO(shoyer): remove when the full numpy namespace is defined
490575
def __getattr__(name: str) -> Any: ...

numpy-stubs/core/numeric.pyi

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)