python / python/mypy

Mypy Gives `Incompatible Definitions` Error When Dynamically Typed

Offen
#13,362 3 Kommentare 0 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

Discussed in https://github.com/python/typing/discussions/1235

Originally posted by adam-grant-hendry August 4, 2022
Related to this SO question, I'm trying to understand why

but

I would have assumed since nothing is statically typed, that mypy would not produced any errors. In fact, the same code on the pyright playground shows no errors.

I though perhaps the error could be due to the potential for a keyword collision, which I gleaned from PEP 692. e.g. If I made the name of the 4th arguments the same, each could fail the same way if I tried to pass foo as a keyword argument:

>>> a = Foo()
>>> a.run(1, 2, 'baz', foo='bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func() got multiple values for argument 'foo'

but if I keep the argument names different, then the above would work if I tried to call Bar.run(1, 2, 'baz', foo='bar'), but break for Foo.

However, I still would originally assume mypy would do nothing because nothing has been statically typed:

By default, mypy will not type check dynamically typed functions. This means that with a few exceptions, mypy will not report any errors with regular unannotated Python.

To Reproduce

Please see the mypy playground (and pyright playground) gists above.

Expected Behavior

mypy issues no errors.

Actual Behavior

mypy issues the error:

Definition of "run" in base class "Foo" is incompatible with definition in base class "Bar"

Your Environment

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.10, x64-bit
  • Operating system and version: Windows 10, version 20H2

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 den beiden verlinkten mypy-play-Reproduktionen und vergleiche ihr Verhalten mit dem verlinkten Pyright Playground. Konzentriere dich dabei auf die dynamisch typisierten Methodendefinitionen und die hinzugefügten **kwargs. Prüfe die zugehörige Typing-Diskussion und vorhandene mypy-Tests zur Kompatibilität von Methodendefinitionen; abgeschlossen ist die Aufgabe, wenn das Verhalten erklärt ist und, falls es als Fehler bestätigt wird, durch einen Regressionstest abgedeckt ist.

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
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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