MyPy Incorrectly Flags Module Attribute Inside Format String
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit dem Reproducer in constants.py und main.py und führen Sie python3 -m mypy constants.py main.py mit dem gemeldeten Beispiel aus. Verfolgen Sie, wie der Format-String-Ausdruck analysiert wird, und überprüfen Sie anschließend, dass das Modul-Attribut innerhalb des Format-Strings keinen attr-defined-Fehler mehr erzeugt, während der bestehende direkte Zugriff weiterhin gültig bleibt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 52/100