python / python/mypy

Mypy false positive for `converter` class attribute in `logging.Formatter` class.

Aperta
#19,241 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-calls
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Mypy false positive: Mypy thinks that setting the converter class attribute for a subclass of logging.Formatter to something like time.gmtime is wrong.

To Reproduce

class UTCFormatter(logging.Formatter):
    """An exact match for the `logging.Formatter` class, except that it logs the time in UTC."""
    converter = time.gmtime

Expected Behavior
No error, since time.gmtime is annotated as def gmtime(seconds: float | None = None, /) -> struct_time: ... and the converter attribute is annotated as converter: Callable[[float | None], struct_time].

Actual Behavior

logger.py:45: error: Incompatible types in assignment (expression has type "Callable[[], struct_time]", base class "Formatter" defined the type as "Callable[[float | None], struct_time]") [assignment]

Your Environment

  • Mypy version used: 1.16.0 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13.4 (on Linux)

By the way, this issue didn't exist in my Python3.12 environment with an older version of mypy installed, but I don't have access to that environment right now so I can't provide which mypy version it had.

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

Non viene indicato alcun file del repository né alcun test. Riproduci l’esempio UTCFormatter con mypy 1.16.0 su Python 3.13.4, quindi individua la gestione delle assegnazioni delle sottoclassi ad attributi di classe chiamabili e aggiungi un test di regressione; il lavoro è completato quando non viene segnalato alcun errore di assegnazione per time.gmtime, mentre le sovrascritture incompatibili continuano a essere diagnosticate.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.