python / python/mypy

False negative for invalid abstractmethod implementation

Aperta
#17,710 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
If a subclass of an ABC implements an abstractmethod as an attribute (e.g. not as method or class variable), (and the subclass is instantiated,) then mypy passes. But it will always fail at runtime (as ABCMeta can only detect variables/methods on the class).
There are only some cases where this can happen because implementing a method as an attribute would yield a mypy type error most of the time:

  • an abstract property is implemented as a normal instance attribute
  • an abstract method is implemented as a function instance attribute

Note: these implementations are valid both pragmatically and for type-checking, but ABCMeta is obligated to raise an error because it cannot detect the implementation.

To Reproduce
The following program will pass in mypy but fail at runtime.

from abc import abstractmethod, ABC
from collections.abc import Callable


class Method(ABC):
    @abstractmethod
    def f(self) -> None:
        ...

class ImplMethod(Method):
    f: Callable[[], None]
    def __init__(self):
        self.f = print

ImplMethod()


class Property(ABC):
    @property
    @abstractmethod
    def x(self) -> int:
        ...

class ImplProperty(Property):
    x: int
    def __init__(self):
        self.x = 2

ImplProperty()

Expected Behavior
Error at runtime.
Mypy should error too because the implementations do not satisfy abstractmethod at runtime.

Actual Behavior
Error at runtime.
Mypy success.

Your Environment

  • Mypy version used: latest (1.11.1), with default playground flags
  • Python version used: 3.12.4

Also:
It seems like mypy doesn't complain when using abstractmethod on a non-ABC class. This is intended, right?

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 con la riproduzione Python fornita e ispeziona la gestione di abstractmethod e ABC da parte di mypy; nell’issue non sono nominati file sorgente né test. Il lavoro è completato quando mypy rifiuta le implementazioni degli attributi di istanza mostrate e mantiene il comportamento previsto di abstractmethod sulle classi non-ABC.

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
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.