Specify methods that are allowed for inference of partially initialized generics
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
Descripción
The inference of generic types for classes in most type-checkers assume that all the generic types can be inferred from the parameters to __init__. This is not always the case. For example, it is a common pattern to create an empty container class and add elements to it later, e.g.:
a = []
a.append("hello world")
Currently for builtin classes, such as list or dict, this idiom is understood by MyPy, but not by Pyright. Moreover, even MyPy is incapable of applying this type inference to custom types (see https://github.com/python/mypy/issues/13134). As mentioned in that issue, the problem is not only with empty containers, but also with some widely used APIs, such as the Estimator API from scikit-learn and related projects, in which the type of the data used is only known when the fit method is called.
My proposal would be to add functionality to the type system to define which methods can be used to infer generic types, apart from __init__. This could be made for example with a decorator (e.g. allow_generic_inference), so that one can define a method such as:
@allow_generic_inference
def append(value: T):
...
In this case the value of the class generic parameter T could be inferred from append if it was undefined before.
I think this proposal may also be similar to the previously suggested TypeAssert mentioned by @erictraut in https://github.com/python/typing/discussions/1013#discussioncomment-1966238. However I am not completely sure that TypeAssert could do what I explained here. I am also not sure that the proposed TypeAssert syntax is better than a decorator.
I hope you too consider this proposal useful, and I look forward to your feedback.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
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
No se nombran archivos fuente ni pruebas. Empieza revisando los ejemplos de la propuesta sobre contenedores vacíos y las API de Estimator de scikit-learn; después, lee el issue enlazado de MyPy y el debate sobre TypeAssert. Se considera terminado llegar a un acuerdo sobre si los métodos distintos de init pueden inferir parámetros genéricos y definir la sintaxis y el comportamiento aceptados.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers
- 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