typing.py 3.1 KB

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