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

Commit f3c6315

Browse files
johanvergeershoyer
authored andcommitted
Add type hinting for numeric.py (#38)
* Add type hinting for numeric.py * Code cleanup using Black * Add ByteString dependency * Replace default values with "..." to satisfy Flake8 * Add core/numerictypes.pyi * Make _ArrayLike TypeVar a lot simpler since an array_like can be just about anything.
1 parent be9a479 commit f3c6315

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
.pytest_cache
33
__pycache__
44
numpy_stubs.egg-info/
5+
venv
6+
.idea

numpy-stubs/__init__.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import datetime as dt
55
from numpy.core._internal import _ctypes
66
from typing import (
77
Any,
8+
ByteString,
89
Container,
910
Dict,
1011
IO,
@@ -82,6 +83,8 @@ _DtypeLike = Union[
8283

8384
_NdArraySubClass = TypeVar("_NdArraySubClass", bound=ndarray)
8485

86+
_ArrayLike = TypeVar("_ArrayLike")
87+
8588
class dtype:
8689
names: Optional[Tuple[str, ...]]
8790
def __init__(

numpy-stubs/core/numeric.pyi

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from types import ModuleType
2+
from typing import (
3+
Sequence,
4+
Optional,
5+
TypeVar,
6+
Union,
7+
Tuple,
8+
List,
9+
Iterable,
10+
Callable,
11+
Any,
12+
)
13+
14+
from numpy import ndarray, dtype, _ArrayLike, _ShapeLike
15+
16+
_T = TypeVar("_T")
17+
18+
def zeros_like(
19+
a: _ArrayLike,
20+
dtype: Optional[dtype] = ...,
21+
order: str = ...,
22+
subok: bool = ...,
23+
shape: Optional[Union[int, Sequence[int]]] = ...,
24+
) -> ndarray[int]: ...
25+
def ones(
26+
shape: _ShapeLike, dtype: Optional[dtype] = ..., order: str = ...
27+
) -> ndarray[int]: ...
28+
def ones_like(
29+
a: _ArrayLike,
30+
dtype: Optional[dtype] = ...,
31+
order: str = ...,
32+
subok: bool = ...,
33+
shape: Optional[_ShapeLike] = ...,
34+
) -> ndarray[int]: ...
35+
def full(
36+
shape: _ShapeLike, fill_value: _T, dtype: Optional[dtype] = ..., order: str = ...
37+
) -> ndarray[_T]: ...
38+
def full_like(
39+
a: _ArrayLike,
40+
fill_value: _T,
41+
dtype: Optional[dtype] = ...,
42+
order: str = ...,
43+
subok: bool = ...,
44+
shape: Optional[_ShapeLike] = ...,
45+
) -> ndarray[_T]: ...
46+
def count_nonzero(
47+
a: _ArrayLike, axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = ...
48+
) -> Union[int, ndarray[int]]: ...
49+
def isfortran(a: ndarray) -> bool: ...
50+
def argwhere(a: _ArrayLike) -> ndarray: ...
51+
def flatnonzero(a: _ArrayLike) -> ndarray: ...
52+
def correlate(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ...
53+
def convolve(a: _ArrayLike, v: _ArrayLike, mode: str = ...) -> ndarray: ...
54+
def outer(a: _ArrayLike, b: _ArrayLike, out: ndarray = ...) -> ndarray: ...
55+
def tensordot(
56+
a: _ArrayLike,
57+
b: _ArrayLike,
58+
axes: Union[
59+
int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...]
60+
] = ...,
61+
) -> ndarray: ...
62+
def roll(
63+
a: _ArrayLike,
64+
shift: Union[int, Tuple[int, ...]],
65+
axis: Optional[Union[int, Tuple[int, ...]]] = ...,
66+
) -> _T: ...
67+
def rollaxis(a: _ArrayLike, axis: int, start: int = ...) -> ndarray: ...
68+
def normalize_axis_tuple(
69+
axis: Union[int, Iterable[int]],
70+
ndim: int,
71+
argname: Optional[str] = ...,
72+
allow_duplicate: bool = ...,
73+
) -> Tuple[int, ...]: ...
74+
def moveaxis(
75+
a: ndarray,
76+
source: Union[int, Sequence[int]],
77+
destination: Union[int, Sequence[int]],
78+
) -> ndarray: ...
79+
def cross(
80+
a: _ArrayLike,
81+
b: _ArrayLike,
82+
axisa: int = ...,
83+
axisb: int = ...,
84+
axisc: int = ...,
85+
axis: Optional[int] = ...,
86+
) -> ndarray: ...
87+
def indices(
88+
dimensions: Sequence[int], dtype: dtype = ..., sparse: bool = ...
89+
) -> Union[ndarray, Tuple[ndarray, ...]]: ...
90+
def fromfunction(function: Callable, shape: Tuple[int, int], **kwargs) -> Any: ...
91+
def isscalar(element: Any) -> bool: ...
92+
def binary_repr(num: int, width: Optional[int] = ...) -> str: ...
93+
def base_repr(number: int, base: int = ..., padding: int = ...) -> str: ...
94+
def identity(n: int, dtype: Optional[dtype] = ...) -> ndarray: ...
95+
def allclose(
96+
a: _ArrayLike,
97+
b: _ArrayLike,
98+
rtol: float = ...,
99+
atol: float = ...,
100+
equal_nan: bool = ...,
101+
) -> bool: ...
102+
def isclose(
103+
a: _ArrayLike,
104+
b: _ArrayLike,
105+
rtol: float = ...,
106+
atol: float = ...,
107+
equal_nan: bool = ...,
108+
) -> _ArrayLike: ...
109+
def array_equal(a1: _ArrayLike, a2: _ArrayLike) -> bool: ...
110+
def array_equiv(a1: _ArrayLike, a2: _ArrayLike) -> bool: ...
111+
def extend_all(module: ModuleType): ...

numpy-stubs/core/numerictypes.pyi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Any, Optional, Union, Tuple, List
2+
3+
from numpy import dtype
4+
5+
def maximum_sctype(t: dtype) -> dtype: ...
6+
def issctype(rep: Any) -> bool: ...
7+
def obj2sctype(rep: Any, default: Optional[Any] = ...) -> type: ...
8+
def issubclass_(
9+
arg1: type, arg2: Union[type, Tuple[Union[type, Tuple], ...]]
10+
) -> bool: ...
11+
def issubsctype(
12+
arg1: type, arg2: Union[type, Tuple[Union[type, Tuple], ...]]
13+
) -> bool: ...
14+
def issubdtype(
15+
arg1: Union[object, type], arg2: Union[type, Tuple[Union[type, Tuple], ...]]
16+
) -> bool: ...
17+
def sctype2char(sctype: Any) -> str: ...
18+
def find_common_type(
19+
array_types: Union[dtype, List[dtype]], scalar_types: Union[dtype, List[dtype]]
20+
) -> dtype: ...

0 commit comments

Comments
 (0)