python / python/typeshed

How to use _typeshed.wsgi with typing.get_type_hints

Abierto
#11,004 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
Python
Estrellas
5.1k
Forks
2.1k
Merge medio
1 d 19 h
PR fusionados (30 d)
82

Descripción

Hello there,
is there a way to get to work combination of _typeshed.wsgi and typing.get_type_hints in runtime?
In flask codebase there is something like

from __future__ import annotations

import typing as t

if t.TYPE_CHECKING:  # pragma: no cover
    from _typeshed.wsgi import WSGIApplication  # noqa: F401
    from werkzeug.datastructures import Headers  # noqa: F401
    from werkzeug.sansio.response import Response  # noqa: F401

# The possible types that are directly convertible or are a Response object.
ResponseValue = t.Union[
    "Response",
    str,
    bytes,
    t.List[t.Any],
    # Only dict is actually accepted, but Mapping allows for TypedDict.
    t.Mapping[str, t.Any],
    t.Iterator[str],
    t.Iterator[bytes],
]

# the possible types for an individual HTTP header
# This should be a Union, but mypy doesn't pass unless it's a TypeVar.
HeaderValue = t.Union[str, t.List[str], t.Tuple[str, ...]]

# the possible types for HTTP headers
HeadersValue = t.Union[
    "Headers",
    t.Mapping[str, HeaderValue],
    t.Sequence[t.Tuple[str, HeaderValue]],
]

# The possible types returned by a route function.
ResponseReturnValue = t.Union[
    ResponseValue,
    t.Tuple[ResponseValue, HeadersValue],
    t.Tuple[ResponseValue, int],
    t.Tuple[ResponseValue, int, HeadersValue],
    "WSGIApplication",
]

https://github.com/pallets/flask/blob/3.0.0/src/flask/typing.py
and in my application I use something like

from flask.typing import ResponseReturnValue

@inject
def my_endpoint() -> ResponseReturnValue:
    return "asdf"

...
def inject(method):
    ...
    arg_types = typing.get_type_hints(method)
    # get args from DI container and call actual function with injected args

which ends with error like

NameError: name 'Response' is not defined

Here is complete playground for my issue https://www.online-python.com/AsZX867MEO

I can probably use my own type for that - that's why I ask here - but not sure how to use typeshed in runtime.
Then I can probably send PR directly to flask with fix for such edge cases (as nobody probably encountered this based on how long this is in flask already).

Thanks for you tips and help in advice.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Empieza por src/flask/typing.py de Flask y la llamada a typing.get_type_hints mostrada en el issue; después, reproduce el NameError usando el playground enlazado. Determina si el fallo en tiempo de ejecución corresponde a Flask o a typeshed, y documenta una resolución concreta con una prueba o ejemplo reproducible.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
flask, python
Área
tooling
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.