Coverage for sqlmodel/ext/asyncio/session.py: 100%
32 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-16 00:02 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-16 00:02 +0000
1from collections.abc import Mapping, Sequence 1abcghidjkeflm
2from typing import ( 1abcghidjkeflm
3 Any,
4 TypeVar,
5 TypeVarTuple,
6 cast,
7 overload,
8)
10from sqlalchemy import util 1abcghidjkeflm
11from sqlalchemy.engine.cursor import CursorResult 1abcghidjkeflm
12from sqlalchemy.engine.interfaces import _CoreAnyExecuteParams 1abcghidjkeflm
13from sqlalchemy.engine.result import Result, ScalarResult, TupleResult 1abcghidjkeflm
14from sqlalchemy.ext.asyncio import AsyncSession as _AsyncSession 1abcghidjkeflm
15from sqlalchemy.ext.asyncio.result import _ensure_sync_result 1abcghidjkeflm
16from sqlalchemy.ext.asyncio.session import _EXECUTE_OPTIONS 1abcghidjkeflm
17from sqlalchemy.orm._typing import OrmExecuteOptionsParameter 1abcghidjkeflm
18from sqlalchemy.sql.base import Executable as _Executable 1abcghidjkeflm
19from sqlalchemy.sql.dml import UpdateBase 1abcghidjkeflm
20from sqlalchemy.util.concurrency import greenlet_spawn 1abcghidjkeflm
21from typing_extensions import deprecated 1abcghidjkeflm
23from ...orm.session import Session 1abcghidjkeflm
24from ...sql.base import Executable 1abcghidjkeflm
25from ...sql.expression import Select, SelectOfScalar 1abcghidjkeflm
27_TSelectParam = TypeVar("_TSelectParam", bound=Any) 1abcghidjkeflm
28_Ts = TypeVarTuple("_Ts") 1abcghidjkeflm
31class AsyncSession(_AsyncSession): 1abcghidjkeflm
32 sync_session_class: type[Session] = Session 1abcghidjkeflm
33 sync_session: Session 1abcdef
35 @overload 1abcghidjkeflm
36 async def exec( 1abcghidjkeflm
37 self,
38 statement: Select[*_Ts], 1abcdef
39 *,
40 params: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = None, 1abcghidjkeflm
41 execution_options: Mapping[str, Any] = util.EMPTY_DICT, 1abcghidjkeflm
42 bind_arguments: dict[str, Any] | None = None, 1abcghidjkeflm
43 _parent_execute_state: Any | None = None, 1abcghidjkeflm
44 _add_event: Any | None = None, 1abcghidjkeflm
45 ) -> TupleResult[tuple[*_Ts]]: ... 1abcdef
47 @overload 1abcghidjkeflm
48 async def exec( 1abcghidjkeflm
49 self,
50 statement: SelectOfScalar[_TSelectParam], 1abcdef
51 *,
52 params: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = None, 1abcghidjkeflm
53 execution_options: Mapping[str, Any] = util.EMPTY_DICT, 1abcghidjkeflm
54 bind_arguments: dict[str, Any] | None = None, 1abcghidjkeflm
55 _parent_execute_state: Any | None = None, 1abcghidjkeflm
56 _add_event: Any | None = None, 1abcghidjkeflm
57 ) -> ScalarResult[_TSelectParam]: ... 1abcdef
59 @overload 1abcghidjkeflm
60 async def exec( 1abcghidjkeflm
61 self,
62 statement: UpdateBase, 1abcdef
63 *,
64 params: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = None, 1abcghidjkeflm
65 execution_options: Mapping[str, Any] = util.EMPTY_DICT, 1abcghidjkeflm
66 bind_arguments: dict[str, Any] | None = None, 1abcghidjkeflm
67 _parent_execute_state: Any | None = None, 1abcghidjkeflm
68 _add_event: Any | None = None, 1abcghidjkeflm
69 ) -> CursorResult[*tuple[Any, ...]]: ... 1abcdef
71 async def exec( 1abcghidjkeflm
72 self,
73 statement: Select[*_Ts]
74 | SelectOfScalar[_TSelectParam]
75 | Executable[_TSelectParam]
76 | UpdateBase,
77 *,
78 params: Mapping[str, Any] | Sequence[Mapping[str, Any]] | None = None,
79 execution_options: Mapping[str, Any] = util.EMPTY_DICT,
80 bind_arguments: dict[str, Any] | None = None,
81 _parent_execute_state: Any | None = None,
82 _add_event: Any | None = None,
83 ) -> (
84 TupleResult[tuple[*_Ts]]
85 | ScalarResult[_TSelectParam]
86 | CursorResult[*tuple[Any, ...]]
87 ):
88 if execution_options: 1nopqrstuvwxyz
89 execution_options = util.immutabledict(execution_options).union( 1nopqrstuvwxyz
90 _EXECUTE_OPTIONS
91 )
92 else:
93 execution_options = _EXECUTE_OPTIONS 1nopqrstuvwxyz
95 result = await greenlet_spawn( 1nopqrstuvwxyz
96 self.sync_session.exec,
97 statement,
98 params=params,
99 execution_options=execution_options,
100 bind_arguments=bind_arguments,
101 _parent_execute_state=_parent_execute_state,
102 _add_event=_add_event,
103 )
104 result_value = await _ensure_sync_result( 1nopqrstuvwxyz
105 cast(Result[*tuple[Any, ...]], result), self.exec
106 )
107 return result_value # type: ignore 1nopqrstuvwxyz
109 @deprecated( 1abcghidjkeflm
110 """
111 🚨 You probably want to use `session.exec()` instead of `session.execute()`.
113 This is the original SQLAlchemy `session.execute()` method that returns objects
114 of type `Row`, on which you have to call `scalars()` to get the model objects.
116 For example:
118 ```Python
119 result = await session.execute(select(Hero))
120 heroes = result.scalars().all()
121 ```
123 Instead, you could use `exec()`:
125 ```Python
126 result = await session.exec(select(Hero))
127 heroes = result.all()
128 ```
129 """
130 )
131 async def execute( 1abcghidjkeflm
132 self,
133 statement: _Executable,
134 params: _CoreAnyExecuteParams | None = None,
135 *,
136 execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
137 bind_arguments: dict[str, Any] | None = None,
138 _parent_execute_state: Any | None = None,
139 _add_event: Any | None = None,
140 ) -> Result[*tuple[Any, ...]]:
141 """
142 🚨 You probably want to use `session.exec()` instead of `session.execute()`.
144 This is the original SQLAlchemy `session.execute()` method that returns objects
145 of type `Row`, on which you have to call `scalars()` to get the model objects.
147 For example:
149 ```Python
150 result = await session.execute(select(Hero))
151 heroes = result.scalars().all()
152 ```
154 Instead, you could use `exec()`:
156 ```Python
157 result = await session.exec(select(Hero))
158 heroes = result.all()
159 ```
160 """
161 return await super().execute( 1nopqrstuvwxyz
162 statement,
163 params=params,
164 execution_options=execution_options,
165 bind_arguments=bind_arguments,
166 _parent_execute_state=_parent_execute_state,
167 _add_event=_add_event,
168 )