Skip to content

Commit b1ba661

Browse files
authored
__qualname__ and __module__ are available in class bodies (#16215)
Resolves #10570 Resolves #6473
1 parent d839a0b commit b1ba661

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/semanal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5569,7 +5569,7 @@ def lookup(
55695569
if not suppress_errors:
55705570
self.name_not_defined(name, ctx)
55715571
return None
5572-
# 2. Class attributes (if within class definition)
5572+
# 2a. Class attributes (if within class definition)
55735573
if self.type and not self.is_func_scope() and name in self.type.names:
55745574
node = self.type.names[name]
55755575
if not node.implicit:
@@ -5579,6 +5579,9 @@ def lookup(
55795579
# Defined through self.x assignment
55805580
implicit_name = True
55815581
implicit_node = node
5582+
# 2b. Class attributes __qualname__ and __module__
5583+
if self.type and not self.is_func_scope() and name in {"__qualname__", "__module__"}:
5584+
return SymbolTableNode(MDEF, Var(name, self.str_type()))
55825585
# 3. Local (function) scopes
55835586
for table in reversed(self.locals):
55845587
if table is not None and name in table:

test-data/unit/check-classes.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8001,3 +8001,11 @@ f5(1) # E: Argument 1 to "f5" has incompatible type "int"; expected "Integral"
80018001
# N: Types from "numbers" aren't supported for static type checking \
80028002
# N: See https://peps.python.org/pep-0484/#the-numeric-tower \
80038003
# N: Consider using a protocol instead, such as typing.SupportsFloat
8004+
8005+
[case testImplicitClassScopedNames]
8006+
class C:
8007+
reveal_type(__module__) # N: Revealed type is "builtins.str"
8008+
reveal_type(__qualname__) # N: Revealed type is "builtins.str"
8009+
def f(self) -> None:
8010+
__module__ # E: Name "__module__" is not defined
8011+
__qualname__ # E: Name "__qualname__" is not defined

0 commit comments

Comments
 (0)