Skip to content

Commit 27715ee

Browse files
authored
[mypyc] Don't crash on decorated staticmethod/classmethods (#8122)
1 parent 526a218 commit 27715ee

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

mypyc/genops.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,13 +2151,16 @@ def gen_func_ir(self,
21512151
func_reg = self.instantiate_callable_class(fn_info)
21522152
else:
21532153
assert isinstance(fn_info.fitem, FuncDef)
2154+
func_decl = self.mapper.func_to_decl[fn_info.fitem]
21542155
if fn_info.is_decorated:
21552156
class_name = None if cdef is None else cdef.name
2156-
func_decl = FuncDecl(fn_info.name, class_name, self.module_name, sig)
2157+
func_decl = FuncDecl(fn_info.name, class_name, self.module_name, sig,
2158+
func_decl.kind,
2159+
func_decl.is_prop_getter, func_decl.is_prop_setter)
21572160
func_ir = FuncIR(func_decl, blocks, env, fn_info.fitem.line,
21582161
traceback_name=fn_info.fitem.name)
21592162
else:
2160-
func_ir = FuncIR(self.mapper.func_to_decl[fn_info.fitem], blocks, env,
2163+
func_ir = FuncIR(func_decl, blocks, env,
21612164
fn_info.fitem.line, traceback_name=fn_info.fitem.name)
21622165
return (func_ir, func_reg)
21632166

@@ -3348,7 +3351,12 @@ def translate_method_call(self, expr: CallExpr, callee: MemberExpr) -> Value:
33483351
if self.is_native_ref_expr(callee):
33493352
# Call to module-level native function or such
33503353
return self.translate_call(expr, callee)
3351-
elif isinstance(callee.expr, RefExpr) and callee.expr.node in self.mapper.type_to_ir:
3354+
elif (
3355+
isinstance(callee.expr, RefExpr)
3356+
and isinstance(callee.expr.node, TypeInfo)
3357+
and callee.expr.node in self.mapper.type_to_ir
3358+
and self.mapper.type_to_ir[callee.expr.node].has_method(callee.name)
3359+
):
33523360
# Call a method via the *class*
33533361
assert isinstance(callee.expr.node, TypeInfo)
33543362
ir = self.mapper.type_to_ir[callee.expr.node]

mypyc/test-data/run.test

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3992,9 +3992,13 @@ started
39923992
index
39933993

39943994
[case testDecoratorsMethods]
3995-
from typing import Any, Callable, Iterator
3995+
from typing import Any, Callable, Iterator, TypeVar
39963996
from contextlib import contextmanager
39973997

3998+
T = TypeVar('T')
3999+
def dec(f: T) -> T:
4000+
return f
4001+
39984002
def a(f: Callable[[Any], None]) -> Callable[[Any], None]:
39994003
def g(a: Any) -> None:
40004004
print('Entering')
@@ -4015,6 +4019,22 @@ class A:
40154019
finally:
40164020
print('contextmanager: exited')
40174021

4022+
class Lol:
4023+
@staticmethod
4024+
def foo() -> None:
4025+
Lol.bar()
4026+
Lol.baz()
4027+
4028+
@staticmethod
4029+
@dec
4030+
def bar() -> None:
4031+
pass
4032+
4033+
@classmethod
4034+
@dec
4035+
def baz(cls) -> None:
4036+
pass
4037+
40184038
def inside() -> None:
40194039
with A().generator() as g:
40204040
print('hello!')

0 commit comments

Comments
 (0)