generic subclasss and typevar with constraint
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
Not sure if this is a "bug" or I didn't understand generics, but
import typing
T = typing.TypeVar('T', str, int)
class Base(typing.Generic[T]):
def __init__(self, bar: T):
self.bar: T = bar
class Child(Base[T]):
def __init__(self, bar: T):
super().__init__(bar)
mypy 0.720 (on windows) says
> mypy.exe test\__init__.py
test\__init__.py:15: error: Argument 1 to "__init__" of "Base" has incompatible type "str"; expected "T"
test\__init__.py:15: error: Argument 1 to "__init__" of "Base" has incompatible type "int"; expected "T"
but if I change the T definition to T = typing.TypeVar('T') the errors go away
What I want is to specify that
- the "Base" constructor has a parameter that could be an int or a str
- the "Child" constructor has the same "signature" of the "Base" one (well, this is a simplified example, I want something more...)
is this a bug? di I misinterpreted the TypeVar usage?
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
Reproduce el ejemplo de test_init_.py con mypy 0.720, comparando el TypeVar restringido con la versión sin restricciones. Lee las rutas relevantes de genéricos y de comprobación de TypeVar, y determina después si el diagnóstico indicado es el esperado; el trabajo estará terminado cuando se documente el comportamiento o se añada una prueba de regresión y se corrija si es un bug.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- tooling
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100