Require argument names to match in Protocol
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Feature
Currently mypy does not check that argument names in functions that implement protocols match the argument names in the protocol. This fails when arguments are passed as keywords.
For example, this passes mypy:
from typing import Protocol
class MyProtocol(Protocol):
def foo(self, a: int) -> None: ...
class MyObject:
def foo(self, b: int) -> None:
pass
def call_foo(obj: MyProtocol) -> None:
obj.foo(a=1)
call_foo(MyObject())
but fails at runtime:
Traceback (most recent call last):
File "/Users/sidharthkapur/protocol_test.py", line 13, in <module>
call_foo(MyObject())
File "/Users/sidharthkapur/protocol_test.py", line 11, in call_foo
obj.foo(a=1)
TypeError: MyObject.foo() got an unexpected keyword argument 'a'
Pitch
If we checked this in mypy, we could prevent runtime errors like the one above.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem Protocol- und MyObject-Reproduzierer aus der Issue und lasse ihn durch mypy laufen, um das aktuelle Verhalten zu bestätigen. Verfolge die Kompatibilitätsprüfungen bei der Implementierung von Protocol, und füge Testabdeckung für nicht übereinstimmende Argumentnamen und Schlüsselwortaufrufe hinzu; abgeschlossen ist die Aufgabe, wenn mypy die inkompatible Implementierung ablehnt und gleichzeitig gültige Protocol-Implementierungen weiterhin akzeptiert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100