Ignore `str-format` error when object implements `__format__()` with custom type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo la riproduzione Python fornita con mypy 1.11.2, quindi esamina mypy/checkstrformat.py, dove viene segnalato l'errore str-format. Segui come viene considerato il metodo format di un oggetto e assicurati che l'esempio non produca alcun errore unsupported-format; conferma il comportamento con la riproduzione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 55/100