typing.py 3.2 KB

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