Ignore `str-format` error when object implements `__format__()` with custom type
Dieses Issue hat noch niemand übernommen.
- 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
- #17714
- #11723
- How does type-checking work for
datetime.datetime, which implements a rich set of custom formatters? - string - Format Mini Language
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
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