python / python/mypy

Subclass method parameter names are not checked against superclass

Offen
#6,709 6 Kommentare 8 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature priority-1-normal
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

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

When a subclass renames the parameters of a method, this results in runtime errors; but this seems like something that should be statically detectable.

The below code provides a superclass A, and two similar subclasses B and C that differ in that C renames the parameter to the method f, but B does not.

mypy vs runtime behaviour for variables c and c2 seems inconsistent, while for c3 and c4 it seems ok.

I was expecting some mypy error to be raised when defining f in class C.

class A:
    def f(self, x: int) -> None:
      print("A: ", x)

class B(A):
    def f(self, x: int) -> None:
      print("B: ", x)

class C(A):
    def f(self, n: int) -> None:
      print("C: ", n)

a = A()
a.f(x=3)

b = B()  # type: A
b.f(x=3)

c = C()  # type: A
c.f(x=3)  # mypy passes, but gives runtime error: TypeError: f() got an unexpected keyword argument 'x'

c2 = C()  # type: A
c2.f(n=3) # mypy gives an error:  error: Unexpected keyword argument "n" for "f" of "A"   but runtime works.

c3 = C()  # type: C
c3.f(x=3)    # mypy give type error: error: Unexpected keyword argument "x" for "f" of "C", and runtime error: mypy-issue.py:25: error: Unexpected keyword argument "x" for "f" of "C"

c4 = C()  # type: C
c4.f(n=3)  # no errors in mypy or at runtime
  • What are the versions of mypy and Python you are using?

$ mypy --version
mypy 0.670
$ python --version
Python 3.7.2+

  • Do you see the same issue after installing mypy from Git master?

yes - mypy 0.710+dev.f10528a5a8d9e98939ceb84d40e614d1f159d9a0

  • What are the mypy flags you are using? (For example --strict-optional)

With no flags, and with --strict.

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 mit dem minimalen Beispiel einer Unterklasse im Issue und führe es mit und ohne --strict mit mypy aus, um die inkonsistenten Prüfungen von Schlüsselwortargumenten zu reproduzieren. Verfolge die Behandlung von Überschreibungen und Methodensignaturen, die für die Klasse C verwendet wird; fertig ist die Untersuchung, wenn mypy beim Definieren der Unterklasse die inkompatible Umbenennung eines Parameters meldet und gleichzeitig gültige Aufrufe für kompatible Überschreibungen beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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