@@ -324,7 +324,7 @@ def __init__(
324324 except HTTPException as e :
325325 self .request .routing_exception = e
326326 self .flashes : list [tuple [str , str ]] | None = None
327- self .session : SessionMixin | None = session
327+ self ._session : SessionMixin | None = session
328328 # Functions that should be executed after the request on the response
329329 # object. These will be called before the regular "after_request"
330330 # functions.
@@ -351,7 +351,7 @@ def copy(self) -> RequestContext:
351351 self .app ,
352352 environ = self .request .environ ,
353353 request = self .request ,
354- session = self .session ,
354+ session = self ._session ,
355355 )
356356
357357 def match_request (self ) -> None :
@@ -364,6 +364,16 @@ def match_request(self) -> None:
364364 except HTTPException as e :
365365 self .request .routing_exception = e
366366
367+ @property
368+ def session (self ) -> SessionMixin :
369+ """The session data associated with this request. Not available until
370+ this context has been pushed. Accessing this property, also accessed by
371+ the :data:`~flask.session` proxy, sets :attr:`.SessionMixin.accessed`.
372+ """
373+ assert self ._session is not None , "The session has not yet been opened."
374+ self ._session .accessed = True
375+ return self ._session
376+
367377 def push (self ) -> None :
368378 # Before we push the request context we have to ensure that there
369379 # is an application context.
@@ -381,12 +391,12 @@ def push(self) -> None:
381391 # This allows a custom open_session method to use the request context.
382392 # Only open a new session if this is the first time the request was
383393 # pushed, otherwise stream_with_context loses the session.
384- if self .session is None :
394+ if self ._session is None :
385395 session_interface = self .app .session_interface
386- self .session = session_interface .open_session (self .app , self .request )
396+ self ._session = session_interface .open_session (self .app , self .request )
387397
388- if self .session is None :
389- self .session = session_interface .make_null_session (self .app )
398+ if self ._session is None :
399+ self ._session = session_interface .make_null_session (self .app )
390400
391401 # Match the request URL after loading the session, so that the
392402 # session is available in custom URL converters.
0 commit comments