`[Async]ContextDecorator.__call__` return type should depend on the underlying context manager's `_ExitT_co`
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
- #14439 de @johnslavik — cerrado sin fusionar
Evaluación
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Error
- Claridad
- Necesita aclaración
- Estado de actividad
- Estancado
- Stack tecnológico
- python
- Área
- developer-experience
Línea de trabajo
Comience con la discusión de Python enlazada y la implementación de la biblioteca estándar en Lib/contextlib.py, centrándose en ContextDecorator.call y en el _ExitT_co del gestor de contexto subyacente. Determine una relación compatible a nivel de tipos para el tipo de retorno de la función invocable envuelta y, después, verifique que la anotación refleje los casos en los que la supresión del gestor de contexto devuelve None.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
[!Important]
Please see https://discuss.python.org/t/support-suppressing-generator-context-managers/103615 first!
This is how ContextDecorator.__call__ is implemented in the Python standard library:
def __call__(self, func):
@wraps(func)
def inner(*args, **kwds):
with self._recreate_cm():
return func(*args, **kwds)
return inner
In typeshed, ContextDecorator.__call__ currently has a return type of the original callable (func).
However, if the return value of self._recreate_cm().__call__ is truthy, the return type of ContextDecorator.__call__ can as well be None, like in the silly example below:
from collections.abc import Generator
from contextlib import contextmanager, suppress
from typing import reveal_type
@contextmanager
def zero_division_to_none() -> Generator[None]:
with suppress(ZeroDivisionError):
yield
@zero_division_to_none()
def div(a: int, b: int) -> float:
return a / b
reveal_type(div(1, 0))
# mypy, pyright: Revealed type is "builtins.float"
# Runtime type is 'NoneType'
My first shot was to patch the proper __call__ functions to "extend" the return type with _ThrottledExcReturnT (a new type parameter of ContextDecorator), constrained by Never and None, with the default type Never for backward compatibility. To me, there's no way to declare the relationship symbolically, so it would have to be typed manually in classes implementing the context manager protocol.
- Lenguaje dominante
- Python
- Estrellas
- 5.1k
- Forks
- 2.1k
- Merge medio
- 2 d 1 h
- PR fusionados (30 d)
- 115
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la 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.
Más de python/typeshed
-
`networkx.set_edge_attributes` should support scalar value + namePosiblemente ocupada @Fire162 la tomó hace 10 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
Los mantenedores suelen responder en 1 día
-
false alarm on decode callPosiblemente ocupada @jw9829 la tomó hace 37 días. Abiertohelp wanted stubs: false positive
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
python/typeshed#16441 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Don't run pyright basic checks on projects with strict pyright settings in CIPosiblemente ocupada @Mohith1612 la tomó hace 74 días. Abiertoproject: infrastructure
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
python/typeshed#16005 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
stubs: improvement
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
python/typeshed#15548 · 5 comentarios ·
Los mantenedores suelen responder en 1 día
-
stubs: improvement
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
python/typeshed#15529 · 5 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de python/typeshed
Issues similares
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 78/100
QuantConnect/Documentation#2838 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
awslabs/route53-dynamic-dns-with-lambda#67 · 1 comentario ·
-
`SubAgents` returns a child run's `DeferredToolRequests` to the parent as a successful tool resultAbiertobug pkg:harness
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
pydantic/pydantic-ai#12405 ·
Los mantenedores suelen responder en 1 día
-
good first issue help wanted
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
good first issue help wanted
Dificultad 1/5 Menos de una hora Aptitud para principiantes 82/100