Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/flask/typing.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import annotations
3import collections.abc as cabc
4import typing as t
6if t.TYPE_CHECKING: # pragma: no cover
7 from _typeshed.wsgi import WSGIApplication # noqa: F401
8 from werkzeug.datastructures import Headers # noqa: F401
9 from werkzeug.sansio.response import Response # noqa: F401
11# The possible types that are directly convertible or are a Response object.
12ResponseValue = t.Union[
13 "Response",
14 str,
15 bytes,
16 list[t.Any],
17 # Only dict is actually accepted, but Mapping allows for TypedDict.
18 t.Mapping[str, t.Any],
19 t.Iterator[str],
20 t.Iterator[bytes],
21 cabc.AsyncIterable[str], # for Quart, until App is generic.
22 cabc.AsyncIterable[bytes],
23]
25# the possible types for an individual HTTP header
26# This should be a Union, but mypy doesn't pass unless it's a TypeVar.
27HeaderValue = t.Union[str, list[str], tuple[str, ...]]
29# the possible types for HTTP headers
30HeadersValue = t.Union[
31 "Headers",
32 t.Mapping[str, HeaderValue],
33 t.Sequence[tuple[str, HeaderValue]],
34]
36# The possible types returned by a route function.
37ResponseReturnValue = t.Union[
38 ResponseValue,
39 tuple[ResponseValue, HeadersValue],
40 tuple[ResponseValue, int],
41 tuple[ResponseValue, int, HeadersValue],
42 "WSGIApplication",
43]
45# Allow any subclass of werkzeug.Response, such as the one from Flask,
46# as a callback argument. Using werkzeug.Response directly makes a
47# callback annotated with flask.Response fail type checking.
48ResponseClass = t.TypeVar("ResponseClass", bound="Response")
50AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
51AfterRequestCallable = t.Union[
52 t.Callable[[ResponseClass], ResponseClass],
53 t.Callable[[ResponseClass], t.Awaitable[ResponseClass]],
54]
55BeforeFirstRequestCallable = t.Union[
56 t.Callable[[], None], t.Callable[[], t.Awaitable[None]]
57]
58BeforeRequestCallable = t.Union[
59 t.Callable[[], t.Optional[ResponseReturnValue]],
60 t.Callable[[], t.Awaitable[t.Optional[ResponseReturnValue]]],
61]
62ShellContextProcessorCallable = t.Callable[[], dict[str, t.Any]]
63TeardownCallable = t.Union[
64 t.Callable[[t.Optional[BaseException]], None],
65 t.Callable[[t.Optional[BaseException]], t.Awaitable[None]],
66]
67TemplateContextProcessorCallable = t.Union[
68 t.Callable[[], dict[str, t.Any]],
69 t.Callable[[], t.Awaitable[dict[str, t.Any]]],
70]
71TemplateFilterCallable = t.Callable[..., t.Any]
72TemplateGlobalCallable = t.Callable[..., t.Any]
73TemplateTestCallable = t.Callable[..., bool]
74URLDefaultCallable = t.Callable[[str, dict[str, t.Any]], None]
75URLValuePreprocessorCallable = t.Callable[
76 [t.Optional[str], t.Optional[dict[str, t.Any]]], None
77]
79# This should take Exception, but that either breaks typing the argument
80# with a specific exception, or decorating multiple times with different
81# exceptions (and using a union type on the argument).
82# https://github.com/pallets/flask/issues/4095
83# https://github.com/pallets/flask/issues/4295
84# https://github.com/pallets/flask/issues/4297
85ErrorHandlerCallable = t.Union[
86 t.Callable[[t.Any], ResponseReturnValue],
87 t.Callable[[t.Any], t.Awaitable[ResponseReturnValue]],
88]
90RouteCallable = t.Union[
91 t.Callable[..., ResponseReturnValue],
92 t.Callable[..., t.Awaitable[ResponseReturnValue]],
93]