Skip to content

Commit

Permalink
[mypyc] Don't crash on decorated staticmethod/classmethods (#8122)
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan authored Dec 10, 2019
1 parent 526a218 commit 27715ee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
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

0 comments on commit 27715ee

Please sign in to comment.