python / python/mypy

Improve error message on incomplete protocol implementation

Aperta
#15,240 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Merge medio
1g 18h
PR unite (30g)
54

Descrizione

Feature

When a class does not fully implement a protocol, more useful error messages should be printed.

Example 1: When a protocol is used only implicitly, the "incompatible type" [arg-type] message should print which function/property is missing/incompatible. (Similar to the end of Example 2.)

Example 2: When a protocol is used explicitly, an error message should printed near the class definition. not (only) near the instantiation (which can be far away in a different module).

from typing import Callable, Protocol

class Proto(Protocol):
    fun: Callable[[], None]

def proto_fun(proto: Proto) -> None:
    proto.fun()

class Class1:
    pass

# Example 1
# In this case, we get a mypy error
# > Argument 1 to "proto_fun" has incompatible type "Class"; expected "Proto"  [arg-type]
# which is expected both in terms of content and location.
# However, the error message should say why Class and Proto are not compatible.
proto_fun(Class1())

# Example 2
# Here, I would expect a mypy error already, as Class does not fully implement Proto.
# Instead, we do not get any error at all:
class Class2(Proto):
    pass

# At least get a more information error message as in the case above:
# > Cannot instantiate abstract class "Class" with abstract attribute "fun"  [abstract]
proto_fun(Class2())

Output:

bug.py:17: error: Argument 1 to "proto_fun" has incompatible type "Class1"; expected "Proto"  [arg-type]
bug.py:27: error: Cannot instantiate abstract class "Class2" with abstract attribute "fun"  [abstract]
Found 2 errors in 1 file (checked 1 source file)

Expected (just an idea):

bug.py:17: error: Argument 1 to "proto_fun" has incompatible type "Class1"; expected "Proto" which has abstract attribute "fun"  [arg-protocol]
bug.py:22: error: "Class2" does not implement "Proto" which has abstract attribute "fun"  [protocol]
bug.py:27: error: Cannot instantiate abstract class "Class2" with abstract attribute "fun"  [abstract]
Found 3 errors in 1 file (checked 1 source file)

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

Non vengono indicati file sorgente o test. Inizia riproducendo i due esempi di protocollo in bug.py e ispeziona la diagnostica esistente per l’uso implicito ed esplicito dei protocolli; il lavoro è completato quando viene segnalato il membro mancante o incompatibile e viene emesso l’errore di implementazione esplicita vicino a Class2, mantenendo l’errore di istanziazione esistente.

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

Valutazione

Stack tecnologico
python
Ambito
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.