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 32
Define special methods for ndarray and add more extensive tests. #10
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
46519eb
Define a special methods for ndarray and add more extensive tests.
shoyer 105e699
Disable failing test
shoyer 134bd7a
Remove SupportsRound
shoyer 878545a
Don't define removed special methods on Python 3
shoyer 8c03e56
Add _DtypeLike
shoyer c17efdb
Tests for arithmetic
shoyer 5430fb7
Grammar
shoyer e9e9523
Adjust version checking
shoyer 301161c
Update mypy requirement and ncomment divmod test
shoyer e3d5cdd
Add __bool__, update comments
shoyer 233ff66
Add None to DtypeLike
shoyer 40847a4
Remove _ConvertibleToDtype in favor of _DtypeLike
shoyer ff9dd2a
Add comments for _DtypeLike
shoyer 9ac0ddc
Add _DtypeLikeNested
shoyer 6297521
Replace Dict[str, Any] in _DtypeLike and add missing ndarray methods
shoyer 087f725
unicode -> Text
shoyer c6e6e95
Convert dtype, shape and strides into properties with setters
shoyer 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
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,3 +1,3 @@ | ||
flake8==3.3.0 | ||
|
||
mypy==0.560.0 | ||
mypy==0.570.0 |
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,5 +1,134 @@ | ||
"""Simple expression that should pass with mypy.""" | ||
import operator | ||
|
||
import numpy as np | ||
from typing import Iterable | ||
|
||
# Basic checks | ||
array = np.array([1, 2]) | ||
def ndarray_func(x: np.ndarray) -> np.ndarray: | ||
return x | ||
ndarray_func(np.array([1, 2])) | ||
array == 1 | ||
array.dtype == float | ||
|
||
# Dtype construction | ||
np.dtype(float) | ||
np.dtype(np.float64) | ||
np.dtype(None) | ||
np.dtype('float64') | ||
np.dtype(np.dtype(float)) | ||
np.dtype(('U', 10)) | ||
np.dtype((np.int32, (2, 2))) | ||
np.dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')]) | ||
np.dtype([('R', 'u1', 1)]) | ||
np.dtype([('R', 'u1', (2, 2))]) | ||
np.dtype({'col1': ('U10', 0), 'col2': ('float32', 10)}) | ||
np.dtype((np.int32, {'real': (np.int16, 0), 'imag': (np.int16, 2)})) | ||
np.dtype((np.int32, (np.int8, 4))) | ||
|
||
# Iteration and indexing | ||
def iterable_func(x: Iterable) -> Iterable: | ||
return x | ||
iterable_func(array) | ||
[element for element in array] | ||
iter(array) | ||
zip(array, array) | ||
array[1] | ||
array[:] | ||
array[...] | ||
array[:] = 0 | ||
|
||
array_2d = np.ones((3, 3)) | ||
array_2d[:2, :2] | ||
array_2d[..., 0] | ||
array_2d[:2, :2] = 0 | ||
|
||
# Other special methods | ||
len(array) | ||
str(array) | ||
array_scalar = np.array(1) | ||
int(array_scalar) | ||
float(array_scalar) | ||
# currently does not work due to https://github.com/python/typeshed/issues/1904 | ||
# complex(array_scalar) | ||
bytes(array_scalar) | ||
operator.index(array_scalar) | ||
bool(array_scalar) | ||
|
||
# comparisons | ||
array < 1 | ||
array <= 1 | ||
array == 1 | ||
array != 1 | ||
array > 1 | ||
array >= 1 | ||
1 < array | ||
1 <= array | ||
1 == array | ||
1 != array | ||
1 > array | ||
1 >= array | ||
|
||
# binary arithmetic | ||
array + 1 | ||
1 + array | ||
array += 1 | ||
|
||
array - 1 | ||
1 - array | ||
array -= 1 | ||
|
||
array * 1 | ||
1 * array | ||
array *= 1 | ||
|
||
array / 1 | ||
1 / array | ||
array /= 1 | ||
|
||
array // 1 | ||
1 // array | ||
array //= 1 | ||
|
||
array % 1 | ||
1 % array | ||
array %= 1 | ||
|
||
divmod(array, 1) | ||
divmod(1, array) | ||
|
||
array ** 1 | ||
1 ** array | ||
array **= 1 | ||
|
||
array << 1 | ||
1 << array | ||
array <<= 1 | ||
|
||
array >> 1 | ||
1 >> array | ||
array >>= 1 | ||
|
||
array & 1 | ||
1 & array | ||
array &= 1 | ||
|
||
array ^ 1 | ||
1 ^ array | ||
array ^= 1 | ||
|
||
array | 1 | ||
1 | array | ||
array |= 1 | ||
|
||
array @ array | ||
|
||
def foo(a: np.ndarray): pass | ||
# unary arithmetic | ||
-array | ||
+array | ||
abs(array) | ||
~array | ||
|
||
foo(np.array(1)) | ||
# Other methods | ||
np.array([1, 2]).transpose() |
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.
Missing
__setattr__
for.dtype
and.shape
?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.
dtype and shape are only typed as attributes, not properties, which means they can be set. But perhaps it would indeed be good to overload setters appropriately...