python / python/mypy

inline configurations (mypy comments) in strings still work

Offen
#19,441 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-type-ignore
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

I take it the mypy inline configuration comments are supposed to work like regular hot comments, and not just activate whenever they appear as characters in the source file, such as in a string. However, as I discovered while investigating https://github.com/python/mypy/pull/17875#issuecomment-2939335586, they aren't actually parsed according to the Python syntax rules (because this would be slower and more complicated, at least given our current implementation) and therefore they do work if they're in a string, provided they are at the start of a line.

For example, in the following code, untyped defs are disallowed and an error is raised:

"""
This is just a silly little multi-line string literal, surely it couldn't—
# mypy: disallow-untyped-defs
"""
def foo():
    return 1

Your Environment

  • Mypy version used: this was probably 1.15, but I'd be surprised if it's been fixed by 1.17 (current as of time of writing)

Related

This, #12358, and https://github.com/python/mypy/issues/19366 form a loose family of "things we could easily fix if we had a parser that told us about comments to begin with" ie if https://github.com/python/mypy/pull/17875#issuecomment-2587759851 ¶2 or https://github.com/python/cpython/issues/101494#issuecomment-2395119743 were implemented.

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

Beginne mit dem Reproducer im Issue und lies die verlinkte Dokumentation zur Inline-Konfiguration, Issue #12358, Issue #19366 sowie den referenzierten Pull Request und die CPython-Diskussion. Als erledigt gilt die Aufgabe, wenn ein mypy-Konfigurationskommentar innerhalb eines Strings ignoriert wird, während derselbe Kommentar in tatsächlichen Quellcodekommentaren weiterhin funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

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