Skip to content
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
12 changes: 8 additions & 4 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6091,10 +6091,14 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
elif isinstance(base.node, Var) and self.type and self.function_stack:
# check for self.bar or cls.bar in method/classmethod
func_def = self.function_stack[-1]
if not func_def.is_static and isinstance(func_def.type, CallableType):
formal_arg = func_def.type.argument_by_name(base.node.name)
if formal_arg and formal_arg.pos == 0:
type_info = self.type
if (
func_def.has_self_or_cls_argument
and func_def.info is self.type
and isinstance(func_def.type, CallableType)
and func_def.arguments
and base.node is func_def.arguments[0].variable
):
type_info = self.type
elif isinstance(base.node, TypeAlias) and base.node.no_args:
assert isinstance(base.node.target, ProperType)
if isinstance(base.node.target, Instance):
Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -8328,6 +8328,37 @@ reveal_type(Foo.baz) # E: "type[Foo]" has no attribute "baz" \
foo: int
def bar(x: int) -> int: ...

[case testClassScopeImportAccessibleInMethods]
class A:
import mod
from mod import var

def __new__(cls) -> "A":
reveal_type(cls.mod) # N: Revealed type is "types.ModuleType"
reveal_type(cls.var) # N: Revealed type is "builtins.int"
reveal_type(cls.mod.var) # N: Revealed type is "builtins.int"
return cls()

def pos_or_named_self(self) -> None:
reveal_type(self.mod) # N: Revealed type is "types.ModuleType"
reveal_type(self.var) # N: Revealed type is "builtins.int"
reveal_type(self.mod.var) # N: Revealed type is "builtins.int"

def pos_only_self(self, /) -> None:
reveal_type(self.mod) # N: Revealed type is "types.ModuleType"
reveal_type(self.var) # N: Revealed type is "builtins.int"
reveal_type(self.mod.var) # N: Revealed type is "builtins.int"

@classmethod
def clsmethod(cls) -> None:
reveal_type(cls.mod) # N: Revealed type is "types.ModuleType"
reveal_type(cls.var) # N: Revealed type is "builtins.int"
reveal_type(cls.mod.var) # N: Revealed type is "builtins.int"

[file mod.py]
var = 1
[builtins fixtures/module.pyi]

Comment on lines +8331 to +8361
Copy link
Contributor Author

@bzoracler bzoracler Dec 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.mod.var was revealed to be Any in the body of __new__() and pos_only_self() before this PR. See e.g. mypy Playground for a reproducer:

class A:
    import math

    def __new__(cls) -> "A":
        reveal_type(cls.math.pi)  # N: Revealed type is "Any"
        return cls()

    def pos_or_named_self(self) -> None:
        reveal_type(self.math.pi)  # N: Revealed type is "builtins.float"

    def pos_only_self(self, /) -> None:
        reveal_type(self.math.pi)  # N: Revealed type is "Any"

    @classmethod
    def clsmethod(cls) -> None:
        reveal_type(cls.math.pi)  # N: Revealed type is "builtins.float"

[case testClassScopeImportFunctionNested]
class Foo:
class Bar:
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,19 @@ class A:
# type: () -> None
pass

[case testNestedFunctionInMethodCannotAccessEnclosingClassAttributes]
class A:
var: int
import mod
class B: ...

def method(self) -> None:
def f1(arg: object) -> None:
arg.var # E: "object" has no attribute "var"
arg.mod # E: "object" has no attribute "mod"
arg.B # E: "object" has no attribute "B"
[file mod.py]

[case testDeepNestedFunctionWithTooFewArgumentsInTypeComment]
class A:
def f(self):
Expand Down