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.

Valutazione

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

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.

Descrizione

bug

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
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

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.

Altre issue di python/mypy

Tutte le issue di python/mypy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.