typing.py 2.9 KB

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