python / python/mypy

MyPy Incorrectly Flags Module Attribute Inside Format String

Offen
#9,646 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
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

  1. Create a file called constants.py with contents below:
FOO = "foo"
  1. Create a file called main.py with contents below:
import constants

def bar():
    # type: () -> str
    print("{foo}".format(foo=constants.FOO))
    return "{constants.FOO}".format(constants=constants)
  1. 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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Ö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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.