@@ -107,6 +107,13 @@ async def get_user(id:int):
107107 """
108108 return self .scoped_session ()
109109
110+ @property
111+ def scoped (self ) -> bool :
112+ """Whether the current context has a session. If False, the session is the default global session,
113+ and the transaction needs to be manually submitted.
114+ """
115+ return bool (self ._session_scope .get ())
116+
110117 def __call__ (self , scope : Any = None ):
111118 return AsyncSessionContextVarManager (self , scope = scope )
112119
@@ -140,7 +147,7 @@ async def session_generator(self) -> AsyncGenerator[AsyncSession, Any]:
140147 return await session.get(User,id)
141148 ```
142149 """
143- if self ._session_scope . get () :
150+ if self .scoped :
144151 """If the current context has a session, return it."""
145152 yield self .session
146153 else :
@@ -210,6 +217,10 @@ def __init__(self, engine: Engine, commit_on_exit: bool = True, **session_option
210217 def session (self ) -> Session :
211218 return self .scoped_session ()
212219
220+ @property
221+ def scoped (self ) -> bool :
222+ return bool (self ._session_scope .get ())
223+
213224 def __call__ (self , scope : Any = None ):
214225 return SessionContextVarManager (self , scope = scope )
215226
@@ -223,7 +234,7 @@ def create(
223234 return cls (engine , ** session_options )
224235
225236 def session_generator (self ) -> Generator [Session , Any , None ]:
226- if self ._session_scope . get () :
237+ if self .scoped :
227238 """If the current context has a session, return it."""
228239 yield self .session
229240 else :
0 commit comments