typing.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import typing as t
  2. if t.TYPE_CHECKING:
  3. from _typeshed.wsgi import WSGIApplication # noqa: F401
  4. from werkzeug.datastructures import Headers # noqa: F401
  5. from werkzeug.wrappers.response 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.Dict[str, t.Any], # any jsonify-able dict
  12. t.Iterator[str],
  13. t.Iterator[bytes],
  14. ]
  15. StatusCode = int
  16. # the possible types for an individual HTTP header
  17. HeaderName = str
  18. HeaderValue = t.Union[str, t.List[str], t.Tuple[str, ...]]
  19. # the possible types for HTTP headers
  20. HeadersValue = t.Union[
  21. "Headers", t.Dict[HeaderName, HeaderValue], t.List[t.Tuple[HeaderName, HeaderValue]]
  22. ]
  23. # The possible types returned by a route function.
  24. ResponseReturnValue = t.Union[
  25. ResponseValue,
  26. t.Tuple[ResponseValue, HeadersValue],
  27. t.Tuple[ResponseValue, StatusCode],
  28. t.Tuple[ResponseValue, StatusCode, HeadersValue],
  29. "WSGIApplication",
  30. ]
  31. AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
  32. AfterRequestCallable = t.Callable[["Response"], "Response"]
  33. BeforeFirstRequestCallable = t.Callable[[], None]
  34. BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
  35. TeardownCallable = t.Callable[[t.Optional[BaseException]], None]
  36. TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]
  37. TemplateFilterCallable = t.Callable[..., t.Any]
  38. TemplateGlobalCallable = t.Callable[..., t.Any]
  39. TemplateTestCallable = t.Callable[..., bool]
  40. URLDefaultCallable = t.Callable[[str, dict], None]
  41. URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]
  42. # This should take Exception, but that either breaks typing the argument
  43. # with a specific exception, or decorating multiple times with different
  44. # exceptions (and using a union type on the argument).
  45. # https://github.com/pallets/flask/issues/4095
  46. # https://github.com/pallets/flask/issues/4295
  47. # https://github.com/pallets/flask/issues/4297
  48. ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]