python / python/mypy

Ignore `str-format` error when object implements `__format__()` with custom type

Offen
#17,951 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

I have inherited a project, where one class implements __format__(self, format_spec: str) -> str to implement a custom type for formatting. Running mypy on that code throws a false-positive error:

mypy-format.py:5 error: Unsupported format character "l" [str-format]

To Reproduce

class MyType:
    def __format__(self, format_spec: str) -> str:
        return "a" if format_spec == "l" else str(self)

print("{:l}".format(MyType()))

Expected Behavior

For a type implementing __format__ mypy/checkstrformat.py should not output any error.

Actual Behavior

mypy-format.py:5 error: Unsupported format character "l"  [str-format]

Your Environment

  • mypy 1.11.2 (compiled: yes)
  • Python 3.11.2

Links

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

Führe zunächst die bereitgestellte Python-Reproduktion mit mypy 1.11.2 aus und untersuche dann mypy/checkstrformat.py, wo der str-format-Fehler gemeldet wird. Verfolge, wie die format-Methode eines Objekts berücksichtigt wird, und stelle sicher, dass das Beispiel keinen unsupported-format-Fehler erzeugt; bestätige das Verhalten mit der Reproduktion.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

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