python / python/mypy

Report protocol problems in no_variant_matches_arguments if possible

Offen
#17,092 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Feature

Report protocol problems in no_variant_matches_arguments if possible.

Pitch

Given following example:

from typing import Protocol, overload


class Foo(Protocol):
    def some_method(self, value: int = 0) -> None: ...


class Bar:
    def some_method(self, value: int) -> None:
        pass


@overload
def accept_foo(value: Foo, v: int) -> None:
    pass


@overload
def accept_foo(value: Foo, v: float) -> None:
    pass


def accept_foo(value: Foo, v: int | float) -> None:
    pass


accept_foo(Bar(), 1)

The Bar has conflict with the Protocol Foo, but currently Mypy won't report the protocol problems. This is the current error report:

protocol_example.py:27: error: No overload variant of "accept_foo" matches argument types "Bar", "int"  [call-overload]
protocol_example.py:27: note: Possible overload variants:
protocol_example.py:27: note:     def accept_foo(value: Foo, v: int) -> None
protocol_example.py:27: note:     def accept_foo(value: Foo, v: float) -> None
Found 1 error in 1 file (checked 1 source file)

If we can report the protocol problems in this case, it may save developers time to figure out what' wrong.

I did a proof-of-concept implementation here: https://github.com/python/mypy/commit/a2040d3680320afbac6e13b3b04593a7965242bc. This is the updated error report:

protocol_example.py:27: error: No overload variant of "accept_foo" matches argument types "Bar", "int"  [call-overload]
protocol_example.py:27: note: Following member(s) of "Bar" have conflicts:
protocol_example.py:27: note:     Expected:
protocol_example.py:27: note:         def some_method(self, value: int = ...) -> None
protocol_example.py:27: note:     Got:
protocol_example.py:27: note:         def some_method(self, value: int) -> None
protocol_example.py:27: note:     Expected:
protocol_example.py:27: note:         def some_method(self, value: int = ...) -> None
protocol_example.py:27: note:     Got:
protocol_example.py:27: note:         def some_method(self, value: int) -> None
protocol_example.py:27: note: Possible overload variants:
protocol_example.py:27: note:     def accept_foo(value: Foo, v: int) -> None
protocol_example.py:27: note:     def accept_foo(value: Foo, v: float) -> None
Found 1 error in 1 file (checked 1 source file)

If this is OK, I'd happy to refine the implementation and submit a PR.

Thanks.

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

Beginnen Sie am Einstiegspunkt no_variant_matches_arguments und prüfen Sie den im Issue verlinkten Proof-of-Concept-Commit. Reproduzieren Sie das bereitgestellte Beispiel zu Protocol und overload, und überprüfen Sie anschließend, dass die Diagnose die konfligierenden Member korrekt meldet, ohne doppelte oder irreführende Ausgabe.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers, devtools
Issue-Typ
Feature
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.