This repository was archived by the owner on Jun 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add type hinting for numeric.py #38
Merged
shoyer
merged 6 commits into
numpy:master
from
johanvergeer:add-type-hints-for-numeric.py
Jan 14, 2020
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2c70580
Add type hinting for numeric.py
johanvergeer 58bc28d
Code cleanup using Black
johanvergeer 3c81e70
Add ByteString dependency
johanvergeer 1b7afd7
Replace default values with "..." to satisfy Flake8
johanvergeer 015f47b
Add core/numerictypes.pyi
johanvergeer b5272fb
Make _ArrayLike TypeVar a lot simpler since an array_like can be just…
johanvergeer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Code cleanup using Black
- Loading branch information
commit 58bc28dcd173cb0b6d4b83d42d1d26e27f7b65e2
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,155 +1,111 @@ | ||
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') | ||
|
||
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] = None, | ||
order: str = 'K', | ||
subok: bool = True, | ||
shape: Optional[Union[int, Sequence[int]]] = None) -> ndarray[int]: ... | ||
|
||
|
||
a: _ArrayLike, | ||
dtype: Optional[dtype] = None, | ||
order: str = "K", | ||
subok: bool = True, | ||
shape: Optional[Union[int, Sequence[int]]] = None, | ||
) -> ndarray[int]: ... | ||
def ones( | ||
shape: _ShapeLike, | ||
dtype: Optional[dtype] = None, | ||
order: str = 'C') -> ndarray[int]: ... | ||
|
||
|
||
shape: _ShapeLike, dtype: Optional[dtype] = None, order: str = "C" | ||
) -> ndarray[int]: ... | ||
def ones_like( | ||
a: _ArrayLike, | ||
dtype: Optional[dtype] = None, | ||
order: str = 'K', | ||
subok: bool = True, | ||
shape: Optional[_ShapeLike] = None) -> ndarray[int]: ... | ||
|
||
|
||
a: _ArrayLike, | ||
dtype: Optional[dtype] = None, | ||
order: str = "K", | ||
subok: bool = True, | ||
shape: Optional[_ShapeLike] = None, | ||
) -> ndarray[int]: ... | ||
def full( | ||
shape: _ShapeLike, | ||
fill_value: T, | ||
dtype: Optional[dtype] = None, | ||
order: str = 'C') -> ndarray[T]: ... | ||
|
||
|
||
shape: _ShapeLike, fill_value: T, dtype: Optional[dtype] = None, order: str = "C" | ||
) -> ndarray[T]: ... | ||
def full_like( | ||
a: _ArrayLike, | ||
fill_value: T, | ||
dtype: Optional[dtype] = None, | ||
order: str = 'K', | ||
subok: bool = True, | ||
shape: Optional[_ShapeLike] = None) -> ndarray[T]: ... | ||
|
||
|
||
a: _ArrayLike, | ||
fill_value: T, | ||
dtype: Optional[dtype] = None, | ||
order: str = "K", | ||
subok: bool = True, | ||
shape: Optional[_ShapeLike] = None, | ||
) -> ndarray[T]: ... | ||
def count_nonzero( | ||
a: _ArrayLike, | ||
axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = None) -> Union[int, ndarray[int]]: ... | ||
|
||
|
||
a: _ArrayLike, axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = None | ||
) -> 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 = 'valid') -> ndarray: ... | ||
|
||
|
||
def convolve(a: _ArrayLike, v: _ArrayLike, mode: str = 'full') -> ndarray: ... | ||
|
||
|
||
def correlate(a: _ArrayLike, v: _ArrayLike, mode: str = "valid") -> ndarray: ... | ||
def convolve(a: _ArrayLike, v: _ArrayLike, mode: str = "full") -> ndarray: ... | ||
def outer(a: _ArrayLike, b: _ArrayLike, out: ndarray = None) -> ndarray: ... | ||
|
||
|
||
def tensordot( | ||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
axes: Union[int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...]] = 2) -> ndarray: ... | ||
|
||
|
||
def roll(a: _ArrayLike, | ||
shift: Union[int, Tuple[int, ...]], | ||
axis: Optional[Union[int, Tuple[int, ...]]] = None) -> T: ... | ||
|
||
|
||
def rollaxis( | ||
a: _ArrayLike, | ||
axis: int, | ||
start: int = 0) -> ndarray: ... | ||
|
||
|
||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
axes: Union[ | ||
int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...] | ||
] = 2, | ||
) -> ndarray: ... | ||
def roll( | ||
a: _ArrayLike, | ||
shift: Union[int, Tuple[int, ...]], | ||
axis: Optional[Union[int, Tuple[int, ...]]] = None, | ||
) -> T: ... | ||
def rollaxis(a: _ArrayLike, axis: int, start: int = 0) -> ndarray: ... | ||
def normalize_axis_tuple( | ||
axis: Union[int, Iterable[int]], | ||
ndim: int, | ||
argname: Optional[str] = None, | ||
allow_duplicate: bool = False) -> Tuple[int, ...]: ... | ||
|
||
|
||
axis: Union[int, Iterable[int]], | ||
ndim: int, | ||
argname: Optional[str] = None, | ||
allow_duplicate: bool = False, | ||
) -> Tuple[int, ...]: ... | ||
def moveaxis( | ||
a: ndarray, | ||
source: Union[int, Sequence[int]], | ||
destination: Union[int, Sequence[int]]) -> ndarray: ... | ||
|
||
|
||
a: ndarray, | ||
source: Union[int, Sequence[int]], | ||
destination: Union[int, Sequence[int]], | ||
) -> ndarray: ... | ||
def cross( | ||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
axisa: int = -1, | ||
axisb: int = -1, | ||
axisc: int = -1, | ||
axis: Optional[int] = None) -> ndarray: ... | ||
|
||
|
||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
axisa: int = -1, | ||
axisb: int = -1, | ||
axisc: int = -1, | ||
axis: Optional[int] = None, | ||
) -> ndarray: ... | ||
def indices( | ||
dimensions: Sequence[int], | ||
dtype: dtype = int, | ||
sparse: bool = False) -> Union[ndarray, Tuple[ndarray, ...]]: ... | ||
|
||
|
||
def fromfunction( | ||
function: Callable, | ||
shape: Tuple[int, int], | ||
**kwargs) -> Any: ... | ||
|
||
|
||
dimensions: Sequence[int], dtype: dtype = int, sparse: bool = False | ||
) -> 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] = None) -> str: ... | ||
|
||
|
||
def base_repr(number: int, base: int = 2, padding: int = 0) -> str: ... | ||
|
||
|
||
def identity(n: int, dtype: Optional[dtype] = None) -> ndarray: ... | ||
|
||
|
||
def allclose( | ||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
rtol: float = 1.e-5, | ||
atol: float = 1.e-8, | ||
equal_nan: bool = False) -> bool: ... | ||
|
||
|
||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
rtol: float = 1.0e-5, | ||
atol: float = 1.0e-8, | ||
equal_nan: bool = False, | ||
) -> bool: ... | ||
def isclose( | ||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
rtol: float = 1.e-5, | ||
atol: float = 1.e-8, | ||
equal_nan: bool = False) -> _ArrayLike: ... | ||
|
||
|
||
a: _ArrayLike, | ||
b: _ArrayLike, | ||
rtol: float = 1.0e-5, | ||
atol: float = 1.0e-8, | ||
equal_nan: bool = False, | ||
) -> _ArrayLike: ... | ||
def array_equal(a1: _ArrayLike, a2: _ArrayLike) -> bool: ... | ||
|
||
|
||
def array_equiv(a1: _ArrayLike, a2: _ArrayLike) -> bool: ... | ||
|
||
|
||
def extend_all(module: ModuleType): ... |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a meaningful difference between
TypeVar("_ArrayLike")
,TypeVar("_ArrayLike", object)
and what you wrote here?I agree that it's a good idea to clearly identify "array like" arguments, but I'm not certain this the right annotation. Note that in most cases NumPy is fine with absolutely any type of object in cases where it needs an "array like" argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have a good point. I was already hesitating on how to create this TypeVar, but since an
array_like
can be just about anything, it makes better sense to just declare it as_ArrayLike = TypeVar("_ArrayLike")
Best thing is, we can always change this TypeVar if we come to another solution.