Skip to content

Commit adf3636

Browse files
authored
merge app and request context (#5812)
2 parents 3301232 + c2705ff commit adf3636

36 files changed

+779
-1007
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Unreleased
55

66
- Drop support for Python 3.9. :pr:`5730`
77
- Remove previously deprecated code: ``__version__``. :pr:`5648`
8+
- ``RequestContext`` has merged with ``AppContext``. ``RequestContext`` is now
9+
a deprecated alias. If an app context is already pushed, it is not reused
10+
when dispatching a request. This greatly simplifies the internal code for tracking
11+
the active context. :issue:`5639`
812
- ``template_filter``, ``template_test``, and ``template_global`` decorators
913
can be used without parentheses. :issue:`5729`
1014

docs/api.rst

Lines changed: 58 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,15 @@ Incoming Request Data
3131
:inherited-members:
3232
:exclude-members: json_module
3333

34-
.. attribute:: request
34+
.. data:: request
3535

36-
To access incoming request data, you can use the global `request`
37-
object. Flask parses incoming request data for you and gives you
38-
access to it through that global object. Internally Flask makes
39-
sure that you always get the correct data for the active thread if you
40-
are in a multithreaded environment.
36+
A proxy to the request data for the current request, an instance of
37+
:class:`.Request`.
4138

42-
This is a proxy. See :ref:`notes-on-proxies` for more information.
39+
This is only available when a :doc:`request context </appcontext>` is
40+
active.
4341

44-
The request object is an instance of a :class:`~flask.Request`.
42+
This is a proxy. See :ref:`context-visibility` for more information.
4543

4644

4745
Response Objects
@@ -62,40 +60,33 @@ does this is by using a signed cookie. The user can look at the session
6260
contents, but can't modify it unless they know the secret key, so make sure to
6361
set that to something complex and unguessable.
6462

65-
To access the current session you can use the :class:`session` object:
63+
To access the current session you can use the :data:`.session` proxy.
6664

67-
.. class:: session
65+
.. data:: session
6866

69-
The session object works pretty much like an ordinary dict, with the
70-
difference that it keeps track of modifications.
67+
A proxy to the session data for the current request, an instance of
68+
:class:`.SessionMixin`.
7169

72-
This is a proxy. See :ref:`notes-on-proxies` for more information.
70+
This is only available when a :doc:`request context </appcontext>` is
71+
active.
7372

74-
The following attributes are interesting:
73+
This is a proxy. See :ref:`context-visibility` for more information.
7574

76-
.. attribute:: new
75+
The session object works like a dict but tracks assignment and access to its
76+
keys. It cannot track modifications to mutable values, you need to set
77+
:attr:`~.SessionMixin.modified` manually when modifying a list, dict, etc.
7778

78-
``True`` if the session is new, ``False`` otherwise.
79-
80-
.. attribute:: modified
81-
82-
``True`` if the session object detected a modification. Be advised
83-
that modifications on mutable structures are not picked up
84-
automatically, in that situation you have to explicitly set the
85-
attribute to ``True`` yourself. Here an example::
79+
.. code-block:: python
8680
87-
# this change is not picked up because a mutable object (here
88-
# a list) is changed.
89-
session['objects'].append(42)
81+
# appending to a list is not detected
82+
session["numbers"].append(42)
9083
# so mark it as modified yourself
9184
session.modified = True
9285
93-
.. attribute:: permanent
94-
95-
If set to ``True`` the session lives for
96-
:attr:`~flask.Flask.permanent_session_lifetime` seconds. The
97-
default is 31 days. If set to ``False`` (which is the default) the
98-
session will be deleted when the user closes the browser.
86+
The session is persisted across requests using a cookie. By default the
87+
users's browser will clear the cookie when it is closed. Set
88+
:attr:`~.SessionMixin.permanent` to ``True`` to persist the cookie for
89+
:data:`PERMANENT_SESSION_LIFETIME`.
9990

10091

10192
Session Interface
@@ -158,20 +149,21 @@ another, a global variable is not good enough because it would break in
158149
threaded environments. Flask provides you with a special object that
159150
ensures it is only valid for the active request and that will return
160151
different values for each request. In a nutshell: it does the right
161-
thing, like it does for :class:`request` and :class:`session`.
152+
thing, like it does for :data:`.request` and :data:`.session`.
162153

163154
.. data:: g
164155

165-
A namespace object that can store data during an
166-
:doc:`application context </appcontext>`. This is an instance of
167-
:attr:`Flask.app_ctx_globals_class`, which defaults to
168-
:class:`ctx._AppCtxGlobals`.
156+
A proxy to a namespace object used to store data during a single request or
157+
app context. An instance of :attr:`.Flask.app_ctx_globals_class`, which
158+
defaults to :class:`._AppCtxGlobals`.
169159

170-
This is a good place to store resources during a request. For
171-
example, a ``before_request`` function could load a user object from
172-
a session id, then set ``g.user`` to be used in the view function.
160+
This is a good place to store resources during a request. For example, a
161+
:meth:`~.Flask.before_request` function could load a user object from a
162+
session id, then set ``g.user`` to be used in the view function.
173163

174-
This is a proxy. See :ref:`notes-on-proxies` for more information.
164+
This is only available when an :doc:`app context </appcontext>` is active.
165+
166+
This is a proxy. See :ref:`context-visibility` for more information.
175167

176168
.. versionchanged:: 0.10
177169
Bound to the application context instead of the request context.
@@ -185,17 +177,16 @@ Useful Functions and Classes
185177

186178
.. data:: current_app
187179

188-
A proxy to the application handling the current request. This is
189-
useful to access the application without needing to import it, or if
190-
it can't be imported, such as when using the application factory
191-
pattern or in blueprints and extensions.
180+
A proxy to the :class:`.Flask` application handling the current request or
181+
other activity.
182+
183+
This is useful to access the application without needing to import it, or if
184+
it can't be imported, such as when using the application factory pattern or
185+
in blueprints and extensions.
192186

193-
This is only available when an
194-
:doc:`application context </appcontext>` is pushed. This happens
195-
automatically during requests and CLI commands. It can be controlled
196-
manually with :meth:`~flask.Flask.app_context`.
187+
This is only available when an :doc:`app context </appcontext>` is active.
197188

198-
This is a proxy. See :ref:`notes-on-proxies` for more information.
189+
This is a proxy. See :ref:`context-visibility` for more information.
199190

200191
.. autofunction:: has_request_context
201192

@@ -299,31 +290,31 @@ Stream Helpers
299290
Useful Internals
300291
----------------
301292

302-
.. autoclass:: flask.ctx.RequestContext
293+
.. autoclass:: flask.ctx.AppContext
303294
:members:
304295

305-
.. data:: flask.globals.request_ctx
296+
.. data:: flask.globals.app_ctx
306297

307-
The current :class:`~flask.ctx.RequestContext`. If a request context
308-
is not active, accessing attributes on this proxy will raise a
309-
``RuntimeError``.
298+
A proxy to the active :class:`.AppContext`.
310299

311-
This is an internal object that is essential to how Flask handles
312-
requests. Accessing this should not be needed in most cases. Most
313-
likely you want :data:`request` and :data:`session` instead.
300+
This is an internal object that is essential to how Flask handles requests.
301+
Accessing this should not be needed in most cases. Most likely you want
302+
:data:`.current_app`, :data:`.g`, :data:`.request`, and :data:`.session` instead.
314303

315-
.. autoclass:: flask.ctx.AppContext
316-
:members:
304+
This is only available when a :doc:`request context </appcontext>` is
305+
active.
317306

318-
.. data:: flask.globals.app_ctx
307+
This is a proxy. See :ref:`context-visibility` for more information.
319308

320-
The current :class:`~flask.ctx.AppContext`. If an app context is not
321-
active, accessing attributes on this proxy will raise a
322-
``RuntimeError``.
309+
.. class:: flask.ctx.RequestContext
310+
311+
.. deprecated:: 3.2
312+
Merged with :class:`AppContext`. This alias will be removed in Flask 4.0.
313+
314+
.. data:: flask.globals.request_ctx
323315

324-
This is an internal object that is essential to how Flask handles
325-
requests. Accessing this should not be needed in most cases. Most
326-
likely you want :data:`current_app` and :data:`g` instead.
316+
.. deprecated:: 3.2
317+
Merged with :data:`.app_ctx`. This alias will be removed in Flask 4.0.
327318

328319
.. autoclass:: flask.blueprints.BlueprintSetupState
329320
:members:

0 commit comments

Comments
 (0)