Multi-level type resolution
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
It would be nice if Mypy was able to resolve the types in the below snippet:
import os
import typing as t
T = t.TypeVar('T')
R = t.TypeVar('R')
class flatmap(t.Generic[T, R]):
def __init__(self, func: t.Callable[[T], R]) -> None:
self.func = func
def __ror__(self, value: t.Optional[T]) -> t.Optional[R]:
if value is not None:
return self.func(value)
return None
# error: Cannot infer type argument 1 of "flatmap"
# error: "object" has no attribute "upper"
print(os.getenv('VARNAME') | flatmap(lambda m: m.upper()))
print(os.getenv('VARNAME') | flatmap(str.upper)) # ok
Pitch
Looking at just flatmap(lambda m: m.upper()), neither T nor R can be immediately determined.
However, it should be possible to deduce the type variables by taking into account the broader
context.
T = strcan be derived from the__ror__()call- After propagating
Tinto theflatmap()andlambda,R = strcan be deduced from the lambda body
One potential use case is already highlighted in the example above. Being able to chain the flatmap() function using ror adds a lot of readability compared to a normal call:
# 1)
os.getenv('VARNAME') | flatmap(lambda m: m.upper())
# 2)
flatmap(os.getenv('VARNAME'), lambda m: m.upper())
The obvious workaround in this example would be to use str.upper which would satisfy Mypy because the function is already typed. However, there are other example where the there is no already-typed alternative, like accessing an attribute of an optional object. Nr. 1 would be preferred over 2 for it's better readability. 3 is an alternative without a helper function, though that also gets harder to read as you start to work with more optional values.
# 1)
email = maybe_get_user() | flatmap(lambda user: user.email) | flatmap(sanitize_email_address)
# 2)
email = flatmap(flatmap(maybe_get_user(), lambda user: user.email), sanitize_email_address)
# 3)
user = maybe_get_user()
email = sanitize_email_address(user.email) if user and user.email else None
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 problema con el fragmento de Python proporcionado y compara el caso de lambda con el caso funcional de str.upper. Rastrea cómo mypy gestiona la construcción genérica de flatmap, la llamada a ror y la propagación a través de lambda; el trabajo estará terminado cuando los ejemplos infieran los resultados esperados de Optional[str] sin errores.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers, 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
- 35/100