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

Offen
#17,091 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Anfängerfreundlichkeit
45/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
devtools

Rechercherichtung

Beginne damit, das Problem mit bar.py und foo.py zu reproduzieren, benenne dann bar.py in bar.pyi um und führe mypy foo.py aus. Untersuche mypy/semanal_classprop.py im Bereich der Zeilen 98–100, um die Behandlung von Stubs zu verstehen. Erledigt ist die Aufgabe, wenn das Stub-basierte Beispiel keinen Fehler wegen eines abstrakten Attributs meldet und damit dem Fall eines regulären Python-Moduls entspricht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.