typing.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. t.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, t.List[str], t.Tuple[str, ...]]
  21. # the possible types for HTTP headers
  22. HeadersValue = t.Union[
  23. "Headers",
  24. t.Mapping[str, HeaderValue],
  25. t.Sequence[t.Tuple[str, HeaderValue]],
  26. ]
  27. # The possible types returned by a route function.
  28. ResponseReturnValue = t.Union[
  29. ResponseValue,
  30. t.Tuple[ResponseValue, HeadersValue],
  31. t.Tuple[ResponseValue, int],
  32. t.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[[], t.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[[], t.Dict[str, t.Any]],
  58. t.Callable[[], t.Awaitable[t.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], None]
  64. URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]
  65. # This should take Exception, but that either breaks typing the argument
  66. # with a specific exception, or decorating multiple times with different
  67. # exceptions (and using a union type on the argument).
  68. # https://github.com/pallets/flask/issues/4095
  69. # https://github.com/pallets/flask/issues/4295
  70. # https://github.com/pallets/flask/issues/4297
  71. ErrorHandlerCallable = t.Union[
  72. t.Callable[[t.Any], ResponseReturnValue],
  73. t.Callable[[t.Any], t.Awaitable[ResponseReturnValue]],
  74. ]
  75. RouteCallable = t.Union[
  76. t.Callable[..., ResponseReturnValue],
  77. t.Callable[..., t.Awaitable[ResponseReturnValue]],
  78. ]