Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/flask/globals.py: 74%

35 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-09 06:08 +0000

1from __future__ import annotations 

2 

3import typing as t 

4from contextvars import ContextVar 

5 

6from werkzeug.local import LocalProxy 

7 

8if t.TYPE_CHECKING: # pragma: no cover 

9 from .app import Flask 

10 from .ctx import _AppCtxGlobals 

11 from .ctx import AppContext 

12 from .ctx import RequestContext 

13 from .sessions import SessionMixin 

14 from .wrappers import Request 

15 

16 

17class _FakeStack: 

18 def __init__(self, name: str, cv: ContextVar[t.Any]) -> None: 

19 self.name = name 

20 self.cv = cv 

21 

22 @property 

23 def top(self) -> t.Any | None: 

24 import warnings 

25 

26 warnings.warn( 

27 f"'_{self.name}_ctx_stack' is deprecated and will be removed in Flask 2.4." 

28 f" Use 'g' to store data, or '{self.name}_ctx' to access the current" 

29 " context.", 

30 DeprecationWarning, 

31 stacklevel=2, 

32 ) 

33 return self.cv.get(None) 

34 

35 

36_no_app_msg = """\ 

37Working outside of application context. 

38 

39This typically means that you attempted to use functionality that needed 

40the current application. To solve this, set up an application context 

41with app.app_context(). See the documentation for more information.\ 

42""" 

43_cv_app: ContextVar[AppContext] = ContextVar("flask.app_ctx") 

44__app_ctx_stack = _FakeStack("app", _cv_app) 

45app_ctx: AppContext = LocalProxy( # type: ignore[assignment] 

46 _cv_app, unbound_message=_no_app_msg 

47) 

48current_app: Flask = LocalProxy( # type: ignore[assignment] 

49 _cv_app, "app", unbound_message=_no_app_msg 

50) 

51g: _AppCtxGlobals = LocalProxy( # type: ignore[assignment] 

52 _cv_app, "g", unbound_message=_no_app_msg 

53) 

54 

55_no_req_msg = """\ 

56Working outside of request context. 

57 

58This typically means that you attempted to use functionality that needed 

59an active HTTP request. Consult the documentation on testing for 

60information about how to avoid this problem.\ 

61""" 

62_cv_request: ContextVar[RequestContext] = ContextVar("flask.request_ctx") 

63__request_ctx_stack = _FakeStack("request", _cv_request) 

64request_ctx: RequestContext = LocalProxy( # type: ignore[assignment] 

65 _cv_request, unbound_message=_no_req_msg 

66) 

67request: Request = LocalProxy( # type: ignore[assignment] 

68 _cv_request, "request", unbound_message=_no_req_msg 

69) 

70session: SessionMixin = LocalProxy( # type: ignore[assignment] 

71 _cv_request, "session", unbound_message=_no_req_msg 

72) 

73 

74 

75def __getattr__(name: str) -> t.Any: 

76 if name == "_app_ctx_stack": 

77 import warnings 

78 

79 warnings.warn( 

80 "'_app_ctx_stack' is deprecated and will be removed in Flask 2.4.", 

81 DeprecationWarning, 

82 stacklevel=2, 

83 ) 

84 return __app_ctx_stack 

85 

86 if name == "_request_ctx_stack": 

87 import warnings 

88 

89 warnings.warn( 

90 "'_request_ctx_stack' is deprecated and will be removed in Flask 2.4.", 

91 DeprecationWarning, 

92 stacklevel=2, 

93 ) 

94 return __request_ctx_stack 

95 

96 raise AttributeError(name)