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

Abierto
#8,836 4 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python

Línea de trabajo

Reproduce el ejemplo de main.py con mypy 0.770 o master y, a continuación, sigue el diagnóstico que informa de "Can only assign concrete classes to a variable of type Type[MyProtocol]." Se considera terminado cuando el error explica por qué MyImplementation no es concreta, incluida la definición de nombre que falta descrita en la issue.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug diagnostics priority-2-low topic-error-reporting topic-protocols topic-usability

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.

Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python/mypy

Todos los issues de python/mypy

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.