python / python/mypy

`safe-super` change breaks Mixin support

Offen
#17,192 3 Kommentare 6 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

We want to indicate that a (super-)class must implement a specific protocol in order to inherit from a specific (sub-) class. We used to annotate self as the super class in the subclass to express this. With mypy 1.10.0 this introduces a safe-super error. Is there currently a way to express this requirement?

To Reproduce

from typing import Protocol


class HasFormValid(Protocol):
    def form_valid(self, form):
        pass


class SaveValidFormMixin:

    def form_valid(self: HasFormValid, form):
        form.save()
        super().form_valid(form)

Expected Behavior

No error: Since we never actually inherit from the HasFormValid protocol, this should be safe.

Actual Behavior

mypy 1.10.0 treats HasFormValid.form_valid as trivial and warns about its usage. In this case, it is assured that HasFormValid.form_valid cannot be actually called via super().form_valid.

mypy <1.10.0 does not raise an error here.

Your Environment

  • Mypy version used: mypy 1.10.0 (compiled: yes)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12.3

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

Beginne damit, das bereitgestellte Mixin- und Protocol-Beispiel mit mypy 1.10.0 zu reproduzieren, und verfolge dann die safe-super-Diagnose und ihre Regressionstests. Ergänze Abdeckung für dieses Mixin-Muster und überprüfe, dass es keinen false positive mehr meldet, während bestehende safe-super-Warnungen weiterhin erhalten bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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