-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
MAINT: Update the annotations in np.core.numeric
#17564
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,45 @@ | ||
from typing import Any, Optional, Union, Sequence, Tuple | ||
import sys | ||
from typing import ( | ||
Any, | ||
Optional, | ||
Union, | ||
Sequence, | ||
Tuple, | ||
Callable, | ||
List, | ||
overload, | ||
TypeVar, | ||
Iterable, | ||
) | ||
|
||
from numpy import ndarray, dtype, bool_, _OrderKACF, _OrderCF | ||
from numpy import ndarray, generic, dtype, bool_, int32, int64, _OrderKACF, _OrderCF | ||
from numpy.typing import ArrayLike, DtypeLike, _ShapeLike | ||
|
||
if sys.version_info >= (3, 8): | ||
from typing import Literal | ||
else: | ||
from typing_extensions import Literal | ||
|
||
_T = TypeVar("_T") | ||
_ArrayType = TypeVar("_ArrayType", bound=ndarray) | ||
|
||
_CorrelateMode = Literal["valid", "same", "full"] | ||
|
||
@overload | ||
def zeros_like( | ||
a: _ArrayType, | ||
dtype: None = ..., | ||
order: _OrderKACF = ..., | ||
subok: Literal[True] = ..., | ||
shape: None = ..., | ||
) -> _ArrayType: ... | ||
@overload | ||
def zeros_like( | ||
a: ArrayLike, | ||
dtype: DtypeLike = ..., | ||
order: _OrderKACF = ..., | ||
subok: bool = ..., | ||
shape: Optional[Union[int, Sequence[int]]] = ..., | ||
shape: Optional[_ShapeLike] = ..., | ||
) -> ndarray: ... | ||
def ones( | ||
shape: _ShapeLike, | ||
|
@@ -17,13 +48,31 @@ def ones( | |
*, | ||
like: ArrayLike = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def ones_like( | ||
a: _ArrayType, | ||
dtype: None = ..., | ||
order: _OrderKACF = ..., | ||
subok: Literal[True] = ..., | ||
shape: None = ..., | ||
) -> _ArrayType: ... | ||
@overload | ||
def ones_like( | ||
a: ArrayLike, | ||
dtype: DtypeLike = ..., | ||
order: _OrderKACF = ..., | ||
subok: bool = ..., | ||
shape: Optional[_ShapeLike] = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def empty_like( | ||
a: _ArrayType, | ||
dtype: None = ..., | ||
order: _OrderKACF = ..., | ||
subok: Literal[True] = ..., | ||
shape: None = ..., | ||
) -> _ArrayType: ... | ||
@overload | ||
def empty_like( | ||
a: ArrayLike, | ||
dtype: DtypeLike = ..., | ||
|
@@ -39,6 +88,16 @@ def full( | |
*, | ||
like: ArrayLike = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def full_like( | ||
a: _ArrayType, | ||
fill_value: Any, | ||
dtype: None = ..., | ||
order: _OrderKACF = ..., | ||
subok: Literal[True] = ..., | ||
shape: None = ..., | ||
) -> _ArrayType: ... | ||
@overload | ||
def full_like( | ||
a: ArrayLike, | ||
fill_value: Any, | ||
|
@@ -47,35 +106,38 @@ def full_like( | |
subok: bool = ..., | ||
shape: Optional[_ShapeLike] = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def count_nonzero( | ||
a: ArrayLike, axis: None = ..., *, keepdims: Literal[False] = ... | ||
) -> int: ... | ||
@overload | ||
def count_nonzero( | ||
a: ArrayLike, axis: Optional[Union[int, Tuple[int], Tuple[int, int]]] = ... | ||
) -> Union[int, ndarray]: ... | ||
def isfortran(a: ndarray) -> bool: ... | ||
a: ArrayLike, axis: _ShapeLike = ..., *, keepdims: bool = ... | ||
) -> Union[int64, int32, ndarray]: ... | ||
def isfortran(a: Union[ndarray, generic]) -> bool: ... | ||
def argwhere(a: ArrayLike) -> ndarray: ... | ||
def flatnonzero(a: ArrayLike) -> ndarray: ... | ||
|
||
_CorrelateMode = Literal["valid", "same", "full"] | ||
|
||
def correlate(a: ArrayLike, v: ArrayLike, mode: _CorrelateMode = ...) -> ndarray: ... | ||
def convolve(a: ArrayLike, v: ArrayLike, mode: _CorrelateMode = ...) -> ndarray: ... | ||
def outer(a: ArrayLike, b: ArrayLike, out: ndarray = ...) -> ndarray: ... | ||
@overload | ||
def outer(a: ArrayLike, b: ArrayLike, out: None = ...) -> ndarray: ... | ||
@overload | ||
def outer(a: ArrayLike, b: ArrayLike, out: _ArrayType = ...) -> _ArrayType: ... | ||
def tensordot( | ||
a: ArrayLike, | ||
b: ArrayLike, | ||
axes: Union[ | ||
int, Tuple[int, int], Tuple[Tuple[int, int], ...], Tuple[List[int, int], ...] | ||
] = ..., | ||
axes: Union[int, Tuple[_ShapeLike, _ShapeLike]] = ..., | ||
) -> ndarray: ... | ||
def roll( | ||
a: ArrayLike, | ||
shift: Union[int, Tuple[int, ...]], | ||
axis: Optional[Union[int, Tuple[int, ...]]] = ..., | ||
shift: _ShapeLike, | ||
axis: Optional[_ShapeLike] = ..., | ||
) -> ndarray: ... | ||
def rollaxis(a: ArrayLike, axis: int, start: int = ...) -> ndarray: ... | ||
def rollaxis(a: ndarray, axis: int, start: int = ...) -> ndarray: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned in the docs: |
||
def moveaxis( | ||
a: ndarray, | ||
source: Union[int, Sequence[int]], | ||
destination: Union[int, Sequence[int]], | ||
source: _ShapeLike, | ||
destination: _ShapeLike, | ||
) -> ndarray: ... | ||
def cross( | ||
a: ArrayLike, | ||
|
@@ -85,16 +147,26 @@ def cross( | |
axisc: int = ..., | ||
axis: Optional[int] = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def indices( | ||
dimensions: Sequence[int], dtype: dtype = ..., sparse: bool = ... | ||
) -> Union[ndarray, Tuple[ndarray, ...]]: ... | ||
dimensions: Sequence[int], | ||
dtype: DtypeLike = ..., | ||
sparse: Literal[False] = ..., | ||
) -> ndarray: ... | ||
@overload | ||
def indices( | ||
dimensions: Sequence[int], | ||
dtype: DtypeLike = ..., | ||
sparse: Literal[True] = ..., | ||
) -> Tuple[ndarray, ...]: ... | ||
def fromfunction( | ||
function: Callable, | ||
shape: Tuple[int, int], | ||
function: Callable[..., _T], | ||
shape: Sequence[int], | ||
*, | ||
dtype: DtypeLike = ..., | ||
like: ArrayLike = ..., | ||
**kwargs, | ||
) -> Any: ... | ||
**kwargs: Any, | ||
) -> _T: ... | ||
def isscalar(element: Any) -> bool: ... | ||
def binary_repr(num: int, width: Optional[int] = ...) -> str: ... | ||
def base_repr(number: int, base: int = ..., padding: int = ...) -> str: ... | ||
|
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" | ||
Tests for :mod:`numpy.core.numeric`. | ||
|
||
Does not include tests which fall under ``array_constructors``. | ||
|
||
""" | ||
|
||
from typing import List | ||
import numpy as np | ||
|
||
class SubClass(np.ndarray): | ||
... | ||
|
||
i8 = np.int64(1) | ||
|
||
A = np.arange(27).reshape(3, 3, 3) | ||
B: List[List[List[int]]] = A.tolist() | ||
C = np.empty((27, 27)).view(SubClass) | ||
|
||
np.count_nonzero(i8) | ||
np.count_nonzero(A) | ||
np.count_nonzero(B) | ||
np.count_nonzero(A, keepdims=True) | ||
np.count_nonzero(A, axis=0) | ||
|
||
np.isfortran(i8) | ||
np.isfortran(A) | ||
|
||
np.argwhere(i8) | ||
np.argwhere(A) | ||
|
||
np.flatnonzero(i8) | ||
np.flatnonzero(A) | ||
|
||
np.correlate(B[0][0], A.ravel(), mode="valid") | ||
np.correlate(A.ravel(), A.ravel(), mode="same") | ||
|
||
np.convolve(B[0][0], A.ravel(), mode="valid") | ||
np.convolve(A.ravel(), A.ravel(), mode="same") | ||
|
||
np.outer(i8, A) | ||
np.outer(B, A) | ||
np.outer(A, A) | ||
np.outer(A, A, out=C) | ||
|
||
np.tensordot(B, A) | ||
np.tensordot(A, A) | ||
np.tensordot(A, A, axes=0) | ||
np.tensordot(A, A, axes=(0, 1)) | ||
|
||
np.isscalar(i8) | ||
np.isscalar(A) | ||
np.isscalar(B) | ||
|
||
np.roll(A, 1) | ||
np.roll(A, (1, 2)) | ||
np.roll(B, 1) | ||
|
||
np.rollaxis(A, 0, 1) | ||
|
||
np.moveaxis(A, 0, 1) | ||
np.moveaxis(A, (0, 1), (1, 2)) | ||
|
||
np.cross(B, A) | ||
np.cross(A, A) | ||
|
||
np.indices([0, 1, 2]) | ||
np.indices([0, 1, 2], sparse=False) | ||
np.indices([0, 1, 2], sparse=True) | ||
|
||
np.binary_repr(1) | ||
|
||
np.base_repr(1) | ||
|
||
np.allclose(i8, A) | ||
np.allclose(B, A) | ||
np.allclose(A, A) | ||
|
||
np.isclose(i8, A) | ||
np.isclose(B, A) | ||
np.isclose(A, A) | ||
|
||
np.array_equal(i8, A) | ||
np.array_equal(B, A) | ||
np.array_equal(A, A) | ||
|
||
np.array_equiv(i8, A) | ||
np.array_equiv(B, A) | ||
np.array_equiv(A, A) |
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
Oops, something went wrong.
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.
A bit odd that one overload returns a builtin
int
and the other one a numpysignedinteger
(or an array).See #17562.