typing.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 import Response # noqa: F401
  6. # The possible types that are directly convertible or are a Response object.
  7. ResponseValue = t.Union["Response", str, bytes, t.Dict[str, t.Any]]
  8. # the possible types for an individual HTTP header
  9. # This should be a Union, but mypy doesn't pass unless it's a TypeVar.
  10. HeaderValue = t.Union[str, t.List[str], t.Tuple[str, ...]]
  11. # the possible types for HTTP headers
  12. HeadersValue = t.Union[
  13. "Headers",
  14. t.Mapping[str, HeaderValue],
  15. t.Sequence[t.Tuple[str, HeaderValue]],
  16. ]
  17. # The possible types returned by a route function.
  18. ResponseReturnValue = t.Union[
  19. ResponseValue,
  20. t.Tuple[ResponseValue, HeadersValue],
  21. t.Tuple[ResponseValue, int],
  22. t.Tuple[ResponseValue, int, HeadersValue],
  23. "WSGIApplication",
  24. ]
  25. # Allow any subclass of werkzeug.Response, such as the one from Flask,
  26. # as a callback argument. Using werkzeug.Response directly makes a
  27. # callback annotated with flask.Response fail type checking.
  28. ResponseClass = t.TypeVar("ResponseClass", bound="Response")
  29. AppOrBlueprintKey = t.Optional[str] # The App key is None, whereas blueprints are named
  30. AfterRequestCallable = t.Callable[[ResponseClass], ResponseClass]
  31. BeforeFirstRequestCallable = t.Callable[[], None]
  32. BeforeRequestCallable = t.Callable[[], t.Optional[ResponseReturnValue]]
  33. TeardownCallable = t.Callable[[t.Optional[BaseException]], None]
  34. TemplateContextProcessorCallable = t.Callable[[], t.Dict[str, t.Any]]
  35. TemplateFilterCallable = t.Callable[..., t.Any]
  36. TemplateGlobalCallable = t.Callable[..., t.Any]
  37. TemplateTestCallable = t.Callable[..., bool]
  38. URLDefaultCallable = t.Callable[[str, dict], None]
  39. URLValuePreprocessorCallable = t.Callable[[t.Optional[str], t.Optional[dict]], None]
  40. # This should take Exception, but that either breaks typing the argument
  41. # with a specific exception, or decorating multiple times with different
  42. # exceptions (and using a union type on the argument).
  43. # https://github.com/pallets/flask/issues/4095
  44. # https://github.com/pallets/flask/issues/4295
  45. # https://github.com/pallets/flask/issues/4297
  46. ErrorHandlerCallable = t.Callable[[t.Any], ResponseReturnValue]
  47. ErrorHandlerDecorator = t.TypeVar("ErrorHandlerDecorator", bound=ErrorHandlerCallable)
  48. ViewCallable = t.Callable[..., ResponseReturnValue]
  49. RouteDecorator = t.TypeVar("RouteDecorator", bound=ViewCallable)