SQLAlchemy 2.1.0b2__cp313-cp313t-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. sqlalchemy/__init__.py +298 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +171 -0
  4. sqlalchemy/connectors/asyncio.py +476 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/dialects/__init__.py +62 -0
  7. sqlalchemy/dialects/_typing.py +30 -0
  8. sqlalchemy/dialects/mssql/__init__.py +89 -0
  9. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  10. sqlalchemy/dialects/mssql/base.py +4166 -0
  11. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  12. sqlalchemy/dialects/mssql/json.py +140 -0
  13. sqlalchemy/dialects/mssql/mssqlpython.py +220 -0
  14. sqlalchemy/dialects/mssql/provision.py +196 -0
  15. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  16. sqlalchemy/dialects/mssql/pyodbc.py +698 -0
  17. sqlalchemy/dialects/mysql/__init__.py +106 -0
  18. sqlalchemy/dialects/mysql/_mariadb_shim.py +312 -0
  19. sqlalchemy/dialects/mysql/aiomysql.py +226 -0
  20. sqlalchemy/dialects/mysql/asyncmy.py +214 -0
  21. sqlalchemy/dialects/mysql/base.py +3877 -0
  22. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  23. sqlalchemy/dialects/mysql/dml.py +279 -0
  24. sqlalchemy/dialects/mysql/enumerated.py +277 -0
  25. sqlalchemy/dialects/mysql/expression.py +146 -0
  26. sqlalchemy/dialects/mysql/json.py +92 -0
  27. sqlalchemy/dialects/mysql/mariadb.py +67 -0
  28. sqlalchemy/dialects/mysql/mariadbconnector.py +330 -0
  29. sqlalchemy/dialects/mysql/mysqlconnector.py +296 -0
  30. sqlalchemy/dialects/mysql/mysqldb.py +312 -0
  31. sqlalchemy/dialects/mysql/provision.py +153 -0
  32. sqlalchemy/dialects/mysql/pymysql.py +157 -0
  33. sqlalchemy/dialects/mysql/pyodbc.py +156 -0
  34. sqlalchemy/dialects/mysql/reflection.py +724 -0
  35. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  36. sqlalchemy/dialects/mysql/types.py +845 -0
  37. sqlalchemy/dialects/oracle/__init__.py +85 -0
  38. sqlalchemy/dialects/oracle/base.py +3977 -0
  39. sqlalchemy/dialects/oracle/cx_oracle.py +1601 -0
  40. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  41. sqlalchemy/dialects/oracle/json.py +158 -0
  42. sqlalchemy/dialects/oracle/oracledb.py +909 -0
  43. sqlalchemy/dialects/oracle/provision.py +288 -0
  44. sqlalchemy/dialects/oracle/types.py +367 -0
  45. sqlalchemy/dialects/oracle/vector.py +368 -0
  46. sqlalchemy/dialects/postgresql/__init__.py +171 -0
  47. sqlalchemy/dialects/postgresql/_psycopg_common.py +229 -0
  48. sqlalchemy/dialects/postgresql/array.py +534 -0
  49. sqlalchemy/dialects/postgresql/asyncpg.py +1323 -0
  50. sqlalchemy/dialects/postgresql/base.py +5789 -0
  51. sqlalchemy/dialects/postgresql/bitstring.py +327 -0
  52. sqlalchemy/dialects/postgresql/dml.py +360 -0
  53. sqlalchemy/dialects/postgresql/ext.py +593 -0
  54. sqlalchemy/dialects/postgresql/hstore.py +423 -0
  55. sqlalchemy/dialects/postgresql/json.py +408 -0
  56. sqlalchemy/dialects/postgresql/named_types.py +521 -0
  57. sqlalchemy/dialects/postgresql/operators.py +130 -0
  58. sqlalchemy/dialects/postgresql/pg8000.py +670 -0
  59. sqlalchemy/dialects/postgresql/pg_catalog.py +344 -0
  60. sqlalchemy/dialects/postgresql/provision.py +184 -0
  61. sqlalchemy/dialects/postgresql/psycopg.py +799 -0
  62. sqlalchemy/dialects/postgresql/psycopg2.py +860 -0
  63. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  64. sqlalchemy/dialects/postgresql/ranges.py +1002 -0
  65. sqlalchemy/dialects/postgresql/types.py +388 -0
  66. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  67. sqlalchemy/dialects/sqlite/aiosqlite.py +321 -0
  68. sqlalchemy/dialects/sqlite/base.py +3063 -0
  69. sqlalchemy/dialects/sqlite/dml.py +279 -0
  70. sqlalchemy/dialects/sqlite/json.py +100 -0
  71. sqlalchemy/dialects/sqlite/provision.py +229 -0
  72. sqlalchemy/dialects/sqlite/pysqlcipher.py +161 -0
  73. sqlalchemy/dialects/sqlite/pysqlite.py +754 -0
  74. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  75. sqlalchemy/engine/__init__.py +62 -0
  76. sqlalchemy/engine/_processors_cy.cp313t-win_arm64.pyd +0 -0
  77. sqlalchemy/engine/_processors_cy.py +92 -0
  78. sqlalchemy/engine/_result_cy.cp313t-win_arm64.pyd +0 -0
  79. sqlalchemy/engine/_result_cy.py +633 -0
  80. sqlalchemy/engine/_row_cy.cp313t-win_arm64.pyd +0 -0
  81. sqlalchemy/engine/_row_cy.py +232 -0
  82. sqlalchemy/engine/_util_cy.cp313t-win_arm64.pyd +0 -0
  83. sqlalchemy/engine/_util_cy.py +136 -0
  84. sqlalchemy/engine/base.py +3354 -0
  85. sqlalchemy/engine/characteristics.py +155 -0
  86. sqlalchemy/engine/create.py +877 -0
  87. sqlalchemy/engine/cursor.py +2421 -0
  88. sqlalchemy/engine/default.py +2402 -0
  89. sqlalchemy/engine/events.py +965 -0
  90. sqlalchemy/engine/interfaces.py +3495 -0
  91. sqlalchemy/engine/mock.py +134 -0
  92. sqlalchemy/engine/processors.py +82 -0
  93. sqlalchemy/engine/reflection.py +2100 -0
  94. sqlalchemy/engine/result.py +1966 -0
  95. sqlalchemy/engine/row.py +397 -0
  96. sqlalchemy/engine/strategies.py +16 -0
  97. sqlalchemy/engine/url.py +922 -0
  98. sqlalchemy/engine/util.py +156 -0
  99. sqlalchemy/event/__init__.py +26 -0
  100. sqlalchemy/event/api.py +220 -0
  101. sqlalchemy/event/attr.py +674 -0
  102. sqlalchemy/event/base.py +472 -0
  103. sqlalchemy/event/legacy.py +258 -0
  104. sqlalchemy/event/registry.py +390 -0
  105. sqlalchemy/events.py +17 -0
  106. sqlalchemy/exc.py +922 -0
  107. sqlalchemy/ext/__init__.py +11 -0
  108. sqlalchemy/ext/associationproxy.py +2072 -0
  109. sqlalchemy/ext/asyncio/__init__.py +29 -0
  110. sqlalchemy/ext/asyncio/base.py +281 -0
  111. sqlalchemy/ext/asyncio/engine.py +1487 -0
  112. sqlalchemy/ext/asyncio/exc.py +21 -0
  113. sqlalchemy/ext/asyncio/result.py +994 -0
  114. sqlalchemy/ext/asyncio/scoping.py +1679 -0
  115. sqlalchemy/ext/asyncio/session.py +2007 -0
  116. sqlalchemy/ext/automap.py +1701 -0
  117. sqlalchemy/ext/baked.py +559 -0
  118. sqlalchemy/ext/compiler.py +600 -0
  119. sqlalchemy/ext/declarative/__init__.py +65 -0
  120. sqlalchemy/ext/declarative/extensions.py +560 -0
  121. sqlalchemy/ext/horizontal_shard.py +481 -0
  122. sqlalchemy/ext/hybrid.py +1877 -0
  123. sqlalchemy/ext/indexable.py +364 -0
  124. sqlalchemy/ext/instrumentation.py +450 -0
  125. sqlalchemy/ext/mutable.py +1081 -0
  126. sqlalchemy/ext/orderinglist.py +439 -0
  127. sqlalchemy/ext/serializer.py +185 -0
  128. sqlalchemy/future/__init__.py +16 -0
  129. sqlalchemy/future/engine.py +15 -0
  130. sqlalchemy/inspection.py +174 -0
  131. sqlalchemy/log.py +283 -0
  132. sqlalchemy/orm/__init__.py +176 -0
  133. sqlalchemy/orm/_orm_constructors.py +2694 -0
  134. sqlalchemy/orm/_typing.py +179 -0
  135. sqlalchemy/orm/attributes.py +2868 -0
  136. sqlalchemy/orm/base.py +976 -0
  137. sqlalchemy/orm/bulk_persistence.py +2152 -0
  138. sqlalchemy/orm/clsregistry.py +582 -0
  139. sqlalchemy/orm/collections.py +1568 -0
  140. sqlalchemy/orm/context.py +3471 -0
  141. sqlalchemy/orm/decl_api.py +2280 -0
  142. sqlalchemy/orm/decl_base.py +2309 -0
  143. sqlalchemy/orm/dependency.py +1306 -0
  144. sqlalchemy/orm/descriptor_props.py +1183 -0
  145. sqlalchemy/orm/dynamic.py +307 -0
  146. sqlalchemy/orm/evaluator.py +379 -0
  147. sqlalchemy/orm/events.py +3386 -0
  148. sqlalchemy/orm/exc.py +237 -0
  149. sqlalchemy/orm/identity.py +302 -0
  150. sqlalchemy/orm/instrumentation.py +746 -0
  151. sqlalchemy/orm/interfaces.py +1589 -0
  152. sqlalchemy/orm/loading.py +1684 -0
  153. sqlalchemy/orm/mapped_collection.py +557 -0
  154. sqlalchemy/orm/mapper.py +4411 -0
  155. sqlalchemy/orm/path_registry.py +829 -0
  156. sqlalchemy/orm/persistence.py +1789 -0
  157. sqlalchemy/orm/properties.py +973 -0
  158. sqlalchemy/orm/query.py +3528 -0
  159. sqlalchemy/orm/relationships.py +3570 -0
  160. sqlalchemy/orm/scoping.py +2232 -0
  161. sqlalchemy/orm/session.py +5403 -0
  162. sqlalchemy/orm/state.py +1175 -0
  163. sqlalchemy/orm/state_changes.py +196 -0
  164. sqlalchemy/orm/strategies.py +3492 -0
  165. sqlalchemy/orm/strategy_options.py +2562 -0
  166. sqlalchemy/orm/sync.py +164 -0
  167. sqlalchemy/orm/unitofwork.py +798 -0
  168. sqlalchemy/orm/util.py +2438 -0
  169. sqlalchemy/orm/writeonly.py +694 -0
  170. sqlalchemy/pool/__init__.py +41 -0
  171. sqlalchemy/pool/base.py +1522 -0
  172. sqlalchemy/pool/events.py +375 -0
  173. sqlalchemy/pool/impl.py +582 -0
  174. sqlalchemy/py.typed +0 -0
  175. sqlalchemy/schema.py +74 -0
  176. sqlalchemy/sql/__init__.py +156 -0
  177. sqlalchemy/sql/_annotated_cols.py +397 -0
  178. sqlalchemy/sql/_dml_constructors.py +132 -0
  179. sqlalchemy/sql/_elements_constructors.py +2164 -0
  180. sqlalchemy/sql/_orm_types.py +20 -0
  181. sqlalchemy/sql/_selectable_constructors.py +840 -0
  182. sqlalchemy/sql/_typing.py +487 -0
  183. sqlalchemy/sql/_util_cy.cp313t-win_arm64.pyd +0 -0
  184. sqlalchemy/sql/_util_cy.py +127 -0
  185. sqlalchemy/sql/annotation.py +590 -0
  186. sqlalchemy/sql/base.py +2699 -0
  187. sqlalchemy/sql/cache_key.py +1066 -0
  188. sqlalchemy/sql/coercions.py +1373 -0
  189. sqlalchemy/sql/compiler.py +8327 -0
  190. sqlalchemy/sql/crud.py +1815 -0
  191. sqlalchemy/sql/ddl.py +1928 -0
  192. sqlalchemy/sql/default_comparator.py +654 -0
  193. sqlalchemy/sql/dml.py +1977 -0
  194. sqlalchemy/sql/elements.py +6033 -0
  195. sqlalchemy/sql/events.py +458 -0
  196. sqlalchemy/sql/expression.py +172 -0
  197. sqlalchemy/sql/functions.py +2305 -0
  198. sqlalchemy/sql/lambdas.py +1443 -0
  199. sqlalchemy/sql/naming.py +209 -0
  200. sqlalchemy/sql/operators.py +2897 -0
  201. sqlalchemy/sql/roles.py +332 -0
  202. sqlalchemy/sql/schema.py +6703 -0
  203. sqlalchemy/sql/selectable.py +7553 -0
  204. sqlalchemy/sql/sqltypes.py +4093 -0
  205. sqlalchemy/sql/traversals.py +1042 -0
  206. sqlalchemy/sql/type_api.py +2446 -0
  207. sqlalchemy/sql/util.py +1495 -0
  208. sqlalchemy/sql/visitors.py +1157 -0
  209. sqlalchemy/testing/__init__.py +96 -0
  210. sqlalchemy/testing/assertions.py +1007 -0
  211. sqlalchemy/testing/assertsql.py +519 -0
  212. sqlalchemy/testing/asyncio.py +128 -0
  213. sqlalchemy/testing/config.py +440 -0
  214. sqlalchemy/testing/engines.py +483 -0
  215. sqlalchemy/testing/entities.py +117 -0
  216. sqlalchemy/testing/exclusions.py +476 -0
  217. sqlalchemy/testing/fixtures/__init__.py +30 -0
  218. sqlalchemy/testing/fixtures/base.py +384 -0
  219. sqlalchemy/testing/fixtures/mypy.py +247 -0
  220. sqlalchemy/testing/fixtures/orm.py +227 -0
  221. sqlalchemy/testing/fixtures/sql.py +538 -0
  222. sqlalchemy/testing/pickleable.py +155 -0
  223. sqlalchemy/testing/plugin/__init__.py +6 -0
  224. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  225. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  226. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  227. sqlalchemy/testing/profiling.py +329 -0
  228. sqlalchemy/testing/provision.py +613 -0
  229. sqlalchemy/testing/requirements.py +1978 -0
  230. sqlalchemy/testing/schema.py +198 -0
  231. sqlalchemy/testing/suite/__init__.py +19 -0
  232. sqlalchemy/testing/suite/test_cte.py +237 -0
  233. sqlalchemy/testing/suite/test_ddl.py +420 -0
  234. sqlalchemy/testing/suite/test_dialect.py +776 -0
  235. sqlalchemy/testing/suite/test_insert.py +630 -0
  236. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  237. sqlalchemy/testing/suite/test_results.py +660 -0
  238. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  239. sqlalchemy/testing/suite/test_select.py +2112 -0
  240. sqlalchemy/testing/suite/test_sequence.py +317 -0
  241. sqlalchemy/testing/suite/test_table_via_select.py +686 -0
  242. sqlalchemy/testing/suite/test_types.py +2271 -0
  243. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  244. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  245. sqlalchemy/testing/util.py +535 -0
  246. sqlalchemy/testing/warnings.py +52 -0
  247. sqlalchemy/types.py +76 -0
  248. sqlalchemy/util/__init__.py +158 -0
  249. sqlalchemy/util/_collections.py +688 -0
  250. sqlalchemy/util/_collections_cy.cp313t-win_arm64.pyd +0 -0
  251. sqlalchemy/util/_collections_cy.pxd +8 -0
  252. sqlalchemy/util/_collections_cy.py +516 -0
  253. sqlalchemy/util/_has_cython.py +46 -0
  254. sqlalchemy/util/_immutabledict_cy.cp313t-win_arm64.pyd +0 -0
  255. sqlalchemy/util/_immutabledict_cy.py +240 -0
  256. sqlalchemy/util/compat.py +299 -0
  257. sqlalchemy/util/concurrency.py +322 -0
  258. sqlalchemy/util/cython.py +79 -0
  259. sqlalchemy/util/deprecations.py +401 -0
  260. sqlalchemy/util/langhelpers.py +2320 -0
  261. sqlalchemy/util/preloaded.py +152 -0
  262. sqlalchemy/util/queue.py +304 -0
  263. sqlalchemy/util/tool_support.py +201 -0
  264. sqlalchemy/util/topological.py +120 -0
  265. sqlalchemy/util/typing.py +711 -0
  266. sqlalchemy-2.1.0b2.dist-info/METADATA +269 -0
  267. sqlalchemy-2.1.0b2.dist-info/RECORD +270 -0
  268. sqlalchemy-2.1.0b2.dist-info/WHEEL +5 -0
  269. sqlalchemy-2.1.0b2.dist-info/licenses/LICENSE +19 -0
  270. sqlalchemy-2.1.0b2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2007 @@
1
+ # ext/asyncio/session.py
2
+ # Copyright (C) 2020-2026 the SQLAlchemy authors and contributors
3
+ # <see AUTHORS file>
4
+ #
5
+ # This module is part of SQLAlchemy and is released under
6
+ # the MIT License: https://www.opensource.org/licenses/mit-license.php
7
+ from __future__ import annotations
8
+
9
+ import asyncio
10
+ from typing import Any
11
+ from typing import Awaitable
12
+ from typing import Callable
13
+ from typing import cast
14
+ from typing import Concatenate
15
+ from typing import Dict
16
+ from typing import Generic
17
+ from typing import Iterable
18
+ from typing import Iterator
19
+ from typing import NoReturn
20
+ from typing import Optional
21
+ from typing import overload
22
+ from typing import ParamSpec
23
+ from typing import Sequence
24
+ from typing import Tuple
25
+ from typing import Type
26
+ from typing import TYPE_CHECKING
27
+ from typing import TypeVar
28
+ from typing import Union
29
+
30
+ from . import engine
31
+ from .base import ReversibleProxy
32
+ from .base import StartableContext
33
+ from .result import _ensure_sync_result
34
+ from .result import AsyncResult
35
+ from .result import AsyncScalarResult
36
+ from ... import util
37
+ from ...orm import close_all_sessions as _sync_close_all_sessions
38
+ from ...orm import object_session
39
+ from ...orm import Session
40
+ from ...orm import SessionTransaction
41
+ from ...orm import state as _instance_state
42
+ from ...util.concurrency import greenlet_spawn
43
+ from ...util.typing import Never
44
+ from ...util.typing import TupleAny
45
+ from ...util.typing import TypeVarTuple
46
+ from ...util.typing import Unpack
47
+
48
+
49
+ if TYPE_CHECKING:
50
+ from .engine import AsyncConnection
51
+ from .engine import AsyncEngine
52
+ from ...engine import Connection
53
+ from ...engine import Engine
54
+ from ...engine import Result
55
+ from ...engine import Row
56
+ from ...engine import RowMapping
57
+ from ...engine import ScalarResult
58
+ from ...engine.interfaces import _CoreAnyExecuteParams
59
+ from ...engine.interfaces import _ExecuteOptions
60
+ from ...engine.interfaces import CoreExecuteOptionsParameter
61
+ from ...event import dispatcher
62
+ from ...orm._typing import _IdentityKeyType
63
+ from ...orm._typing import _O
64
+ from ...orm._typing import OrmExecuteOptionsParameter
65
+ from ...orm.identity import IdentityMap
66
+ from ...orm.interfaces import ORMOption
67
+ from ...orm.session import _BindArguments
68
+ from ...orm.session import _EntityBindKey
69
+ from ...orm.session import _PKIdentityArgument
70
+ from ...orm.session import _SessionBind
71
+ from ...orm.session import _SessionBindKey
72
+ from ...sql._typing import _InfoType
73
+ from ...sql.base import Executable
74
+ from ...sql.elements import ClauseElement
75
+ from ...sql.selectable import ForUpdateParameter
76
+ from ...sql.selectable import TypedReturnsRows
77
+
78
+ _AsyncSessionBind = Union["AsyncEngine", "AsyncConnection"]
79
+
80
+ _P = ParamSpec("_P")
81
+ _T = TypeVar("_T", bound=Any)
82
+ _Ts = TypeVarTuple("_Ts")
83
+
84
+ _EXECUTE_OPTIONS = util.immutabledict({"prebuffer_rows": True})
85
+ _STREAM_OPTIONS = util.immutabledict({"stream_results": True})
86
+
87
+
88
+ class AsyncAttrs:
89
+ """Mixin class which provides an awaitable accessor for all attributes.
90
+
91
+ E.g.::
92
+
93
+ from __future__ import annotations
94
+
95
+ from typing import List
96
+
97
+ from sqlalchemy import ForeignKey
98
+ from sqlalchemy import func
99
+ from sqlalchemy.ext.asyncio import AsyncAttrs
100
+ from sqlalchemy.orm import DeclarativeBase
101
+ from sqlalchemy.orm import Mapped
102
+ from sqlalchemy.orm import mapped_column
103
+ from sqlalchemy.orm import relationship
104
+
105
+
106
+ class Base(AsyncAttrs, DeclarativeBase):
107
+ pass
108
+
109
+
110
+ class A(Base):
111
+ __tablename__ = "a"
112
+
113
+ id: Mapped[int] = mapped_column(primary_key=True)
114
+ data: Mapped[str]
115
+ bs: Mapped[List[B]] = relationship()
116
+
117
+
118
+ class B(Base):
119
+ __tablename__ = "b"
120
+ id: Mapped[int] = mapped_column(primary_key=True)
121
+ a_id: Mapped[int] = mapped_column(ForeignKey("a.id"))
122
+ data: Mapped[str]
123
+
124
+ In the above example, the :class:`_asyncio.AsyncAttrs` mixin is applied to
125
+ the declarative ``Base`` class where it takes effect for all subclasses.
126
+ This mixin adds a single new attribute
127
+ :attr:`_asyncio.AsyncAttrs.awaitable_attrs` to all classes, which will
128
+ yield the value of any attribute as an awaitable. This allows attributes
129
+ which may be subject to lazy loading or deferred / unexpiry loading to be
130
+ accessed such that IO can still be emitted::
131
+
132
+ a1 = (await async_session.scalars(select(A).where(A.id == 5))).one()
133
+
134
+ # use the lazy loader on ``a1.bs`` via the ``.awaitable_attrs``
135
+ # interface, so that it may be awaited
136
+ for b1 in await a1.awaitable_attrs.bs:
137
+ print(b1)
138
+
139
+ The :attr:`_asyncio.AsyncAttrs.awaitable_attrs` performs a call against the
140
+ attribute that is approximately equivalent to using the
141
+ :meth:`_asyncio.AsyncSession.run_sync` method, e.g.::
142
+
143
+ for b1 in await async_session.run_sync(lambda sess: a1.bs):
144
+ print(b1)
145
+
146
+ .. versionadded:: 2.0.13
147
+
148
+ .. seealso::
149
+
150
+ :ref:`asyncio_orm_avoid_lazyloads`
151
+
152
+ """
153
+
154
+ class _AsyncAttrGetitem:
155
+ __slots__ = "_instance"
156
+
157
+ def __init__(self, _instance: Any):
158
+ self._instance = _instance
159
+
160
+ def __getattr__(self, name: str) -> Awaitable[Any]:
161
+ return greenlet_spawn(getattr, self._instance, name)
162
+
163
+ @property
164
+ def awaitable_attrs(self) -> AsyncAttrs._AsyncAttrGetitem:
165
+ """provide a namespace of all attributes on this object wrapped
166
+ as awaitables.
167
+
168
+ e.g.::
169
+
170
+
171
+ a1 = (await async_session.scalars(select(A).where(A.id == 5))).one()
172
+
173
+ some_attribute = await a1.awaitable_attrs.some_deferred_attribute
174
+ some_collection = await a1.awaitable_attrs.some_collection
175
+
176
+ """ # noqa: E501
177
+
178
+ return AsyncAttrs._AsyncAttrGetitem(self)
179
+
180
+
181
+ @util.create_proxy_methods(
182
+ Session,
183
+ ":class:`_orm.Session`",
184
+ ":class:`_asyncio.AsyncSession`",
185
+ classmethods=["object_session", "identity_key"],
186
+ methods=[
187
+ "__contains__",
188
+ "__iter__",
189
+ "add",
190
+ "add_all",
191
+ "expire",
192
+ "expire_all",
193
+ "expunge",
194
+ "expunge_all",
195
+ "is_modified",
196
+ "in_transaction",
197
+ "in_nested_transaction",
198
+ ],
199
+ attributes=[
200
+ "dirty",
201
+ "deleted",
202
+ "new",
203
+ "identity_map",
204
+ "is_active",
205
+ "autoflush",
206
+ "no_autoflush",
207
+ "info",
208
+ "execution_options",
209
+ ],
210
+ )
211
+ class AsyncSession(ReversibleProxy[Session]):
212
+ """Asyncio version of :class:`_orm.Session`.
213
+
214
+ The :class:`_asyncio.AsyncSession` is a proxy for a traditional
215
+ :class:`_orm.Session` instance.
216
+
217
+ The :class:`_asyncio.AsyncSession` is **not safe for use in concurrent
218
+ tasks.**. See :ref:`session_faq_threadsafe` for background.
219
+
220
+ .. versionadded:: 1.4
221
+
222
+ To use an :class:`_asyncio.AsyncSession` with custom :class:`_orm.Session`
223
+ implementations, see the
224
+ :paramref:`_asyncio.AsyncSession.sync_session_class` parameter.
225
+
226
+
227
+ """
228
+
229
+ _is_asyncio = True
230
+
231
+ dispatch: dispatcher[Session]
232
+
233
+ def __init__(
234
+ self,
235
+ bind: Optional[_AsyncSessionBind] = None,
236
+ *,
237
+ binds: Optional[Dict[_SessionBindKey, _AsyncSessionBind]] = None,
238
+ sync_session_class: Optional[Type[Session]] = None,
239
+ **kw: Any,
240
+ ):
241
+ r"""Construct a new :class:`_asyncio.AsyncSession`.
242
+
243
+ All parameters other than ``sync_session_class`` are passed to the
244
+ ``sync_session_class`` callable directly to instantiate a new
245
+ :class:`_orm.Session`. Refer to :meth:`_orm.Session.__init__` for
246
+ parameter documentation.
247
+
248
+ :param sync_session_class:
249
+ A :class:`_orm.Session` subclass or other callable which will be used
250
+ to construct the :class:`_orm.Session` which will be proxied. This
251
+ parameter may be used to provide custom :class:`_orm.Session`
252
+ subclasses. Defaults to the
253
+ :attr:`_asyncio.AsyncSession.sync_session_class` class-level
254
+ attribute.
255
+
256
+ .. versionadded:: 1.4.24
257
+
258
+ """
259
+ sync_bind = sync_binds = None
260
+
261
+ if bind:
262
+ self.bind = bind
263
+ sync_bind = engine._get_sync_engine_or_connection(bind)
264
+
265
+ if binds:
266
+ self.binds = binds
267
+ sync_binds = {
268
+ key: engine._get_sync_engine_or_connection(b)
269
+ for key, b in binds.items()
270
+ }
271
+
272
+ if sync_session_class:
273
+ self.sync_session_class = sync_session_class
274
+
275
+ self.sync_session = self._proxied = self._assign_proxied(
276
+ self.sync_session_class(bind=sync_bind, binds=sync_binds, **kw)
277
+ )
278
+
279
+ sync_session_class: Type[Session] = Session
280
+ """The class or callable that provides the
281
+ underlying :class:`_orm.Session` instance for a particular
282
+ :class:`_asyncio.AsyncSession`.
283
+
284
+ At the class level, this attribute is the default value for the
285
+ :paramref:`_asyncio.AsyncSession.sync_session_class` parameter. Custom
286
+ subclasses of :class:`_asyncio.AsyncSession` can override this.
287
+
288
+ At the instance level, this attribute indicates the current class or
289
+ callable that was used to provide the :class:`_orm.Session` instance for
290
+ this :class:`_asyncio.AsyncSession` instance.
291
+
292
+ .. versionadded:: 1.4.24
293
+
294
+ """
295
+
296
+ sync_session: Session
297
+ """Reference to the underlying :class:`_orm.Session` this
298
+ :class:`_asyncio.AsyncSession` proxies requests towards.
299
+
300
+ This instance can be used as an event target.
301
+
302
+ .. seealso::
303
+
304
+ :ref:`asyncio_events`
305
+
306
+ """
307
+
308
+ @classmethod
309
+ def _no_async_engine_events(cls) -> NoReturn:
310
+ raise NotImplementedError(
311
+ "asynchronous events are not implemented at this time. Apply "
312
+ "synchronous listeners to the AsyncSession.sync_session."
313
+ )
314
+
315
+ async def refresh(
316
+ self,
317
+ instance: object,
318
+ attribute_names: Optional[Iterable[str]] = None,
319
+ with_for_update: ForUpdateParameter = None,
320
+ ) -> None:
321
+ """Expire and refresh the attributes on the given instance.
322
+
323
+ A query will be issued to the database and all attributes will be
324
+ refreshed with their current database value.
325
+
326
+ This is the async version of the :meth:`_orm.Session.refresh` method.
327
+ See that method for a complete description of all options.
328
+
329
+ .. seealso::
330
+
331
+ :meth:`_orm.Session.refresh` - main documentation for refresh
332
+
333
+ """
334
+
335
+ await greenlet_spawn(
336
+ self.sync_session.refresh,
337
+ instance,
338
+ attribute_names=attribute_names,
339
+ with_for_update=with_for_update,
340
+ )
341
+
342
+ async def run_sync(
343
+ self,
344
+ fn: Callable[Concatenate[Session, _P], _T],
345
+ *arg: _P.args,
346
+ **kw: _P.kwargs,
347
+ ) -> _T:
348
+ '''Invoke the given synchronous (i.e. not async) callable,
349
+ passing a synchronous-style :class:`_orm.Session` as the first
350
+ argument.
351
+
352
+ This method allows traditional synchronous SQLAlchemy functions to
353
+ run within the context of an asyncio application.
354
+
355
+ E.g.::
356
+
357
+ def some_business_method(session: Session, param: str) -> str:
358
+ """A synchronous function that does not require awaiting
359
+
360
+ :param session: a SQLAlchemy Session, used synchronously
361
+
362
+ :return: an optional return value is supported
363
+
364
+ """
365
+ session.add(MyObject(param=param))
366
+ session.flush()
367
+ return "success"
368
+
369
+
370
+ async def do_something_async(async_engine: AsyncEngine) -> None:
371
+ """an async function that uses awaiting"""
372
+
373
+ with AsyncSession(async_engine) as async_session:
374
+ # run some_business_method() with a sync-style
375
+ # Session, proxied into an awaitable
376
+ return_code = await async_session.run_sync(
377
+ some_business_method, param="param1"
378
+ )
379
+ print(return_code)
380
+
381
+ This method maintains the asyncio event loop all the way through
382
+ to the database connection by running the given callable in a
383
+ specially instrumented greenlet.
384
+
385
+ .. tip::
386
+
387
+ The provided callable is invoked inline within the asyncio event
388
+ loop, and will block on traditional IO calls. IO within this
389
+ callable should only call into SQLAlchemy's asyncio database
390
+ APIs which will be properly adapted to the greenlet context.
391
+
392
+ .. seealso::
393
+
394
+ :class:`.AsyncAttrs` - a mixin for ORM mapped classes that provides
395
+ a similar feature more succinctly on a per-attribute basis
396
+
397
+ :meth:`.AsyncConnection.run_sync`
398
+
399
+ :ref:`session_run_sync`
400
+ ''' # noqa: E501
401
+
402
+ return await greenlet_spawn(
403
+ fn, self.sync_session, *arg, _require_await=False, **kw
404
+ )
405
+
406
+ @overload
407
+ async def execute(
408
+ self,
409
+ statement: TypedReturnsRows[Unpack[_Ts]],
410
+ params: Optional[_CoreAnyExecuteParams] = None,
411
+ *,
412
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
413
+ bind_arguments: Optional[_BindArguments] = None,
414
+ _parent_execute_state: Optional[Any] = None,
415
+ _add_event: Optional[Any] = None,
416
+ ) -> Result[Unpack[_Ts]]: ...
417
+
418
+ @overload
419
+ async def execute(
420
+ self,
421
+ statement: Executable,
422
+ params: Optional[_CoreAnyExecuteParams] = None,
423
+ *,
424
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
425
+ bind_arguments: Optional[_BindArguments] = None,
426
+ _parent_execute_state: Optional[Any] = None,
427
+ _add_event: Optional[Any] = None,
428
+ ) -> Result[Unpack[TupleAny]]: ...
429
+
430
+ async def execute(
431
+ self,
432
+ statement: Executable,
433
+ params: Optional[_CoreAnyExecuteParams] = None,
434
+ *,
435
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
436
+ bind_arguments: Optional[_BindArguments] = None,
437
+ **kw: Any,
438
+ ) -> Result[Unpack[TupleAny]]:
439
+ """Execute a statement and return a buffered
440
+ :class:`_engine.Result` object.
441
+
442
+ .. seealso::
443
+
444
+ :meth:`_orm.Session.execute` - main documentation for execute
445
+
446
+ """
447
+
448
+ if execution_options:
449
+ execution_options = util.immutabledict(execution_options).union(
450
+ _EXECUTE_OPTIONS
451
+ )
452
+ else:
453
+ execution_options = _EXECUTE_OPTIONS
454
+
455
+ result = await greenlet_spawn(
456
+ self.sync_session.execute,
457
+ statement,
458
+ params=params,
459
+ execution_options=execution_options,
460
+ bind_arguments=bind_arguments,
461
+ **kw,
462
+ )
463
+ return await _ensure_sync_result(result, self.execute)
464
+
465
+ # special case to handle mypy issue:
466
+ # https://github.com/python/mypy/issues/20651
467
+ @overload
468
+ async def scalar(
469
+ self,
470
+ statement: TypedReturnsRows[Never],
471
+ params: Optional[_CoreAnyExecuteParams] = None,
472
+ *,
473
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
474
+ bind_arguments: Optional[_BindArguments] = None,
475
+ **kw: Any,
476
+ ) -> Optional[Any]: ...
477
+
478
+ @overload
479
+ async def scalar(
480
+ self,
481
+ statement: TypedReturnsRows[_T],
482
+ params: Optional[_CoreAnyExecuteParams] = None,
483
+ *,
484
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
485
+ bind_arguments: Optional[_BindArguments] = None,
486
+ **kw: Any,
487
+ ) -> Optional[_T]: ...
488
+
489
+ @overload
490
+ async def scalar(
491
+ self,
492
+ statement: Executable,
493
+ params: Optional[_CoreAnyExecuteParams] = None,
494
+ *,
495
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
496
+ bind_arguments: Optional[_BindArguments] = None,
497
+ **kw: Any,
498
+ ) -> Any: ...
499
+
500
+ async def scalar(
501
+ self,
502
+ statement: Executable,
503
+ params: Optional[_CoreAnyExecuteParams] = None,
504
+ *,
505
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
506
+ bind_arguments: Optional[_BindArguments] = None,
507
+ **kw: Any,
508
+ ) -> Any:
509
+ """Execute a statement and return a scalar result.
510
+
511
+ .. seealso::
512
+
513
+ :meth:`_orm.Session.scalar` - main documentation for scalar
514
+
515
+ """
516
+
517
+ if execution_options:
518
+ execution_options = util.immutabledict(execution_options).union(
519
+ _EXECUTE_OPTIONS
520
+ )
521
+ else:
522
+ execution_options = _EXECUTE_OPTIONS
523
+
524
+ return await greenlet_spawn(
525
+ self.sync_session.scalar,
526
+ statement,
527
+ params=params,
528
+ execution_options=execution_options,
529
+ bind_arguments=bind_arguments,
530
+ **kw,
531
+ )
532
+
533
+ @overload
534
+ async def scalars(
535
+ self,
536
+ statement: TypedReturnsRows[_T],
537
+ params: Optional[_CoreAnyExecuteParams] = None,
538
+ *,
539
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
540
+ bind_arguments: Optional[_BindArguments] = None,
541
+ **kw: Any,
542
+ ) -> ScalarResult[_T]: ...
543
+
544
+ @overload
545
+ async def scalars(
546
+ self,
547
+ statement: Executable,
548
+ params: Optional[_CoreAnyExecuteParams] = None,
549
+ *,
550
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
551
+ bind_arguments: Optional[_BindArguments] = None,
552
+ **kw: Any,
553
+ ) -> ScalarResult[Any]: ...
554
+
555
+ async def scalars(
556
+ self,
557
+ statement: Executable,
558
+ params: Optional[_CoreAnyExecuteParams] = None,
559
+ *,
560
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
561
+ bind_arguments: Optional[_BindArguments] = None,
562
+ **kw: Any,
563
+ ) -> ScalarResult[Any]:
564
+ """Execute a statement and return scalar results.
565
+
566
+ :return: a :class:`_result.ScalarResult` object
567
+
568
+ .. versionadded:: 1.4.24 Added :meth:`_asyncio.AsyncSession.scalars`
569
+
570
+ .. versionadded:: 1.4.26 Added
571
+ :meth:`_asyncio.async_scoped_session.scalars`
572
+
573
+ .. seealso::
574
+
575
+ :meth:`_orm.Session.scalars` - main documentation for scalars
576
+
577
+ :meth:`_asyncio.AsyncSession.stream_scalars` - streaming version
578
+
579
+ """
580
+
581
+ result = await self.execute(
582
+ statement,
583
+ params=params,
584
+ execution_options=execution_options,
585
+ bind_arguments=bind_arguments,
586
+ **kw,
587
+ )
588
+ return result.scalars()
589
+
590
+ async def get(
591
+ self,
592
+ entity: _EntityBindKey[_O],
593
+ ident: _PKIdentityArgument,
594
+ *,
595
+ options: Optional[Sequence[ORMOption]] = None,
596
+ populate_existing: bool = False,
597
+ with_for_update: ForUpdateParameter = None,
598
+ identity_token: Optional[Any] = None,
599
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
600
+ ) -> Union[_O, None]:
601
+ """Return an instance based on the given primary key identifier,
602
+ or ``None`` if not found.
603
+
604
+ .. seealso::
605
+
606
+ :meth:`_orm.Session.get` - main documentation for get
607
+
608
+
609
+ """
610
+
611
+ return await greenlet_spawn(
612
+ cast("Callable[..., _O]", self.sync_session.get),
613
+ entity,
614
+ ident,
615
+ options=options,
616
+ populate_existing=populate_existing,
617
+ with_for_update=with_for_update,
618
+ identity_token=identity_token,
619
+ execution_options=execution_options,
620
+ )
621
+
622
+ async def get_one(
623
+ self,
624
+ entity: _EntityBindKey[_O],
625
+ ident: _PKIdentityArgument,
626
+ *,
627
+ options: Optional[Sequence[ORMOption]] = None,
628
+ populate_existing: bool = False,
629
+ with_for_update: ForUpdateParameter = None,
630
+ identity_token: Optional[Any] = None,
631
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
632
+ ) -> _O:
633
+ """Return an instance based on the given primary key identifier,
634
+ or raise an exception if not found.
635
+
636
+ Raises :class:`_exc.NoResultFound` if the query selects no rows.
637
+
638
+ ..versionadded: 2.0.22
639
+
640
+ .. seealso::
641
+
642
+ :meth:`_orm.Session.get_one` - main documentation for get_one
643
+
644
+ """
645
+
646
+ return await greenlet_spawn(
647
+ cast("Callable[..., _O]", self.sync_session.get_one),
648
+ entity,
649
+ ident,
650
+ options=options,
651
+ populate_existing=populate_existing,
652
+ with_for_update=with_for_update,
653
+ identity_token=identity_token,
654
+ execution_options=execution_options,
655
+ )
656
+
657
+ @overload
658
+ async def stream(
659
+ self,
660
+ statement: TypedReturnsRows[Unpack[_Ts]],
661
+ params: Optional[_CoreAnyExecuteParams] = None,
662
+ *,
663
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
664
+ bind_arguments: Optional[_BindArguments] = None,
665
+ **kw: Any,
666
+ ) -> AsyncResult[Unpack[_Ts]]: ...
667
+
668
+ @overload
669
+ async def stream(
670
+ self,
671
+ statement: Executable,
672
+ params: Optional[_CoreAnyExecuteParams] = None,
673
+ *,
674
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
675
+ bind_arguments: Optional[_BindArguments] = None,
676
+ **kw: Any,
677
+ ) -> AsyncResult[Unpack[TupleAny]]: ...
678
+
679
+ async def stream(
680
+ self,
681
+ statement: Executable,
682
+ params: Optional[_CoreAnyExecuteParams] = None,
683
+ *,
684
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
685
+ bind_arguments: Optional[_BindArguments] = None,
686
+ **kw: Any,
687
+ ) -> AsyncResult[Unpack[TupleAny]]:
688
+ """Execute a statement and return a streaming
689
+ :class:`_asyncio.AsyncResult` object.
690
+
691
+ """
692
+
693
+ if execution_options:
694
+ execution_options = util.immutabledict(execution_options).union(
695
+ _STREAM_OPTIONS
696
+ )
697
+ else:
698
+ execution_options = _STREAM_OPTIONS
699
+
700
+ result = await greenlet_spawn(
701
+ self.sync_session.execute,
702
+ statement,
703
+ params=params,
704
+ execution_options=execution_options,
705
+ bind_arguments=bind_arguments,
706
+ **kw,
707
+ )
708
+ return AsyncResult(result)
709
+
710
+ @overload
711
+ async def stream_scalars(
712
+ self,
713
+ statement: TypedReturnsRows[_T],
714
+ params: Optional[_CoreAnyExecuteParams] = None,
715
+ *,
716
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
717
+ bind_arguments: Optional[_BindArguments] = None,
718
+ **kw: Any,
719
+ ) -> AsyncScalarResult[_T]: ...
720
+
721
+ @overload
722
+ async def stream_scalars(
723
+ self,
724
+ statement: Executable,
725
+ params: Optional[_CoreAnyExecuteParams] = None,
726
+ *,
727
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
728
+ bind_arguments: Optional[_BindArguments] = None,
729
+ **kw: Any,
730
+ ) -> AsyncScalarResult[Any]: ...
731
+
732
+ async def stream_scalars(
733
+ self,
734
+ statement: Executable,
735
+ params: Optional[_CoreAnyExecuteParams] = None,
736
+ *,
737
+ execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
738
+ bind_arguments: Optional[_BindArguments] = None,
739
+ **kw: Any,
740
+ ) -> AsyncScalarResult[Any]:
741
+ """Execute a statement and return a stream of scalar results.
742
+
743
+ :return: an :class:`_asyncio.AsyncScalarResult` object
744
+
745
+ .. versionadded:: 1.4.24
746
+
747
+ .. seealso::
748
+
749
+ :meth:`_orm.Session.scalars` - main documentation for scalars
750
+
751
+ :meth:`_asyncio.AsyncSession.scalars` - non streaming version
752
+
753
+ """
754
+
755
+ result = await self.stream(
756
+ statement,
757
+ params=params,
758
+ execution_options=execution_options,
759
+ bind_arguments=bind_arguments,
760
+ **kw,
761
+ )
762
+ return result.scalars()
763
+
764
+ async def delete(self, instance: object) -> None:
765
+ """Mark an instance as deleted.
766
+
767
+ The database delete operation occurs upon ``flush()``.
768
+
769
+ As this operation may need to cascade along unloaded relationships,
770
+ it is awaitable to allow for those queries to take place.
771
+
772
+ .. seealso::
773
+
774
+ :meth:`_orm.Session.delete` - main documentation for delete
775
+
776
+ """
777
+ await greenlet_spawn(self.sync_session.delete, instance)
778
+
779
+ async def delete_all(self, instances: Iterable[object]) -> None:
780
+ """Calls :meth:`.AsyncSession.delete` on multiple instances.
781
+
782
+ .. seealso::
783
+
784
+ :meth:`_orm.Session.delete_all` - main documentation for delete_all
785
+
786
+ """
787
+ await greenlet_spawn(self.sync_session.delete_all, instances)
788
+
789
+ async def merge(
790
+ self,
791
+ instance: _O,
792
+ *,
793
+ load: bool = True,
794
+ options: Optional[Sequence[ORMOption]] = None,
795
+ ) -> _O:
796
+ """Copy the state of a given instance into a corresponding instance
797
+ within this :class:`_asyncio.AsyncSession`.
798
+
799
+ .. seealso::
800
+
801
+ :meth:`_orm.Session.merge` - main documentation for merge
802
+
803
+ """
804
+ return await greenlet_spawn(
805
+ self.sync_session.merge, instance, load=load, options=options
806
+ )
807
+
808
+ async def merge_all(
809
+ self,
810
+ instances: Iterable[_O],
811
+ *,
812
+ load: bool = True,
813
+ options: Optional[Sequence[ORMOption]] = None,
814
+ ) -> Sequence[_O]:
815
+ """Calls :meth:`.AsyncSession.merge` on multiple instances.
816
+
817
+ .. seealso::
818
+
819
+ :meth:`_orm.Session.merge_all` - main documentation for merge_all
820
+
821
+ """
822
+ return await greenlet_spawn(
823
+ self.sync_session.merge_all, instances, load=load, options=options
824
+ )
825
+
826
+ async def flush(self, objects: Optional[Sequence[Any]] = None) -> None:
827
+ """Flush all the object changes to the database.
828
+
829
+ .. seealso::
830
+
831
+ :meth:`_orm.Session.flush` - main documentation for flush
832
+
833
+ """
834
+ await greenlet_spawn(self.sync_session.flush, objects=objects)
835
+
836
+ def get_transaction(self) -> Optional[AsyncSessionTransaction]:
837
+ """Return the current root transaction in progress, if any.
838
+
839
+ :return: an :class:`_asyncio.AsyncSessionTransaction` object, or
840
+ ``None``.
841
+
842
+ .. versionadded:: 1.4.18
843
+
844
+ """
845
+ trans = self.sync_session.get_transaction()
846
+ if trans is not None:
847
+ return AsyncSessionTransaction._retrieve_proxy_for_target(
848
+ trans, async_session=self
849
+ )
850
+ else:
851
+ return None
852
+
853
+ def get_nested_transaction(self) -> Optional[AsyncSessionTransaction]:
854
+ """Return the current nested transaction in progress, if any.
855
+
856
+ :return: an :class:`_asyncio.AsyncSessionTransaction` object, or
857
+ ``None``.
858
+
859
+ .. versionadded:: 1.4.18
860
+
861
+ """
862
+
863
+ trans = self.sync_session.get_nested_transaction()
864
+ if trans is not None:
865
+ return AsyncSessionTransaction._retrieve_proxy_for_target(
866
+ trans, async_session=self
867
+ )
868
+ else:
869
+ return None
870
+
871
+ def get_bind(
872
+ self,
873
+ mapper: Optional[_EntityBindKey[_O]] = None,
874
+ clause: Optional[ClauseElement] = None,
875
+ bind: Optional[_SessionBind] = None,
876
+ **kw: Any,
877
+ ) -> Union[Engine, Connection]:
878
+ """Return a "bind" to which the synchronous proxied :class:`_orm.Session`
879
+ is bound.
880
+
881
+ Unlike the :meth:`_orm.Session.get_bind` method, this method is
882
+ currently **not** used by this :class:`.AsyncSession` in any way
883
+ in order to resolve engines for requests.
884
+
885
+ .. note::
886
+
887
+ This method proxies directly to the :meth:`_orm.Session.get_bind`
888
+ method, however is currently **not** useful as an override target,
889
+ in contrast to that of the :meth:`_orm.Session.get_bind` method.
890
+ The example below illustrates how to implement custom
891
+ :meth:`_orm.Session.get_bind` schemes that work with
892
+ :class:`.AsyncSession` and :class:`.AsyncEngine`.
893
+
894
+ The pattern introduced at :ref:`session_custom_partitioning`
895
+ illustrates how to apply a custom bind-lookup scheme to a
896
+ :class:`_orm.Session` given a set of :class:`_engine.Engine` objects.
897
+ To apply a corresponding :meth:`_orm.Session.get_bind` implementation
898
+ for use with a :class:`.AsyncSession` and :class:`.AsyncEngine`
899
+ objects, continue to subclass :class:`_orm.Session` and apply it to
900
+ :class:`.AsyncSession` using
901
+ :paramref:`.AsyncSession.sync_session_class`. The inner method must
902
+ continue to return :class:`_engine.Engine` instances, which can be
903
+ acquired from a :class:`_asyncio.AsyncEngine` using the
904
+ :attr:`_asyncio.AsyncEngine.sync_engine` attribute::
905
+
906
+ # using example from "Custom Vertical Partitioning"
907
+
908
+
909
+ import random
910
+
911
+ from sqlalchemy.ext.asyncio import AsyncSession
912
+ from sqlalchemy.ext.asyncio import create_async_engine
913
+ from sqlalchemy.ext.asyncio import async_sessionmaker
914
+ from sqlalchemy.orm import Session
915
+
916
+ # construct async engines w/ async drivers
917
+ engines = {
918
+ "leader": create_async_engine("sqlite+aiosqlite:///leader.db"),
919
+ "other": create_async_engine("sqlite+aiosqlite:///other.db"),
920
+ "follower1": create_async_engine("sqlite+aiosqlite:///follower1.db"),
921
+ "follower2": create_async_engine("sqlite+aiosqlite:///follower2.db"),
922
+ }
923
+
924
+
925
+ class RoutingSession(Session):
926
+ def get_bind(self, mapper=None, clause=None, **kw):
927
+ # within get_bind(), return sync engines
928
+ if mapper and issubclass(mapper.class_, MyOtherClass):
929
+ return engines["other"].sync_engine
930
+ elif self._flushing or isinstance(clause, (Update, Delete)):
931
+ return engines["leader"].sync_engine
932
+ else:
933
+ return engines[
934
+ random.choice(["follower1", "follower2"])
935
+ ].sync_engine
936
+
937
+
938
+ # apply to AsyncSession using sync_session_class
939
+ AsyncSessionMaker = async_sessionmaker(sync_session_class=RoutingSession)
940
+
941
+ The :meth:`_orm.Session.get_bind` method is called in a non-asyncio,
942
+ implicitly non-blocking context in the same manner as ORM event hooks
943
+ and functions that are invoked via :meth:`.AsyncSession.run_sync`, so
944
+ routines that wish to run SQL commands inside of
945
+ :meth:`_orm.Session.get_bind` can continue to do so using
946
+ blocking-style code, which will be translated to implicitly async calls
947
+ at the point of invoking IO on the database drivers.
948
+
949
+ """ # noqa: E501
950
+
951
+ return self.sync_session.get_bind(
952
+ mapper=mapper, clause=clause, bind=bind, **kw
953
+ )
954
+
955
+ async def connection(
956
+ self,
957
+ bind_arguments: Optional[_BindArguments] = None,
958
+ execution_options: Optional[CoreExecuteOptionsParameter] = None,
959
+ **kw: Any,
960
+ ) -> AsyncConnection:
961
+ r"""Return a :class:`_asyncio.AsyncConnection` object corresponding to
962
+ this :class:`.Session` object's transactional state.
963
+
964
+ This method may also be used to establish execution options for the
965
+ database connection used by the current transaction.
966
+
967
+ .. versionadded:: 1.4.24 Added \**kw arguments which are passed
968
+ through to the underlying :meth:`_orm.Session.connection` method.
969
+
970
+ .. seealso::
971
+
972
+ :meth:`_orm.Session.connection` - main documentation for
973
+ "connection"
974
+
975
+ """
976
+
977
+ sync_connection = await greenlet_spawn(
978
+ self.sync_session.connection,
979
+ bind_arguments=bind_arguments,
980
+ execution_options=execution_options,
981
+ **kw,
982
+ )
983
+ return engine.AsyncConnection._retrieve_proxy_for_target(
984
+ sync_connection
985
+ )
986
+
987
+ def begin(self) -> AsyncSessionTransaction:
988
+ """Return an :class:`_asyncio.AsyncSessionTransaction` object.
989
+
990
+ The underlying :class:`_orm.Session` will perform the
991
+ "begin" action when the :class:`_asyncio.AsyncSessionTransaction`
992
+ object is entered::
993
+
994
+ async with async_session.begin():
995
+ ... # ORM transaction is begun
996
+
997
+ Note that database IO will not normally occur when the session-level
998
+ transaction is begun, as database transactions begin on an
999
+ on-demand basis. However, the begin block is async to accommodate
1000
+ for a :meth:`_orm.SessionEvents.after_transaction_create`
1001
+ event hook that may perform IO.
1002
+
1003
+ For a general description of ORM begin, see
1004
+ :meth:`_orm.Session.begin`.
1005
+
1006
+ """
1007
+
1008
+ return AsyncSessionTransaction(self)
1009
+
1010
+ def begin_nested(self) -> AsyncSessionTransaction:
1011
+ """Return an :class:`_asyncio.AsyncSessionTransaction` object
1012
+ which will begin a "nested" transaction, e.g. SAVEPOINT.
1013
+
1014
+ Behavior is the same as that of :meth:`_asyncio.AsyncSession.begin`.
1015
+
1016
+ For a general description of ORM begin nested, see
1017
+ :meth:`_orm.Session.begin_nested`.
1018
+
1019
+ .. seealso::
1020
+
1021
+ :ref:`aiosqlite_serializable` - special workarounds required
1022
+ with the SQLite asyncio driver in order for SAVEPOINT to work
1023
+ correctly.
1024
+
1025
+ """
1026
+
1027
+ return AsyncSessionTransaction(self, nested=True)
1028
+
1029
+ async def rollback(self) -> None:
1030
+ """Rollback the current transaction in progress.
1031
+
1032
+ .. seealso::
1033
+
1034
+ :meth:`_orm.Session.rollback` - main documentation for
1035
+ "rollback"
1036
+ """
1037
+ await greenlet_spawn(self.sync_session.rollback)
1038
+
1039
+ async def commit(self) -> None:
1040
+ """Commit the current transaction in progress.
1041
+
1042
+ .. seealso::
1043
+
1044
+ :meth:`_orm.Session.commit` - main documentation for
1045
+ "commit"
1046
+ """
1047
+ await greenlet_spawn(self.sync_session.commit)
1048
+
1049
+ async def close(self) -> None:
1050
+ """Close out the transactional resources and ORM objects used by this
1051
+ :class:`_asyncio.AsyncSession`.
1052
+
1053
+ .. seealso::
1054
+
1055
+ :meth:`_orm.Session.close` - main documentation for
1056
+ "close"
1057
+
1058
+ :ref:`session_closing` - detail on the semantics of
1059
+ :meth:`_asyncio.AsyncSession.close` and
1060
+ :meth:`_asyncio.AsyncSession.reset`.
1061
+
1062
+ """
1063
+ await greenlet_spawn(self.sync_session.close)
1064
+
1065
+ async def reset(self) -> None:
1066
+ """Close out the transactional resources and ORM objects used by this
1067
+ :class:`_orm.Session`, resetting the session to its initial state.
1068
+
1069
+ .. versionadded:: 2.0.22
1070
+
1071
+ .. seealso::
1072
+
1073
+ :meth:`_orm.Session.reset` - main documentation for
1074
+ "reset"
1075
+
1076
+ :ref:`session_closing` - detail on the semantics of
1077
+ :meth:`_asyncio.AsyncSession.close` and
1078
+ :meth:`_asyncio.AsyncSession.reset`.
1079
+
1080
+ """
1081
+ await greenlet_spawn(self.sync_session.reset)
1082
+
1083
+ async def aclose(self) -> None:
1084
+ """A synonym for :meth:`_asyncio.AsyncSession.close`.
1085
+
1086
+ The :meth:`_asyncio.AsyncSession.aclose` name is specifically
1087
+ to support the Python standard library ``@contextlib.aclosing``
1088
+ context manager function.
1089
+
1090
+ .. versionadded:: 2.0.20
1091
+
1092
+ """
1093
+ await self.close()
1094
+
1095
+ async def invalidate(self) -> None:
1096
+ """Close this Session, using connection invalidation.
1097
+
1098
+ For a complete description, see :meth:`_orm.Session.invalidate`.
1099
+ """
1100
+ await greenlet_spawn(self.sync_session.invalidate)
1101
+
1102
+ @classmethod
1103
+ @util.deprecated(
1104
+ "2.0",
1105
+ "The :meth:`.AsyncSession.close_all` method is deprecated and will be "
1106
+ "removed in a future release. Please refer to "
1107
+ ":func:`_asyncio.close_all_sessions`.",
1108
+ )
1109
+ async def close_all(cls) -> None:
1110
+ """Close all :class:`_asyncio.AsyncSession` sessions."""
1111
+ await close_all_sessions()
1112
+
1113
+ async def __aenter__(self: _AS) -> _AS:
1114
+ return self
1115
+
1116
+ async def __aexit__(self, type_: Any, value: Any, traceback: Any) -> None:
1117
+ task = asyncio.create_task(self.close())
1118
+ await asyncio.shield(task)
1119
+
1120
+ def _maker_context_manager(self: _AS) -> _AsyncSessionContextManager[_AS]:
1121
+ return _AsyncSessionContextManager(self)
1122
+
1123
+ # START PROXY METHODS AsyncSession
1124
+
1125
+ # code within this block is **programmatically,
1126
+ # statically generated** by tools/generate_proxy_methods.py
1127
+
1128
+ def __contains__(self, instance: object) -> bool:
1129
+ r"""Return True if the instance is associated with this session.
1130
+
1131
+ .. container:: class_bases
1132
+
1133
+ Proxied for the :class:`_orm.Session` class on
1134
+ behalf of the :class:`_asyncio.AsyncSession` class.
1135
+
1136
+ The instance may be pending or persistent within the Session for a
1137
+ result of True.
1138
+
1139
+
1140
+ """ # noqa: E501
1141
+
1142
+ return self._proxied.__contains__(instance)
1143
+
1144
+ def __iter__(self) -> Iterator[object]:
1145
+ r"""Iterate over all pending or persistent instances within this
1146
+ Session.
1147
+
1148
+ .. container:: class_bases
1149
+
1150
+ Proxied for the :class:`_orm.Session` class on
1151
+ behalf of the :class:`_asyncio.AsyncSession` class.
1152
+
1153
+
1154
+ """ # noqa: E501
1155
+
1156
+ return self._proxied.__iter__()
1157
+
1158
+ def add(self, instance: object, *, _warn: bool = True) -> None:
1159
+ r"""Place an object into this :class:`_orm.Session`.
1160
+
1161
+ .. container:: class_bases
1162
+
1163
+ Proxied for the :class:`_orm.Session` class on
1164
+ behalf of the :class:`_asyncio.AsyncSession` class.
1165
+
1166
+ Objects that are in the :term:`transient` state when passed to the
1167
+ :meth:`_orm.Session.add` method will move to the
1168
+ :term:`pending` state, until the next flush, at which point they
1169
+ will move to the :term:`persistent` state.
1170
+
1171
+ Objects that are in the :term:`detached` state when passed to the
1172
+ :meth:`_orm.Session.add` method will move to the :term:`persistent`
1173
+ state directly.
1174
+
1175
+ If the transaction used by the :class:`_orm.Session` is rolled back,
1176
+ objects which were transient when they were passed to
1177
+ :meth:`_orm.Session.add` will be moved back to the
1178
+ :term:`transient` state, and will no longer be present within this
1179
+ :class:`_orm.Session`.
1180
+
1181
+ .. seealso::
1182
+
1183
+ :meth:`_orm.Session.add_all`
1184
+
1185
+ :ref:`session_adding` - at :ref:`session_basics`
1186
+
1187
+
1188
+ """ # noqa: E501
1189
+
1190
+ return self._proxied.add(instance, _warn=_warn)
1191
+
1192
+ def add_all(self, instances: Iterable[object]) -> None:
1193
+ r"""Add the given collection of instances to this :class:`_orm.Session`.
1194
+
1195
+ .. container:: class_bases
1196
+
1197
+ Proxied for the :class:`_orm.Session` class on
1198
+ behalf of the :class:`_asyncio.AsyncSession` class.
1199
+
1200
+ See the documentation for :meth:`_orm.Session.add` for a general
1201
+ behavioral description.
1202
+
1203
+ .. seealso::
1204
+
1205
+ :meth:`_orm.Session.add`
1206
+
1207
+ :ref:`session_adding` - at :ref:`session_basics`
1208
+
1209
+
1210
+ """ # noqa: E501
1211
+
1212
+ return self._proxied.add_all(instances)
1213
+
1214
+ def expire(
1215
+ self, instance: object, attribute_names: Optional[Iterable[str]] = None
1216
+ ) -> None:
1217
+ r"""Expire the attributes on an instance.
1218
+
1219
+ .. container:: class_bases
1220
+
1221
+ Proxied for the :class:`_orm.Session` class on
1222
+ behalf of the :class:`_asyncio.AsyncSession` class.
1223
+
1224
+ Marks the attributes of an instance as out of date. When an expired
1225
+ attribute is next accessed, a query will be issued to the
1226
+ :class:`.Session` object's current transactional context in order to
1227
+ load all expired attributes for the given instance. Note that
1228
+ a highly isolated transaction will return the same values as were
1229
+ previously read in that same transaction, regardless of changes
1230
+ in database state outside of that transaction.
1231
+
1232
+ To expire all objects in the :class:`.Session` simultaneously,
1233
+ use :meth:`Session.expire_all`.
1234
+
1235
+ The :class:`.Session` object's default behavior is to
1236
+ expire all state whenever the :meth:`Session.rollback`
1237
+ or :meth:`Session.commit` methods are called, so that new
1238
+ state can be loaded for the new transaction. For this reason,
1239
+ calling :meth:`Session.expire` only makes sense for the specific
1240
+ case that a non-ORM SQL statement was emitted in the current
1241
+ transaction.
1242
+
1243
+ :param instance: The instance to be refreshed.
1244
+ :param attribute_names: optional list of string attribute names
1245
+ indicating a subset of attributes to be expired.
1246
+
1247
+ .. seealso::
1248
+
1249
+ :ref:`session_expire` - introductory material
1250
+
1251
+ :meth:`.Session.expire`
1252
+
1253
+ :meth:`.Session.refresh`
1254
+
1255
+ :meth:`_orm.Query.populate_existing`
1256
+
1257
+
1258
+ """ # noqa: E501
1259
+
1260
+ return self._proxied.expire(instance, attribute_names=attribute_names)
1261
+
1262
+ def expire_all(self) -> None:
1263
+ r"""Expires all persistent instances within this Session.
1264
+
1265
+ .. container:: class_bases
1266
+
1267
+ Proxied for the :class:`_orm.Session` class on
1268
+ behalf of the :class:`_asyncio.AsyncSession` class.
1269
+
1270
+ When any attributes on a persistent instance is next accessed,
1271
+ a query will be issued using the
1272
+ :class:`.Session` object's current transactional context in order to
1273
+ load all expired attributes for the given instance. Note that
1274
+ a highly isolated transaction will return the same values as were
1275
+ previously read in that same transaction, regardless of changes
1276
+ in database state outside of that transaction.
1277
+
1278
+ To expire individual objects and individual attributes
1279
+ on those objects, use :meth:`Session.expire`.
1280
+
1281
+ The :class:`.Session` object's default behavior is to
1282
+ expire all state whenever the :meth:`Session.rollback`
1283
+ or :meth:`Session.commit` methods are called, so that new
1284
+ state can be loaded for the new transaction. For this reason,
1285
+ calling :meth:`Session.expire_all` is not usually needed,
1286
+ assuming the transaction is isolated.
1287
+
1288
+ .. seealso::
1289
+
1290
+ :ref:`session_expire` - introductory material
1291
+
1292
+ :meth:`.Session.expire`
1293
+
1294
+ :meth:`.Session.refresh`
1295
+
1296
+ :meth:`_orm.Query.populate_existing`
1297
+
1298
+
1299
+ """ # noqa: E501
1300
+
1301
+ return self._proxied.expire_all()
1302
+
1303
+ def expunge(self, instance: object) -> None:
1304
+ r"""Remove the `instance` from this ``Session``.
1305
+
1306
+ .. container:: class_bases
1307
+
1308
+ Proxied for the :class:`_orm.Session` class on
1309
+ behalf of the :class:`_asyncio.AsyncSession` class.
1310
+
1311
+ This will free all internal references to the instance. Cascading
1312
+ will be applied according to the *expunge* cascade rule.
1313
+
1314
+
1315
+ """ # noqa: E501
1316
+
1317
+ return self._proxied.expunge(instance)
1318
+
1319
+ def expunge_all(self) -> None:
1320
+ r"""Remove all object instances from this ``Session``.
1321
+
1322
+ .. container:: class_bases
1323
+
1324
+ Proxied for the :class:`_orm.Session` class on
1325
+ behalf of the :class:`_asyncio.AsyncSession` class.
1326
+
1327
+ This is equivalent to calling ``expunge(obj)`` on all objects in this
1328
+ ``Session``.
1329
+
1330
+
1331
+ """ # noqa: E501
1332
+
1333
+ return self._proxied.expunge_all()
1334
+
1335
+ def is_modified(
1336
+ self, instance: object, include_collections: bool = True
1337
+ ) -> bool:
1338
+ r"""Return ``True`` if the given instance has locally
1339
+ modified attributes.
1340
+
1341
+ .. container:: class_bases
1342
+
1343
+ Proxied for the :class:`_orm.Session` class on
1344
+ behalf of the :class:`_asyncio.AsyncSession` class.
1345
+
1346
+ This method retrieves the history for each instrumented
1347
+ attribute on the instance and performs a comparison of the current
1348
+ value to its previously flushed or committed value, if any.
1349
+
1350
+ It is in effect a more expensive and accurate
1351
+ version of checking for the given instance in the
1352
+ :attr:`.Session.dirty` collection; a full test for
1353
+ each attribute's net "dirty" status is performed.
1354
+
1355
+ E.g.::
1356
+
1357
+ return session.is_modified(someobject)
1358
+
1359
+ A few caveats to this method apply:
1360
+
1361
+ * Instances present in the :attr:`.Session.dirty` collection may
1362
+ report ``False`` when tested with this method. This is because
1363
+ the object may have received change events via attribute mutation,
1364
+ thus placing it in :attr:`.Session.dirty`, but ultimately the state
1365
+ is the same as that loaded from the database, resulting in no net
1366
+ change here.
1367
+ * Scalar attributes may not have recorded the previously set
1368
+ value when a new value was applied, if the attribute was not loaded,
1369
+ or was expired, at the time the new value was received - in these
1370
+ cases, the attribute is assumed to have a change, even if there is
1371
+ ultimately no net change against its database value. SQLAlchemy in
1372
+ most cases does not need the "old" value when a set event occurs, so
1373
+ it skips the expense of a SQL call if the old value isn't present,
1374
+ based on the assumption that an UPDATE of the scalar value is
1375
+ usually needed, and in those few cases where it isn't, is less
1376
+ expensive on average than issuing a defensive SELECT.
1377
+
1378
+ The "old" value is fetched unconditionally upon set only if the
1379
+ attribute container has the ``active_history`` flag set to ``True``.
1380
+ This flag is set typically for primary key attributes and scalar
1381
+ object references that are not a simple many-to-one. To set this
1382
+ flag for any arbitrary mapped column, use the ``active_history``
1383
+ argument with :func:`.column_property`.
1384
+
1385
+ :param instance: mapped instance to be tested for pending changes.
1386
+ :param include_collections: Indicates if multivalued collections
1387
+ should be included in the operation. Setting this to ``False`` is a
1388
+ way to detect only local-column based properties (i.e. scalar columns
1389
+ or many-to-one foreign keys) that would result in an UPDATE for this
1390
+ instance upon flush.
1391
+
1392
+
1393
+ """ # noqa: E501
1394
+
1395
+ return self._proxied.is_modified(
1396
+ instance, include_collections=include_collections
1397
+ )
1398
+
1399
+ def in_transaction(self) -> bool:
1400
+ r"""Return True if this :class:`_orm.Session` has begun a transaction.
1401
+
1402
+ .. container:: class_bases
1403
+
1404
+ Proxied for the :class:`_orm.Session` class on
1405
+ behalf of the :class:`_asyncio.AsyncSession` class.
1406
+
1407
+ .. versionadded:: 1.4
1408
+
1409
+ .. seealso::
1410
+
1411
+ :attr:`_orm.Session.is_active`
1412
+
1413
+
1414
+
1415
+ """ # noqa: E501
1416
+
1417
+ return self._proxied.in_transaction()
1418
+
1419
+ def in_nested_transaction(self) -> bool:
1420
+ r"""Return True if this :class:`_orm.Session` has begun a nested
1421
+ transaction, e.g. SAVEPOINT.
1422
+
1423
+ .. container:: class_bases
1424
+
1425
+ Proxied for the :class:`_orm.Session` class on
1426
+ behalf of the :class:`_asyncio.AsyncSession` class.
1427
+
1428
+ .. versionadded:: 1.4
1429
+
1430
+
1431
+ """ # noqa: E501
1432
+
1433
+ return self._proxied.in_nested_transaction()
1434
+
1435
+ @property
1436
+ def dirty(self) -> Any:
1437
+ r"""The set of all persistent instances considered dirty.
1438
+
1439
+ .. container:: class_bases
1440
+
1441
+ Proxied for the :class:`_orm.Session` class
1442
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1443
+
1444
+ E.g.::
1445
+
1446
+ some_mapped_object in session.dirty
1447
+
1448
+ Instances are considered dirty when they were modified but not
1449
+ deleted.
1450
+
1451
+ Note that this 'dirty' calculation is 'optimistic'; most
1452
+ attribute-setting or collection modification operations will
1453
+ mark an instance as 'dirty' and place it in this set, even if
1454
+ there is no net change to the attribute's value. At flush
1455
+ time, the value of each attribute is compared to its
1456
+ previously saved value, and if there's no net change, no SQL
1457
+ operation will occur (this is a more expensive operation so
1458
+ it's only done at flush time).
1459
+
1460
+ To check if an instance has actionable net changes to its
1461
+ attributes, use the :meth:`.Session.is_modified` method.
1462
+
1463
+
1464
+ """ # noqa: E501
1465
+
1466
+ return self._proxied.dirty
1467
+
1468
+ @property
1469
+ def deleted(self) -> Any:
1470
+ r"""The set of all instances marked as 'deleted' within this ``Session``
1471
+
1472
+ .. container:: class_bases
1473
+
1474
+ Proxied for the :class:`_orm.Session` class
1475
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1476
+
1477
+ """ # noqa: E501
1478
+
1479
+ return self._proxied.deleted
1480
+
1481
+ @property
1482
+ def new(self) -> Any:
1483
+ r"""The set of all instances marked as 'new' within this ``Session``.
1484
+
1485
+ .. container:: class_bases
1486
+
1487
+ Proxied for the :class:`_orm.Session` class
1488
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1489
+
1490
+ """ # noqa: E501
1491
+
1492
+ return self._proxied.new
1493
+
1494
+ @property
1495
+ def identity_map(self) -> IdentityMap:
1496
+ r"""Proxy for the :attr:`_orm.Session.identity_map` attribute
1497
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1498
+
1499
+ """ # noqa: E501
1500
+
1501
+ return self._proxied.identity_map
1502
+
1503
+ @identity_map.setter
1504
+ def identity_map(self, attr: IdentityMap) -> None:
1505
+ self._proxied.identity_map = attr
1506
+
1507
+ @property
1508
+ def is_active(self) -> Any:
1509
+ r"""True if this :class:`.Session` not in "partial rollback" state.
1510
+
1511
+ .. container:: class_bases
1512
+
1513
+ Proxied for the :class:`_orm.Session` class
1514
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1515
+
1516
+ .. versionchanged:: 1.4 The :class:`_orm.Session` no longer begins
1517
+ a new transaction immediately, so this attribute will be False
1518
+ when the :class:`_orm.Session` is first instantiated.
1519
+
1520
+ "partial rollback" state typically indicates that the flush process
1521
+ of the :class:`_orm.Session` has failed, and that the
1522
+ :meth:`_orm.Session.rollback` method must be emitted in order to
1523
+ fully roll back the transaction.
1524
+
1525
+ If this :class:`_orm.Session` is not in a transaction at all, the
1526
+ :class:`_orm.Session` will autobegin when it is first used, so in this
1527
+ case :attr:`_orm.Session.is_active` will return True.
1528
+
1529
+ Otherwise, if this :class:`_orm.Session` is within a transaction,
1530
+ and that transaction has not been rolled back internally, the
1531
+ :attr:`_orm.Session.is_active` will also return True.
1532
+
1533
+ .. seealso::
1534
+
1535
+ :ref:`faq_session_rollback`
1536
+
1537
+ :meth:`_orm.Session.in_transaction`
1538
+
1539
+
1540
+ """ # noqa: E501
1541
+
1542
+ return self._proxied.is_active
1543
+
1544
+ @property
1545
+ def autoflush(self) -> bool:
1546
+ r"""Proxy for the :attr:`_orm.Session.autoflush` attribute
1547
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1548
+
1549
+ """ # noqa: E501
1550
+
1551
+ return self._proxied.autoflush
1552
+
1553
+ @autoflush.setter
1554
+ def autoflush(self, attr: bool) -> None:
1555
+ self._proxied.autoflush = attr
1556
+
1557
+ @property
1558
+ def no_autoflush(self) -> Any:
1559
+ r"""Return a context manager that disables autoflush.
1560
+
1561
+ .. container:: class_bases
1562
+
1563
+ Proxied for the :class:`_orm.Session` class
1564
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1565
+
1566
+ e.g.::
1567
+
1568
+ with session.no_autoflush:
1569
+
1570
+ some_object = SomeClass()
1571
+ session.add(some_object)
1572
+ # won't autoflush
1573
+ some_object.related_thing = session.query(SomeRelated).first()
1574
+
1575
+ Operations that proceed within the ``with:`` block
1576
+ will not be subject to flushes occurring upon query
1577
+ access. This is useful when initializing a series
1578
+ of objects which involve existing database queries,
1579
+ where the uncompleted object should not yet be flushed.
1580
+
1581
+
1582
+ """ # noqa: E501
1583
+
1584
+ return self._proxied.no_autoflush
1585
+
1586
+ @property
1587
+ def info(self) -> Any:
1588
+ r"""A user-modifiable dictionary.
1589
+
1590
+ .. container:: class_bases
1591
+
1592
+ Proxied for the :class:`_orm.Session` class
1593
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1594
+
1595
+ The initial value of this dictionary can be populated using the
1596
+ ``info`` argument to the :class:`.Session` constructor or
1597
+ :class:`.sessionmaker` constructor or factory methods. The dictionary
1598
+ here is always local to this :class:`.Session` and can be modified
1599
+ independently of all other :class:`.Session` objects.
1600
+
1601
+
1602
+ """ # noqa: E501
1603
+
1604
+ return self._proxied.info
1605
+
1606
+ @property
1607
+ def execution_options(self) -> _ExecuteOptions:
1608
+ r"""Proxy for the :attr:`_orm.Session.execution_options` attribute
1609
+ on behalf of the :class:`_asyncio.AsyncSession` class.
1610
+
1611
+ """ # noqa: E501
1612
+
1613
+ return self._proxied.execution_options
1614
+
1615
+ @execution_options.setter
1616
+ def execution_options(self, attr: _ExecuteOptions) -> None:
1617
+ self._proxied.execution_options = attr
1618
+
1619
+ @classmethod
1620
+ def object_session(cls, instance: object) -> Optional[Session]:
1621
+ r"""Return the :class:`.Session` to which an object belongs.
1622
+
1623
+ .. container:: class_bases
1624
+
1625
+ Proxied for the :class:`_orm.Session` class on
1626
+ behalf of the :class:`_asyncio.AsyncSession` class.
1627
+
1628
+ This is an alias of :func:`.object_session`.
1629
+
1630
+
1631
+ """ # noqa: E501
1632
+
1633
+ return Session.object_session(instance)
1634
+
1635
+ @classmethod
1636
+ def identity_key(
1637
+ cls,
1638
+ class_: Optional[Type[Any]] = None,
1639
+ ident: Union[Any, Tuple[Any, ...]] = None,
1640
+ *,
1641
+ instance: Optional[Any] = None,
1642
+ row: Optional[Union[Row[Unpack[TupleAny]], RowMapping]] = None,
1643
+ identity_token: Optional[Any] = None,
1644
+ ) -> _IdentityKeyType[Any]:
1645
+ r"""Return an identity key.
1646
+
1647
+ .. container:: class_bases
1648
+
1649
+ Proxied for the :class:`_orm.Session` class on
1650
+ behalf of the :class:`_asyncio.AsyncSession` class.
1651
+
1652
+ This is an alias of :func:`.util.identity_key`.
1653
+
1654
+
1655
+ """ # noqa: E501
1656
+
1657
+ return Session.identity_key(
1658
+ class_=class_,
1659
+ ident=ident,
1660
+ instance=instance,
1661
+ row=row,
1662
+ identity_token=identity_token,
1663
+ )
1664
+
1665
+ # END PROXY METHODS AsyncSession
1666
+
1667
+
1668
+ _AS = TypeVar("_AS", bound="AsyncSession")
1669
+
1670
+
1671
+ class async_sessionmaker(Generic[_AS]):
1672
+ """A configurable :class:`.AsyncSession` factory.
1673
+
1674
+ The :class:`.async_sessionmaker` factory works in the same way as the
1675
+ :class:`.sessionmaker` factory, to generate new :class:`.AsyncSession`
1676
+ objects when called, creating them given
1677
+ the configurational arguments established here.
1678
+
1679
+ e.g.::
1680
+
1681
+ from sqlalchemy.ext.asyncio import create_async_engine
1682
+ from sqlalchemy.ext.asyncio import AsyncSession
1683
+ from sqlalchemy.ext.asyncio import async_sessionmaker
1684
+
1685
+
1686
+ async def run_some_sql(
1687
+ async_session: async_sessionmaker[AsyncSession],
1688
+ ) -> None:
1689
+ async with async_session() as session:
1690
+ session.add(SomeObject(data="object"))
1691
+ session.add(SomeOtherObject(name="other object"))
1692
+ await session.commit()
1693
+
1694
+
1695
+ async def main() -> None:
1696
+ # an AsyncEngine, which the AsyncSession will use for connection
1697
+ # resources
1698
+ engine = create_async_engine(
1699
+ "postgresql+asyncpg://scott:tiger@localhost/"
1700
+ )
1701
+
1702
+ # create a reusable factory for new AsyncSession instances
1703
+ async_session = async_sessionmaker(engine)
1704
+
1705
+ await run_some_sql(async_session)
1706
+
1707
+ await engine.dispose()
1708
+
1709
+ The :class:`.async_sessionmaker` is useful so that different parts
1710
+ of a program can create new :class:`.AsyncSession` objects with a
1711
+ fixed configuration established up front. Note that :class:`.AsyncSession`
1712
+ objects may also be instantiated directly when not using
1713
+ :class:`.async_sessionmaker`.
1714
+
1715
+ .. versionadded:: 2.0 :class:`.async_sessionmaker` provides a
1716
+ :class:`.sessionmaker` class that's dedicated to the
1717
+ :class:`.AsyncSession` object, including pep-484 typing support.
1718
+
1719
+ .. seealso::
1720
+
1721
+ :ref:`asyncio_orm` - shows example use
1722
+
1723
+ :class:`.sessionmaker` - general overview of the
1724
+ :class:`.sessionmaker` architecture
1725
+
1726
+
1727
+ :ref:`session_getting` - introductory text on creating
1728
+ sessions using :class:`.sessionmaker`.
1729
+
1730
+ """ # noqa E501
1731
+
1732
+ class_: Type[_AS]
1733
+
1734
+ @overload
1735
+ def __init__(
1736
+ self,
1737
+ bind: Optional[_AsyncSessionBind] = ...,
1738
+ *,
1739
+ class_: Type[_AS],
1740
+ autoflush: bool = ...,
1741
+ expire_on_commit: bool = ...,
1742
+ info: Optional[_InfoType] = ...,
1743
+ **kw: Any,
1744
+ ): ...
1745
+
1746
+ @overload
1747
+ def __init__(
1748
+ self: "async_sessionmaker[AsyncSession]",
1749
+ bind: Optional[_AsyncSessionBind] = ...,
1750
+ *,
1751
+ autoflush: bool = ...,
1752
+ expire_on_commit: bool = ...,
1753
+ info: Optional[_InfoType] = ...,
1754
+ **kw: Any,
1755
+ ): ...
1756
+
1757
+ def __init__(
1758
+ self,
1759
+ bind: Optional[_AsyncSessionBind] = None,
1760
+ *,
1761
+ class_: Type[_AS] = AsyncSession, # type: ignore
1762
+ autoflush: bool = True,
1763
+ expire_on_commit: bool = True,
1764
+ info: Optional[_InfoType] = None,
1765
+ **kw: Any,
1766
+ ):
1767
+ r"""Construct a new :class:`.async_sessionmaker`.
1768
+
1769
+ All arguments here except for ``class_`` correspond to arguments
1770
+ accepted by :class:`.Session` directly. See the
1771
+ :meth:`.AsyncSession.__init__` docstring for more details on
1772
+ parameters.
1773
+
1774
+
1775
+ """
1776
+ kw["bind"] = bind
1777
+ kw["autoflush"] = autoflush
1778
+ kw["expire_on_commit"] = expire_on_commit
1779
+ if info is not None:
1780
+ kw["info"] = info
1781
+ self.kw = kw
1782
+ self.class_ = class_
1783
+
1784
+ def begin(self) -> _AsyncSessionContextManager[_AS]:
1785
+ """Produce a context manager that both provides a new
1786
+ :class:`_orm.AsyncSession` as well as a transaction that commits.
1787
+
1788
+
1789
+ e.g.::
1790
+
1791
+ async def main():
1792
+ Session = async_sessionmaker(some_engine)
1793
+
1794
+ async with Session.begin() as session:
1795
+ session.add(some_object)
1796
+
1797
+ # commits transaction, closes session
1798
+
1799
+ """
1800
+
1801
+ session = self()
1802
+ return session._maker_context_manager()
1803
+
1804
+ def __call__(self, **local_kw: Any) -> _AS:
1805
+ """Produce a new :class:`.AsyncSession` object using the configuration
1806
+ established in this :class:`.async_sessionmaker`.
1807
+
1808
+ In Python, the ``__call__`` method is invoked on an object when
1809
+ it is "called" in the same way as a function::
1810
+
1811
+ AsyncSession = async_sessionmaker(async_engine, expire_on_commit=False)
1812
+ session = AsyncSession() # invokes sessionmaker.__call__()
1813
+
1814
+ """ # noqa E501
1815
+ for k, v in self.kw.items():
1816
+ if k == "info" and "info" in local_kw:
1817
+ d = v.copy()
1818
+ d.update(local_kw["info"])
1819
+ local_kw["info"] = d
1820
+ else:
1821
+ local_kw.setdefault(k, v)
1822
+ return self.class_(**local_kw)
1823
+
1824
+ def configure(self, **new_kw: Any) -> None:
1825
+ """(Re)configure the arguments for this async_sessionmaker.
1826
+
1827
+ e.g.::
1828
+
1829
+ AsyncSession = async_sessionmaker(some_engine)
1830
+
1831
+ AsyncSession.configure(bind=create_async_engine("sqlite+aiosqlite://"))
1832
+ """ # noqa E501
1833
+
1834
+ self.kw.update(new_kw)
1835
+
1836
+ def __repr__(self) -> str:
1837
+ return "%s(class_=%r, %s)" % (
1838
+ self.__class__.__name__,
1839
+ self.class_.__name__,
1840
+ ", ".join("%s=%r" % (k, v) for k, v in self.kw.items()),
1841
+ )
1842
+
1843
+
1844
+ class _AsyncSessionContextManager(Generic[_AS]):
1845
+ __slots__ = ("async_session", "trans")
1846
+
1847
+ async_session: _AS
1848
+ trans: AsyncSessionTransaction
1849
+
1850
+ def __init__(self, async_session: _AS):
1851
+ self.async_session = async_session
1852
+
1853
+ async def __aenter__(self) -> _AS:
1854
+ self.trans = self.async_session.begin()
1855
+ await self.trans.__aenter__()
1856
+ return self.async_session
1857
+
1858
+ async def __aexit__(self, type_: Any, value: Any, traceback: Any) -> None:
1859
+ async def go() -> None:
1860
+ await self.trans.__aexit__(type_, value, traceback)
1861
+ await self.async_session.__aexit__(type_, value, traceback)
1862
+
1863
+ task = asyncio.create_task(go())
1864
+ await asyncio.shield(task)
1865
+
1866
+
1867
+ class AsyncSessionTransaction(
1868
+ ReversibleProxy[SessionTransaction],
1869
+ StartableContext["AsyncSessionTransaction"],
1870
+ ):
1871
+ """A wrapper for the ORM :class:`_orm.SessionTransaction` object.
1872
+
1873
+ This object is provided so that a transaction-holding object
1874
+ for the :meth:`_asyncio.AsyncSession.begin` may be returned.
1875
+
1876
+ The object supports both explicit calls to
1877
+ :meth:`_asyncio.AsyncSessionTransaction.commit` and
1878
+ :meth:`_asyncio.AsyncSessionTransaction.rollback`, as well as use as an
1879
+ async context manager.
1880
+
1881
+
1882
+ .. versionadded:: 1.4
1883
+
1884
+ """
1885
+
1886
+ __slots__ = ("session", "sync_transaction", "nested")
1887
+
1888
+ session: AsyncSession
1889
+ sync_transaction: Optional[SessionTransaction]
1890
+
1891
+ def __init__(self, session: AsyncSession, nested: bool = False):
1892
+ self.session = session
1893
+ self.nested = nested
1894
+ self.sync_transaction = None
1895
+
1896
+ @property
1897
+ def is_active(self) -> bool:
1898
+ return (
1899
+ self._sync_transaction() is not None
1900
+ and self._sync_transaction().is_active
1901
+ )
1902
+
1903
+ def _sync_transaction(self) -> SessionTransaction:
1904
+ if not self.sync_transaction:
1905
+ self._raise_for_not_started()
1906
+ return self.sync_transaction
1907
+
1908
+ async def rollback(self) -> None:
1909
+ """Roll back this :class:`_asyncio.AsyncTransaction`."""
1910
+ await greenlet_spawn(self._sync_transaction().rollback)
1911
+
1912
+ async def commit(self) -> None:
1913
+ """Commit this :class:`_asyncio.AsyncTransaction`."""
1914
+
1915
+ await greenlet_spawn(self._sync_transaction().commit)
1916
+
1917
+ @classmethod
1918
+ def _regenerate_proxy_for_target( # type: ignore[override]
1919
+ cls,
1920
+ target: SessionTransaction,
1921
+ async_session: AsyncSession,
1922
+ **additional_kw: Any, # noqa: U100
1923
+ ) -> AsyncSessionTransaction:
1924
+ sync_transaction = target
1925
+ nested = target.nested
1926
+ obj = cls.__new__(cls)
1927
+ obj.session = async_session
1928
+ obj.sync_transaction = obj._assign_proxied(sync_transaction)
1929
+ obj.nested = nested
1930
+ return obj
1931
+
1932
+ async def start(
1933
+ self, is_ctxmanager: bool = False
1934
+ ) -> AsyncSessionTransaction:
1935
+ self.sync_transaction = self._assign_proxied(
1936
+ await greenlet_spawn(
1937
+ self.session.sync_session.begin_nested
1938
+ if self.nested
1939
+ else self.session.sync_session.begin
1940
+ )
1941
+ )
1942
+ if is_ctxmanager:
1943
+ self.sync_transaction.__enter__()
1944
+ return self
1945
+
1946
+ async def __aexit__(self, type_: Any, value: Any, traceback: Any) -> None:
1947
+ await greenlet_spawn(
1948
+ self._sync_transaction().__exit__, type_, value, traceback
1949
+ )
1950
+
1951
+
1952
+ def async_object_session(instance: object) -> Optional[AsyncSession]:
1953
+ """Return the :class:`_asyncio.AsyncSession` to which the given instance
1954
+ belongs.
1955
+
1956
+ This function makes use of the sync-API function
1957
+ :class:`_orm.object_session` to retrieve the :class:`_orm.Session` which
1958
+ refers to the given instance, and from there links it to the original
1959
+ :class:`_asyncio.AsyncSession`.
1960
+
1961
+ If the :class:`_asyncio.AsyncSession` has been garbage collected, the
1962
+ return value is ``None``.
1963
+
1964
+ This functionality is also available from the
1965
+ :attr:`_orm.InstanceState.async_session` accessor.
1966
+
1967
+ :param instance: an ORM mapped instance
1968
+ :return: an :class:`_asyncio.AsyncSession` object, or ``None``.
1969
+
1970
+ .. versionadded:: 1.4.18
1971
+
1972
+ """
1973
+
1974
+ session = object_session(instance)
1975
+ if session is not None:
1976
+ return async_session(session)
1977
+ else:
1978
+ return None
1979
+
1980
+
1981
+ def async_session(session: Session) -> Optional[AsyncSession]:
1982
+ """Return the :class:`_asyncio.AsyncSession` which is proxying the given
1983
+ :class:`_orm.Session` object, if any.
1984
+
1985
+ :param session: a :class:`_orm.Session` instance.
1986
+ :return: a :class:`_asyncio.AsyncSession` instance, or ``None``.
1987
+
1988
+ .. versionadded:: 1.4.18
1989
+
1990
+ """
1991
+ return AsyncSession._retrieve_proxy_for_target(session, regenerate=False)
1992
+
1993
+
1994
+ async def close_all_sessions() -> None:
1995
+ """Close all :class:`_asyncio.AsyncSession` sessions.
1996
+
1997
+ .. versionadded:: 2.0.23
1998
+
1999
+ .. seealso::
2000
+
2001
+ :func:`.session.close_all_sessions`
2002
+
2003
+ """
2004
+ await greenlet_spawn(_sync_close_all_sessions)
2005
+
2006
+
2007
+ _instance_state._async_provider = async_session # type: ignore