python / python/typeshed

How to use _typeshed.wsgi with typing.get_type_hints

Aperta
#11,004 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
5.1k
Fork
2.1k
Merge medio
1g 19h
PR unite (30g)
82

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da src/flask/typing.py di Flask e dalla chiamata a typing.get_type_hints mostrata nell’issue, quindi riproduci il NameError usando il playground collegato. Determina se l’errore a runtime riguarda Flask o typeshed e documenta una risoluzione concreta con un test o un esempio riproducibile.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
flask, python
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.