More-helpful typing errors with Generics and Callables
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Feature
Taking into consideration the following code (using mypy==1.4.1), the error messages are not very useful in debugging the problem. I have included the Pyright error message for comparison:
"""A test pipeline"""
from __future__ import annotations
from typing import Callable, Generic, TypeVar
from pydantic import BaseModel
from filter_base.filters.base_filter import BaseFilter
ModelT = TypeVar("ModelT", bound=BaseModel)
class TestFilter(BaseFilter, Generic[ModelT]):
"""A filter for loading a JSON file."""
def __init__(self, model_type: type[ModelT]):
"""Test filter."""
self.model_type: type[ModelT] = model_type
def output(self) -> ModelT:
"""Output the loaded model"""
return self.model_type()
def input(self, obj: ModelT) -> None:
"""Input the model"""
pass
T = TypeVar("T")
def connect(
connect_signal: Callable[[], T],
connect_slot: Callable[[T], None],
) -> None:
"""Connect a signal to a slot"""
# TODO: this is a bit silly - we're just immedicately calling the signal/slot
# however - it'll do for the purposes of type checking
connect_slot(connect_signal())
class SimpleModel(BaseModel):
"""A simple model"""
a: int
b: str
class SimpleModel2(BaseModel):
"""A simple model"""
c: int
d: str
def main() -> None:
a = TestFilter(SimpleModel)
b = TestFilter(SimpleModel2)
connect(a.output, b.input)
The diagnostics from mypy and Pyright are as follows (error is on the final line of code):
Diagnostics:
1. mypy: Cannot infer type argument 1 of "connect" [misc]
2. Pyright: Argument of type "(obj: SimpleModel2) -> None" cannot be assigned to parameter "connect_slot" of type "(T@connect) -> None" in function "connect"
Type "(obj: SimpleModel2) -> None" cannot be assigned to type "(SimpleModel) -> None"
Parameter 1: type "SimpleModel" cannot be assigned to type "SimpleModel2"
"SimpleModel" is incompatible with "SimpleModel2" [reportGeneralTypeIssues]
Pitch
Having the error messages of mypy more in-line with Pyright's reporting would mean that it is not essential to use Pyright's typechecking system when developing these sorts of generic systems.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Empieza reproduciendo el ejemplo de Python proporcionado con mypy 1.4.1 y compara su diagnóstico final con la salida de Pyright mostrada en la issue. Se considera terminado cuando mypy explique el argumento Generic Callable incompatible con detalles de tipos igualmente accionables; la issue no indica archivos fuente ni pruebas a las que dirigirse.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 25/100