Skip to content

[mypyc] Don't crash on decorated staticmethod/classmethods #8122

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
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions mypyc/genops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2151,13 +2151,16 @@ def gen_func_ir(self,
func_reg = self.instantiate_callable_class(fn_info)
else:
assert isinstance(fn_info.fitem, FuncDef)
func_decl = self.mapper.func_to_decl[fn_info.fitem]
if fn_info.is_decorated:
class_name = None if cdef is None else cdef.name
func_decl = FuncDecl(fn_info.name, class_name, self.module_name, sig)
func_decl = FuncDecl(fn_info.name, class_name, self.module_name, sig,
func_decl.kind,
func_decl.is_prop_getter, func_decl.is_prop_setter)
func_ir = FuncIR(func_decl, blocks, env, fn_info.fitem.line,
traceback_name=fn_info.fitem.name)
else:
func_ir = FuncIR(self.mapper.func_to_decl[fn_info.fitem], blocks, env,
func_ir = FuncIR(func_decl, blocks, env,
fn_info.fitem.line, traceback_name=fn_info.fitem.name)
return (func_ir, func_reg)

Expand Down Expand Up @@ -3348,7 +3351,12 @@ def translate_method_call(self, expr: CallExpr, callee: MemberExpr) -> Value:
if self.is_native_ref_expr(callee):
# Call to module-level native function or such
return self.translate_call(expr, callee)
elif isinstance(callee.expr, RefExpr) and callee.expr.node in self.mapper.type_to_ir:
elif (
isinstance(callee.expr, RefExpr)
and isinstance(callee.expr.node, TypeInfo)
and callee.expr.node in self.mapper.type_to_ir
and self.mapper.type_to_ir[callee.expr.node].has_method(callee.name)
):
# Call a method via the *class*
assert isinstance(callee.expr.node, TypeInfo)
ir = self.mapper.type_to_ir[callee.expr.node]
Expand Down
22 changes: 21 additions & 1 deletion mypyc/test-data/run.test
Original file line number Diff line number Diff line change
Expand Up @@ -3992,9 +3992,13 @@ started
index

[case testDecoratorsMethods]
from typing import Any, Callable, Iterator
from typing import Any, Callable, Iterator, TypeVar
from contextlib import contextmanager

T = TypeVar('T')
def dec(f: T) -> T:
return f

def a(f: Callable[[Any], None]) -> Callable[[Any], None]:
def g(a: Any) -> None:
print('Entering')
Expand All @@ -4015,6 +4019,22 @@ class A:
finally:
print('contextmanager: exited')

class Lol:
@staticmethod
def foo() -> None:
Lol.bar()
Lol.baz()

@staticmethod
@dec
def bar() -> None:
pass

@classmethod
@dec
def baz(cls) -> None:
pass

def inside() -> None:
with A().generator() as g:
print('hello!')
Expand Down