Name in class def is resolved in enclosing function, instead of global, scope.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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"
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con SemanticAnalyzer.lookup() in semanal.py, in particolare con la ricerca nell'ambito locale descritta nell'issue, e riproduci il comportamento usando l'esempio X.py fornito. Verifica la correzione eseguendo mypy su X.py e confermando che entrambe le chiamate a reveal_type riportino la x globale come builtins.int.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 48/100