Associated types on generics
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
-
Request Type: Feature Request
-
Mock-up repro (more detailed mock-up here):
import abc import typing # Define two base types `ABase` & `BBase` with a similar interface, # that each return a different object type `AValue` & `BValue` from # some method # # The object type is also reexposed as a class attribute on the # respective base type for easier access. class AValue: value: bytes def __init__(self, value: bytes): self.value = value class ABase(metaclass=abc.ABCMeta): VALUE_T = AValue @abc.abstractmethod def get_some_value(self) -> AValue: ... class BValue: value: str def __init__(self, value: str): self.value = value class BBase(metaclass=abc.ABCMeta): VALUE_T = BValue @abc.abstractmethod def get_some_value(self) -> BValue: ... # Define generic type over the two defined above AnyBase = typing.TypeVar("AnyBase", ABase, BBase) # # HOW WOULD I NAME THE RETURN TYPE IN THE NEXT LINE? # # In particular note that the return type could not be just `AnyBase` # (over which we are generic) since we aren't returning *that* type, # but some other type determined by which type `AnyBase` is. def magic_convert(input: AnyBase) -> AnyBase.VALUE_T: #??? return input.VALUE_T(self.get_some_value().value * 2)``` -
Actual behaviour: There does seem to be anything like
AnyBase.VALUE_Tavailable for use inmypyand the given syntax results in both type-time and runtime errors. -
Expected behaviour: Some way to do this.
-
mypy version: 0.730
-
Python version: 3.7 (Debian)
-
mypy flags: None
I used some title that probably is close to the real (type theory) name of this feature, feel free to change it to something more fitting. 🙂
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
Comienza con la reproducción en Python 3.7 del issue y lee el mock-up detallado enlazado para entender la relación de tipo asociado deseada. Define cómo deberían ser la sintaxis compatible y el comportamiento de comprobación de tipos para el tipo de retorno genérico, incluido el ejemplo mostrado de magic_convert; el issue estará terminado cuando mypy pueda expresar y validar esa relación sin los errores de tiempo de tipos o de ejecución indicados.
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
- Necesita aclaración
- Aptitud para principiantes
- 30/100