Does not catch variable declared global/nonlocal after appearing in the same 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 program references a variable and then declares it global or nonlocal, mypy does not catch this.
To Reproduce
foo.py:
x = 'x'
from typing import TYPE_CHECKING
def foo() -> None:
x = 42
global x
if TYPE_CHECKING: reveal_type(x)
print(x)
foo()
$ mypy foo.py
foo.py:8: note: Revealed type is 'builtins.str'
$ python3 foo.py
File "foo.py", line 7
global x
^
SyntaxError: name 'x' is assigned to before global declaration
Expected Behavior
mypy should report the error on line 7.
This applies to any other usage of 'x',
- except
(x): int(with no assignment)
which is not a syntax error, as it is not a name binding for 'x'.
When you fix mypy to catch these syntax errors, be sure and take this into account. The only difference between(x): intandx: intis in the value ofast3.AnnAssign.simple= 0 and 1, resp.ast3.AnnAssign.simple= 0 in both cases if there is an assignment.
Actual Behavior
No error (see above).
Suggestion
I would guess that the fix belongs in the semantic analyzer. When it encounters a global or nonlocal declaration, it should know that the variable name has already been used or bound.
Don't forget to check for ast3.AnnAssign.simple = 1 and not consider the variable to be bound, or even used, in this case.
Note that ast3.AnnAssign.target is an ast3.Name instance, and ast3.AnnAssign.target.expr_context is an ast3.Store instance. This is a bug in the parser. It should be ast3.Load(), or better yet, None, since the variable is not really used at all.
Your Environment
- Mypy version used: 0.761
- Python version used: 3.8.10
- Operating system and version: Ubuntu (WSL on Windows)
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 nell’analizzatore semantico di mypy e riproduci il problema con l’esempio foo.py del report. Traccia la gestione delle dichiarazioni global e nonlocal, inclusi i casi ast3.AnnAssign.simple, e verifica che x non venga trattato erroneamente come usato o associato per x: int senza assegnazione. Il lavoro è completato quando mypy segnala l’errore di sintassi preservando l’eccezione indicata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100