globals.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. import typing as t
  3. from contextvars import ContextVar
  4. from werkzeug.local import LocalProxy
  5. if t.TYPE_CHECKING: # pragma: no cover
  6. from .app import Flask
  7. from .ctx import _AppCtxGlobals
  8. from .ctx import AppContext
  9. from .ctx import RequestContext
  10. from .sessions import SessionMixin
  11. from .wrappers import Request
  12. _no_app_msg = """\
  13. Working outside of application context.
  14. This typically means that you attempted to use functionality that needed
  15. the current application. To solve this, set up an application context
  16. with app.app_context(). See the documentation for more information.\
  17. """
  18. _cv_app: ContextVar[AppContext] = ContextVar("flask.app_ctx")
  19. app_ctx: AppContext = LocalProxy( # type: ignore[assignment]
  20. _cv_app, unbound_message=_no_app_msg
  21. )
  22. current_app: Flask = LocalProxy( # type: ignore[assignment]
  23. _cv_app, "app", unbound_message=_no_app_msg
  24. )
  25. g: _AppCtxGlobals = LocalProxy( # type: ignore[assignment]
  26. _cv_app, "g", unbound_message=_no_app_msg
  27. )
  28. _no_req_msg = """\
  29. Working outside of request context.
  30. This typically means that you attempted to use functionality that needed
  31. an active HTTP request. Consult the documentation on testing for
  32. information about how to avoid this problem.\
  33. """
  34. _cv_request: ContextVar[RequestContext] = ContextVar("flask.request_ctx")
  35. request_ctx: RequestContext = LocalProxy( # type: ignore[assignment]
  36. _cv_request, unbound_message=_no_req_msg
  37. )
  38. request: Request = LocalProxy( # type: ignore[assignment]
  39. _cv_request, "request", unbound_message=_no_req_msg
  40. )
  41. session: SessionMixin = LocalProxy( # type: ignore[assignment]
  42. _cv_request, "session", unbound_message=_no_req_msg
  43. )