MyPy Incorrectly Flags Module Attribute Inside Format String
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
An unexpected [attr-defined] was reported when accessing module attributes inside of a format string.
To Reproduce
- Create a file called
constants.pywith contents below:
FOO = "foo"
- Create a file called
main.pywith contents below:
import constants
def bar():
# type: () -> str
print("{foo}".format(foo=constants.FOO))
return "{constants.FOO}".format(constants=constants)
- run
python3 -m mypy constants.py main.py
main.py:6: error: Module has no attribute "FOO"
Found 1 error in 1 file (checked 2 source files)
Expected Behavior
I expected the format string "{constants.FOO}".format(constants=constants) to not throw an error, since constants.FOO is defined.
(Write what you thought would happen.)
Actual Behavior
The line print("{foo}".format(foo=constants.FOO)) reports no error (indicating that the value FOO exists in constants, but the line "{constants.FOO}".format(constants=constants) reports an error as if it does not.
Your Environment
- Mypy version used: 0.790
- Mypy command-line flags:
python3 -m mypy constants.py main.py - Mypy configuration options from
mypy.ini(and other config files): No mypy.ini used - Python version used: Python 3.8.5 (brew installed)
- Operating system and version: MacOS Catalina 10.15.6
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 il riproduttore di constants.py e main.py ed esegui python3 -m mypy constants.py main.py usando l’esempio riportato. Traccia il modo in cui viene analizzata l’espressione di formattazione, quindi verifica che l’attributo del modulo all’interno della stringa di formattazione non produca più un errore attr-defined, mentre l’accesso diretto esistente rimanga valido.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 52/100