typing.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 .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. t.AnyStr,
  10. t.Dict[str, t.Any], # any jsonify-able dict
  11. t.Generator[t.AnyStr, None, None],
  12. ]
  13. StatusCode = int
  14. # the possible types for an individual HTTP header
  15. HeaderName = str
  16. HeaderValue = t.Union[str, t.List[str], t.Tuple[str, ...]]
  17. # the possible types for HTTP headers
  18. HeadersValue = t.Union[
  19. "Headers", t.Dict[HeaderName, HeaderValue], t.List[t.Tuple[HeaderName, HeaderValue]]
  20. ]
  21. # The possible types returned by a route function.
  22. ResponseReturnValue = t.Union[
  23. ResponseValue,
  24. t.Tuple[ResponseValue, HeadersValue],
  25. t.Tuple[ResponseValue, StatusCode],
  26. t.Tuple[ResponseValue, StatusCode, HeadersValue],
  27. "WSGIApplication",
  28. ]
  29. GenericException = t.TypeVar("GenericException", bound=Exception, contravariant=True)
  30. AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
  31. AfterRequestCallable = t.Callable[["Response"], "Response"]
  32. BeforeFirstRequestCallable = t.Callable[[], None]
  33. BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
  34. TeardownCallable = t.Callable[[t.Optional[BaseException]], None]
  35. TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]
  36. TemplateFilterCallable = t.Callable[..., t.Any]
  37. TemplateGlobalCallable = t.Callable[..., t.Any]
  38. TemplateTestCallable = t.Callable[..., bool]
  39. URLDefaultCallable = t.Callable[[str, dict], None]
  40. URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]
  41. if t.TYPE_CHECKING:
  42. import typing_extensions as te
  43. class ErrorHandlerCallable(te.Protocol[GenericException]):
  44. def __call__(self, error: GenericException) -> ResponseReturnValue:
  45. ...