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
ENH: add mypy plugin for more precise ufunc signatures #56
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e8f6627
ENH: add mypy plugin for more precise ufunc signatures
person142 5a8eb75
Infer ufunc arity from the ufunc object itself
person142 463739c
Abuse literals to allow inferring ufunc arity from Generic
person142 b583871
Also support modifying the return type of the ufunc
person142 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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from mypy.nodes import ARG_POS | ||
from mypy.plugin import Plugin | ||
import mypy.types | ||
from mypy.types import CallableType, LiteralType, TupleType, UnionType | ||
|
||
|
||
def ufunc_call_hook(ctx): | ||
nin_arg, nout_arg = ctx.type.args | ||
if not isinstance(nin_arg, LiteralType): | ||
# Not a literal; we can't make the signature any more precise. | ||
return ctx.default_signature | ||
if not isinstance(nout_arg, LiteralType): | ||
return ctx.default_signature | ||
nin, nout = nin_arg.value, nout_arg.value | ||
|
||
# Strip off *args and replace it with the correct number of | ||
# positional arguments. | ||
arg_kinds = [ARG_POS] * nin + ctx.default_signature.arg_kinds[1:] | ||
arg_names = ( | ||
[f'x{i}' for i in range(nin)] + | ||
ctx.default_signature.arg_names[1:] | ||
) | ||
arg_types = ( | ||
[ctx.default_signature.arg_types[0]] * nin + | ||
ctx.default_signature.arg_types[1:] | ||
) | ||
ndarray_type, generic_type, _ = ctx.default_signature.ret_type.items | ||
scalar_or_ndarray = UnionType([ndarray_type, generic_type]) | ||
if nout == 1: | ||
ret_type = scalar_or_ndarray | ||
else: | ||
ret_type = TupleType([scalar_or_ndarray] * nout) | ||
|
||
return ctx.default_signature.copy_modified( | ||
arg_kinds=arg_kinds, | ||
arg_names=arg_names, | ||
arg_types=arg_types, | ||
ret_type=ret_type, | ||
) | ||
|
||
|
||
class UFuncPlugin(Plugin): | ||
def get_method_signature_hook(self, method): | ||
if method == 'numpy.ufunc.__call__': | ||
return ufunc_call_hook | ||
|
||
|
||
def plugin(version): | ||
return UFuncPlugin |
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
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,2 @@ | ||
[mypy] | ||
plugins = numpy_ufuncs_plugin |
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,6 @@ | ||
import numpy as np | ||
|
||
reveal_type(np.sin(1)) # E: Union[numpy.ndarray, numpy.generic] | ||
reveal_type(np.sin([1, 2, 3])) # E: Union[numpy.ndarray, numpy.generic] | ||
reveal_type(np.sin.nin) # E: Literal[1] | ||
reveal_type(np.sin.nout) # E: Literal[1] |
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
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.
The punchline: we statically know that sin takes one argument.