python / python/mypy

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

Aperta
#17,951 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.