Open
Description
Bug Report
If a name x
appearing in a class def C
is not yet bound, and it is bound in an enclosing function def f
, then mypy resolves x
as f.x
. However, the python doc says:
Class definition blocks ... are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace.
To Reproduce
X.py:
from typing import TYPE_CHECKING
x = 10
class C:
print('C.x', x)
if TYPE_CHECKING: reveal_type(x)
x = "2"
print(' ->', x)
def f() -> None:
x = 20.0
class C:
print('f.C.x', x)
if TYPE_CHECKING: reveal_type(x)
x = "2"
print(' ->', x)
python X.py
C.x 10 -- the global x
-> 2 -- the local x
f.C.x 10 -- the global x
-> 2 -- the local x
mypy X.py
X.py:6:32: note: Revealed type is "builtins.int"
X.py:13:33: note: Revealed type is "builtins.float" -- Wrong. It finds the local f.x!
Expected Behavior
X.py:6:32: note: Revealed type is "builtins.int"
X.py:13:33: note: Revealed type is "builtins.int" -- It finds the global x
Actual Behavior
See above.
Your Environment
- Mypy version used: 0.950
- Python version used: 3.7
Cause of the bug and a fix for it
In SemanticAnalyzer.lookup()
method semanal.py
line 4419:
# 3. Local (function) scopes
for table in reversed(self.locals):
if table is not None and name in table:
return table[name]
This looks in enclosed functions even if in a class definition.
Change this to:
# 3. Local (function) scopes (if within function definition)
if self.is_func_scope():
for table in reversed(self.locals):
if table is not None and name in table:
return table[name]
Result from mypy with this change:
X.py:6:32: note: Revealed type is "builtins.int"
X.py:13:33: note: Revealed type is "builtins.int"