python / python/mypy

Report protocol problems in no_variant_matches_arguments if possible

Aperta
#17,092 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal punto di ingresso no_variant_matches_arguments e rivedi il commit di prova di concetto collegato nell'issue. Riproduci l'esempio fornito di Protocol e overload, quindi verifica che la diagnostica riporti accuratamente i membri in conflitto, senza output duplicato o fuorviante.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers, devtools
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.