Skip to content

Commit

Permalink
__qualname__ and __module__ are available in class bodies (#16215)
Browse files Browse the repository at this point in the history
Resolves #10570
Resolves #6473
  • Loading branch information
asottile authored Oct 4, 2023
1 parent d839a0b commit b1ba661
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -5569,7 +5569,7 @@ def lookup(
if not suppress_errors:
self.name_not_defined(name, ctx)
return None
# 2. Class attributes (if within class definition)
# 2a. Class attributes (if within class definition)
if self.type and not self.is_func_scope() and name in self.type.names:
node = self.type.names[name]
if not node.implicit:
Expand All @@ -5579,6 +5579,9 @@ def lookup(
# Defined through self.x assignment
implicit_name = True
implicit_node = node
# 2b. Class attributes __qualname__ and __module__
if self.type and not self.is_func_scope() and name in {"__qualname__", "__module__"}:
return SymbolTableNode(MDEF, Var(name, self.str_type()))
# 3. Local (function) scopes
for table in reversed(self.locals):
if table is not None and name in table:
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -8001,3 +8001,11 @@ f5(1) # E: Argument 1 to "f5" has incompatible type "int"; expected "Integral"
# N: Types from "numbers" aren't supported for static type checking \
# N: See https://peps.python.org/pep-0484/#the-numeric-tower \
# N: Consider using a protocol instead, such as typing.SupportsFloat

[case testImplicitClassScopedNames]
class C:
reveal_type(__module__) # N: Revealed type is "builtins.str"
reveal_type(__qualname__) # N: Revealed type is "builtins.str"
def f(self) -> None:
__module__ # E: Name "__module__" is not defined
__qualname__ # E: Name "__qualname__" is not defined

0 comments on commit b1ba661

Please sign in to comment.