python / python/mypy

Inconsistent errors with superclass Liskov violations

Offen
#7,336 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug needs discussion priority-1-normal topic-inheritance
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

  • Are you reporting a bug, or opening a feature request?

Bug report

class ParentA:
    def get_description(self, s: str): ...


class ParentB:
    def get_description(self, i: int): ...


# This is a Liskov violation and needs to be ignored.
class ParentViolation(ParentA, ParentB):  # type: ignore
    ...


class ShouldBeIrrelevant:
    ...


# Inheriting from a violating parent, showing no error
class NoViolation(ParentViolation):
    ...


# Inheriting from a violating parent, but an extra class makes a violation appear
class SpuriousViolation(ParentViolation, ShouldBeIrrelevant):
    ...
  • What is the actual behavior/output?

The NoViolation class line has no errors.
The SpuriousViolation class line has an error of:
error: Definition of "get_description" in base class "ParentA" is incompatible with definition in base class "ParentB"

  • What is the behavior/output you expect?

Either they should both error, or neither should. Personally, I would prefer it if neither errored, since the Liskov violation has already been explicitly ignored by the ParentViolation class. However, I also don't have enough context to know whether this is actually the correct solution.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy 0.720, and verified with mypy 0.730+dev.17a93eac7d1d78e31fc9b347682d86d8d53fe02b

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führen Sie das bereitgestellte Python-Snippet mit mypy 0.730+dev aus und vergleichen Sie die Diagnosen in den Klassenzeilen NoViolation und SpuriousViolation. Verfolgen Sie, wie der Checker mit der ignorierten ParentViolation und den geerbten Definitionen der Superklasse umgeht; das Issue ist abgeschlossen, wenn beide Fälle konsistent behandelt werden und ein Regressionstest die beiden Beispiele abdeckt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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