This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: add basic typing for
np.ufunc
(#44)
* ENH: add basic typing for `np.ufunc` This adds basic type hints for `np.ufunc` and types all ufuncs in the top-level namespace as `np.ufunc`. Ufuncs are highly dynamic, e.g. - Their call signatures can vary - The `reduce`/`accumulate`/`reduceat`/`outer`/`at` methods may or may not always raise so it is difficult to have precise types. A path forward would be writing a mypy plugin to get more precise typing. * MAINT: fix return type for `ufunc.__call__` Co-authored-by: Stephan Hoyer <shoyer@gmail.com>
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -4,4 +4,5 @@ __pycache__ | |
numpy_stubs.egg-info/ | ||
venv | ||
.idea | ||
*~ | ||
**~ |
This file contains 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 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,21 @@ | ||
import numpy as np | ||
|
||
|
||
def main(): | ||
ufuncs = [] | ||
for obj_name in np.__dir__(): | ||
obj = getattr(np, obj_name) | ||
if isinstance(obj, np.ufunc): | ||
ufuncs.append(obj) | ||
|
||
ufunc_stubs = [] | ||
for ufunc in set(ufuncs): | ||
ufunc_stubs.append(f'{ufunc.__name__}: ufunc') | ||
ufunc_stubs.sort() | ||
|
||
for stub in ufunc_stubs: | ||
print(stub) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains 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,5 @@ | ||
import numpy as np | ||
|
||
np.sin.nin + 'foo' # E: Unsupported operand types | ||
np.sin(1, foo='bar') # E: Unexpected keyword argument | ||
np.sin(1, extobj=['foo', 'foo', 'foo']) # E: incompatible type |
This file contains 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,14 @@ | ||
import numpy as np | ||
|
||
np.sin(1) | ||
np.sin([1, 2, 3]) | ||
np.sin(1, out=np.empty(1)) | ||
np.matmul( | ||
np.ones((2, 2, 2)), | ||
np.ones((2, 2, 2)), | ||
axes=[(0, 1), (0, 1), (0, 1)], | ||
) | ||
np.sin(1, signature='D') | ||
np.sin(1, extobj=[16, 1, lambda: None]) | ||
np.sin(1) + np.sin(1) | ||
np.sin.types[0] |