Skip to content

Fix crash when using decorator in class scope #12724

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
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
6 changes: 3 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4927,15 +4927,15 @@ def _get_node_for_class_scoped_import(
# when it can also be a FuncBase. Once fixed, `f` in the following can be removed.
# See also https://github.com/mypyc/mypyc/issues/892
f = cast(Any, lambda x: x)
if isinstance(f(symbol_node), (FuncBase, Var)):
if isinstance(f(symbol_node), (Decorator, FuncBase, Var)):
# For imports in class scope, we construct a new node to represent the symbol and
# set its `info` attribute to `self.type`.
existing = self.current_symbol_table().get(name)
if (
# The redefinition checks in `add_symbol_table_node` don't work for our
# constructed Var / FuncBase, so check for possible redefinitions here.
existing is not None
and isinstance(f(existing.node), (FuncBase, Var))
and isinstance(f(existing.node), (Decorator, FuncBase, Var))
and (
isinstance(f(existing.type), f(AnyType))
or f(existing.type) == f(symbol_node).type
Expand All @@ -4944,7 +4944,7 @@ def _get_node_for_class_scoped_import(
return existing.node

# Construct the new node
if isinstance(f(symbol_node), FuncBase):
if isinstance(f(symbol_node), (FuncBase, Decorator)):
# In theory we could construct a new node here as well, but in practice
# it doesn't work well, see #12197
typ: Optional[Type] = AnyType(TypeOfAny.from_error)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -7284,3 +7284,19 @@ def meth1(self: Any, y: str) -> str: ...

T = TypeVar("T")
def meth2(self: Any, y: T) -> T: ...

[case testClassScopeImportWithWrapperAndError]
class Foo:
from mod import foo # E: Unsupported class scoped import

[file mod.py]
from typing import Any, Callable, TypeVar

FuncT = TypeVar("FuncT", bound=Callable[..., Any])
def identity_wrapper(func: FuncT) -> FuncT:
return func

@identity_wrapper
def foo(self: Any) -> str:
return ""