Skip to content

Commit 4746519

Browse files
authored
Fix crash when using decorator in class scope (#12724)
Fixes #12474
1 parent 9f8b814 commit 4746519

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

mypy/semanal.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4927,15 +4927,15 @@ def _get_node_for_class_scoped_import(
49274927
# when it can also be a FuncBase. Once fixed, `f` in the following can be removed.
49284928
# See also https://github.com/mypyc/mypyc/issues/892
49294929
f = cast(Any, lambda x: x)
4930-
if isinstance(f(symbol_node), (FuncBase, Var)):
4930+
if isinstance(f(symbol_node), (Decorator, FuncBase, Var)):
49314931
# For imports in class scope, we construct a new node to represent the symbol and
49324932
# set its `info` attribute to `self.type`.
49334933
existing = self.current_symbol_table().get(name)
49344934
if (
49354935
# The redefinition checks in `add_symbol_table_node` don't work for our
49364936
# constructed Var / FuncBase, so check for possible redefinitions here.
49374937
existing is not None
4938-
and isinstance(f(existing.node), (FuncBase, Var))
4938+
and isinstance(f(existing.node), (Decorator, FuncBase, Var))
49394939
and (
49404940
isinstance(f(existing.type), f(AnyType))
49414941
or f(existing.type) == f(symbol_node).type
@@ -4944,7 +4944,7 @@ def _get_node_for_class_scoped_import(
49444944
return existing.node
49454945

49464946
# Construct the new node
4947-
if isinstance(f(symbol_node), FuncBase):
4947+
if isinstance(f(symbol_node), (FuncBase, Decorator)):
49484948
# In theory we could construct a new node here as well, but in practice
49494949
# it doesn't work well, see #12197
49504950
typ: Optional[Type] = AnyType(TypeOfAny.from_error)

test-data/unit/check-classes.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7284,3 +7284,19 @@ def meth1(self: Any, y: str) -> str: ...
72847284

72857285
T = TypeVar("T")
72867286
def meth2(self: Any, y: T) -> T: ...
7287+
7288+
[case testClassScopeImportWithWrapperAndError]
7289+
class Foo:
7290+
from mod import foo # E: Unsupported class scoped import
7291+
7292+
[file mod.py]
7293+
from typing import Any, Callable, TypeVar
7294+
7295+
FuncT = TypeVar("FuncT", bound=Callable[..., Any])
7296+
def identity_wrapper(func: FuncT) -> FuncT:
7297+
return func
7298+
7299+
@identity_wrapper
7300+
def foo(self: Any) -> str:
7301+
return ""
7302+

0 commit comments

Comments
 (0)