python / python/mypy

Mypy does not honor implicitly abstract class in stubs; false positive about ABCMeta

Aperta
#17,091 1 commento 2 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

When analyzing stub files, mypy issues false positives when an implicitly abstract class inherits from abc.ABC rather than setting metaclass=ABCMeta:

To Reproduce

Have an implicitly abstract class in a file.

# bar.py
import abc
class A(abc.ABC):  # same behavior as if using metaclass=abc.ABCMeta here
    @abc.abstractmethod
    def meth(self) -> None:
        pass

class B(A):  # at runtime B.__class__ shows the metaclass is abc.ABCMeta
    pass

Import and subclass the implicitly abstract class

# foo.py
import bar

class C(bar.B):
    def meth(self) -> None:
        pass
$ mypy foo.py 
Success: no issues found in 1 source file

Rename the imported module so it's a stub.

$ mv bar.py bar.pyi

Expected Behavior

$ mypy foo.py 
Success: no issues found in 1 source file

Actual Behavior

$ mypy foo.py 
bar.pyi:10: error: Class bar.B has abstract attributes "meth"  [misc]
bar.pyi:10: note: If it is meant to be abstract, add 'abc.ABCMeta' as an explicit metaclass
Found 1 error in 1 file (checked 1 source file)

Mypy should emulate the runtime behavior and understand that B.__class__ is always going to be abc.ABCMeta.
Seems the bug is here: https://github.com/python/mypy/blob/master/mypy/semanal_classprop.py#L98-L100

Your Environment

  • Mypy version used: 1.9
  • Python version used: python 3.11

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 riproducendo il problema con bar.py e foo.py, poi rinomina bar.py in bar.pyi ed esegui mypy foo.py. Esamina mypy/semanal_classprop.py intorno alle righe 98-100 per capire la gestione degli stub. Il lavoro è completato quando l'esempio basato sugli stub non segnala alcun errore di attributo astratto, come nel caso di un normale modulo Python.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.