spurious used-before-def on admittedly odd wildcard import
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
When using wildcard imports and modifying the import module-level variables, mypy will detect a undef-before-use of imported module-level variables. This is an admittedly weird bit of code, but crops up, for example, in some kinds of Django settings.py refactorings people do.
To Reproduce
This bug unfortunately requires two files to reproduce, so I have to inline them here.
Executing last.py will get you correct output, but mypy will claim there's a undef-before-use.
# first.py
A_VALUE: str = "in first"
# last.py
from first import *
# from first import A_VALUE
A_VALUE += " in last" # comment either this, or the next line, or uncomment the import above for the mypy check to pass
A_VALUE = "in last again " + A_VALUE
if __name__ == "__main__":
print(A_VALUE)
Expected Behavior
The expected behavior would be a successful pass, I believe.
Actual Behavior
Running PYTHONPATH=. mypy last.py will get you:
last.py:3: error: Name "A_VALUE" is used before definition [used-before-def]
Found 1 error in 1 file (checked 1 source file)
For context, running PYTHONPATH=. python3 last.py will show the expected output of in last again in first in last.
Interestingly, both A_VALUE updates in last.py are necessary for mypy to call out a used-before-def. And if you explicitly import the module-level variable, mypy passes successfully, too. We've seen this behavior with a .index and a += method call on a List before a =, so we don't believe it's something specific to str or +=.
Your Environment
- Mypy version used: 1.0.0
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.10.9 and 3.8.14
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
Riproduci la segnalazione con first.py e last.py usando PYTHONPATH=. mypy last.py, quindi confronta l’importazione wildcard con l’importazione esplicita e con la rimozione di uno dei due aggiornamenti. Traccia il percorso con cui i nomi delle importazioni wildcard arrivano al controllo di uso prima della definizione; il lavoro è completato quando l’esempio a due file passa senza sopprimere la diagnostica, preservando il comportamento esistente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers, devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100