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

ENH: add basic typing for np.ufunc #44

Merged
merged 3 commits into from
Apr 16, 2020
Merged

Conversation

person142
Copy link
Member

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.

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.
import numpy as np


def main():
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the script I used to generate the big list of ufuncs. Could ultimately be done as part of a build step.

# int, an int, and a callable, but there's no way to express
# that.
extobj: List[Union[int, Callable]] = ...,
) -> Union[ndarray, generic]: ...
Copy link
Member Author

@person142 person142 Mar 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that after a mypy upgrade to resolve python/typing#253 the union can be removed and replaced with two overloads (one for all scalars and one for array like). On the pinned version of mypy that doesn't work because the signatures have overlapping input types.

person142 added a commit to person142/numpy-stubs that referenced this pull request Mar 26, 2020
The most significant benefit is changes to `@overload` around what
counts as overlapping signatures that could unlock some functionality;
see e.g.

- numpy#44 (comment)
- numpy#44 (comment)
person142 added a commit to person142/numpy-stubs that referenced this pull request Mar 26, 2020
The most significant benefit is changes to `@overload` around what
counts as overlapping signatures that could unlock some functionality;
see e.g.

- numpy#44 (comment)
- numpy#11 (comment)
rgommers pushed a commit that referenced this pull request Mar 29, 2020
The most significant benefit is changes to `@overload` around what
counts as overlapping signatures that could unlock some functionality;
see e.g.

- #44 (comment)
- #11 (comment)
@shoyer shoyer merged commit 8bf2d45 into numpy:master Apr 16, 2020
@shoyer
Copy link
Member

shoyer commented Apr 16, 2020

Thanks!

person142 added a commit to person142/numpy-stubs that referenced this pull request Apr 17, 2020
Follow up to numpy#44.

SciPy actually uses `__name__`, so add it.
@person142 person142 deleted the basic-ufunc branch April 17, 2020 04:11
rgommers pushed a commit that referenced this pull request Apr 17, 2020
Follow up to #44.

SciPy actually uses `__name__`, so add it.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants