python / python/mypy

"Can only assign concrete classes to a variable of type "Type[MyProtocol]"" is hard to debug

Aperta
#8,836 4 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug diagnostics priority-2-low topic-error-reporting topic-protocols topic-usability
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

If you consider bad error messages a bug, it is a bug. Otherwise, it is a feature request.

The following code sample is wrong because name is not optional. Defining name's value in MyImplementation or its constructor would fix this code.

from typing import Type, Protocol

class MyProtocol(Protocol):
    name: str

class MyImplementation(MyProtocol):
    pass

foo: Type[MyProtocol] = MyImplementation

However, running mypy from master or 0.770 produces an error which is very challenging to debug:

main.py:9: error: Can only assign concrete classes to a variable of type "Type[MyProtocol]"

Ideally, mypy would tell me the underlying problem. In this case, I took nearly a whole day of experimenting to fix it, ultimately needing to delete code until I got the error message to change. It would be ideal if mypy could say "and it is not concrete because name is not defined statically or in __init__." or something.

  • What are the versions of mypy and Python you are using?
    mypy 0.770, python 3.8, yes it persists on master

  • What are the mypy flags you are using? (For example --strict-optional)

This behavior seems unchanged by flags.

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

Riproduci l'esempio in main.py con mypy 0.770 o master, quindi segui la diagnostica che segnala "Can only assign concrete classes to a variable of type Type[MyProtocol]." Il lavoro è completato quando l'errore spiega perché MyImplementation non è concreta, inclusa la definizione del nome mancante descritta nell'issue.

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.