-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Add initial support for compiling singledispatch functions #10753
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
msullivan
merged 14 commits into
python:master
from
pranavrajpal:mypyc-singledispatch-support
Jul 4, 2021
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b6ca92d
Refactor getting arguments into new function
pranavrajpal ad39803
Collect info about singledispatch functions in prebuild pass
pranavrajpal 0955d5b
Name slow isinstance primitive
pranavrajpal 2345bd9
Avoid loading registered impls as decorated functions
pranavrajpal 9381149
Generate IR for singledispatch functions
pranavrajpal aa4b16f
Fix flake8 errors
pranavrajpal d805ce1
Refactor getting singledispatch function from register call
pranavrajpal 6e29a9b
Add support for registering with type annotations
pranavrajpal fa7b202
Change xfail tests to regular tests
pranavrajpal a910c50
Avoid crash on no-argument decorated functions
pranavrajpal 466f80c
Load current function's arguments, not registered function's
pranavrajpal 03fab62
Add 2 xfailed tests
pranavrajpal bc1df27
Remove test with different type annotation and register argument
pranavrajpal 16b9295
Mark keyword arguments test as xfail
pranavrajpal 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
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,7 +1,10 @@ | ||
from typing import Dict, List, Set | ||
from mypy.types import Instance, get_proper_type | ||
from typing import DefaultDict, Dict, List, NamedTuple, Set, Optional, Tuple | ||
from collections import defaultdict | ||
|
||
from mypy.nodes import ( | ||
Decorator, Expression, FuncDef, FuncItem, LambdaExpr, NameExpr, SymbolNode, Var, MemberExpr | ||
Decorator, Expression, FuncDef, FuncItem, LambdaExpr, NameExpr, SymbolNode, Var, MemberExpr, | ||
CallExpr, RefExpr, TypeInfo | ||
) | ||
from mypy.traverser import TraverserVisitor | ||
|
||
|
@@ -50,6 +53,10 @@ def __init__(self) -> None: | |
# Map function to its non-special decorators. | ||
self.funcs_to_decorators: Dict[FuncDef, List[Expression]] = {} | ||
|
||
# Map of main singledispatch function to list of registered implementations | ||
self.singledispatch_impls: DefaultDict[ | ||
FuncDef, List[Tuple[TypeInfo, FuncDef]]] = defaultdict(list) | ||
|
||
def visit_decorator(self, dec: Decorator) -> None: | ||
if dec.decorators: | ||
# Only add the function being decorated if there exist | ||
|
@@ -63,6 +70,20 @@ def visit_decorator(self, dec: Decorator) -> None: | |
# Property setters are not treated as decorated methods. | ||
self.prop_setters.add(dec.func) | ||
else: | ||
removed: List[int] = [] | ||
for i, d in enumerate(dec.decorators): | ||
impl = get_singledispatch_register_call_info(d, dec.func) | ||
if impl is not None: | ||
self.singledispatch_impls[impl.singledispatch_func].append( | ||
(impl.dispatch_type, dec.func)) | ||
removed.append(i) | ||
for i in reversed(removed): | ||
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. I don't think that we should mutate the AST in mypyc. Could you track this in some other way? |
||
del dec.decorators[i] | ||
# if the only decorators are register calls, we shouldn't treat this | ||
# as a decorated function because there aren't any decorators to apply | ||
if not dec.decorators: | ||
return | ||
|
||
self.funcs_to_decorators[dec.func] = dec.decorators | ||
super().visit_decorator(dec) | ||
|
||
|
@@ -141,3 +162,45 @@ def add_free_variable(self, symbol: SymbolNode) -> None: | |
# and mark is as a non-local symbol within that function. | ||
func = self.symbols_to_funcs[symbol] | ||
self.free_variables.setdefault(func, set()).add(symbol) | ||
|
||
|
||
class RegisteredImpl(NamedTuple): | ||
singledispatch_func: FuncDef | ||
dispatch_type: TypeInfo | ||
|
||
|
||
def get_singledispatch_register_call_info(decorator: Expression, func: FuncDef | ||
) -> Optional[RegisteredImpl]: | ||
# @fun.register(complex) | ||
# def g(arg): ... | ||
if (isinstance(decorator, CallExpr) and len(decorator.args) == 1 | ||
and isinstance(decorator.args[0], RefExpr)): | ||
callee = decorator.callee | ||
dispatch_type = decorator.args[0].node | ||
if not isinstance(dispatch_type, TypeInfo): | ||
return None | ||
|
||
if isinstance(callee, MemberExpr): | ||
return registered_impl_from_possible_register_call(callee, dispatch_type) | ||
# @fun.register | ||
# def g(arg: int): ... | ||
elif isinstance(decorator, MemberExpr): | ||
# we don't know if this is a register call yet, so we can't be sure that the function | ||
# actually has arguments | ||
if not func.arguments: | ||
return None | ||
arg_type = get_proper_type(func.arguments[0].variable.type) | ||
if not isinstance(arg_type, Instance): | ||
return None | ||
info = arg_type.type | ||
return registered_impl_from_possible_register_call(decorator, info) | ||
return None | ||
|
||
|
||
def registered_impl_from_possible_register_call(expr: MemberExpr, dispatch_type: TypeInfo | ||
) -> Optional[RegisteredImpl]: | ||
if expr.name == 'register' and isinstance(expr.expr, NameExpr): | ||
node = expr.expr.node | ||
if isinstance(node, Decorator): | ||
return RegisteredImpl(node.func, dispatch_type) | ||
return None |
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
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.
You could also just do
args, arg_kinds, arg_names = get_args(builder, rt_args, line)
, if you prefer.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.
I mainly did that because I didn't want to accidentally mess up the order of the fields in the assignment.