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,694 @@
1
+ # orm/writeonly.py
2
+ # Copyright (C) 2005-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
+
8
+ """Write-only collection API.
9
+
10
+ This is an alternate mapped attribute style that only supports single-item
11
+ collection mutation operations. To read the collection, a select()
12
+ object must be executed each time.
13
+
14
+ .. versionadded:: 2.0
15
+
16
+
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ from typing import Any
22
+ from typing import Collection
23
+ from typing import Dict
24
+ from typing import Generic
25
+ from typing import Iterable
26
+ from typing import Iterator
27
+ from typing import List
28
+ from typing import Literal
29
+ from typing import NoReturn
30
+ from typing import Optional
31
+ from typing import overload
32
+ from typing import Tuple
33
+ from typing import Type
34
+ from typing import TYPE_CHECKING
35
+ from typing import TypeVar
36
+ from typing import Union
37
+
38
+ from sqlalchemy.sql import bindparam
39
+ from . import attributes
40
+ from . import interfaces
41
+ from . import relationships
42
+ from . import strategies
43
+ from .base import ATTR_EMPTY
44
+ from .base import NEVER_SET
45
+ from .base import object_mapper
46
+ from .base import PassiveFlag
47
+ from .base import RelationshipDirection
48
+ from .. import exc
49
+ from .. import inspect
50
+ from .. import log
51
+ from .. import util
52
+ from ..sql import delete
53
+ from ..sql import insert
54
+ from ..sql import select
55
+ from ..sql import update
56
+ from ..sql.dml import Delete
57
+ from ..sql.dml import Insert
58
+ from ..sql.dml import Update
59
+
60
+ if TYPE_CHECKING:
61
+ from . import QueryableAttribute
62
+ from ._typing import _InstanceDict
63
+ from .attributes import AttributeEventToken
64
+ from .base import LoaderCallableStatus
65
+ from .collections import _AdaptedCollectionProtocol
66
+ from .collections import CollectionAdapter
67
+ from .mapper import Mapper
68
+ from .relationships import _RelationshipOrderByArg
69
+ from .state import InstanceState
70
+ from .util import AliasedClass
71
+ from ..event import _Dispatch
72
+ from ..sql.selectable import FromClause
73
+ from ..sql.selectable import Select
74
+
75
+ _T = TypeVar("_T", bound=Any)
76
+
77
+
78
+ class WriteOnlyHistory(Generic[_T]):
79
+ """Overrides AttributeHistory to receive append/remove events directly."""
80
+
81
+ unchanged_items: util.OrderedIdentitySet
82
+ added_items: util.OrderedIdentitySet
83
+ deleted_items: util.OrderedIdentitySet
84
+ _reconcile_collection: bool
85
+
86
+ def __init__(
87
+ self,
88
+ attr: _WriteOnlyAttributeImpl,
89
+ state: InstanceState[_T],
90
+ passive: PassiveFlag,
91
+ apply_to: Optional[WriteOnlyHistory[_T]] = None,
92
+ ) -> None:
93
+ if apply_to:
94
+ if passive & PassiveFlag.SQL_OK:
95
+ raise exc.InvalidRequestError(
96
+ f"Attribute {attr} can't load the existing state from the "
97
+ "database for this operation; full iteration is not "
98
+ "permitted. If this is a delete operation, configure "
99
+ f"passive_deletes=True on the {attr} relationship in "
100
+ "order to resolve this error."
101
+ )
102
+
103
+ self.unchanged_items = apply_to.unchanged_items
104
+ self.added_items = apply_to.added_items
105
+ self.deleted_items = apply_to.deleted_items
106
+ self._reconcile_collection = apply_to._reconcile_collection
107
+ else:
108
+ self.deleted_items = util.OrderedIdentitySet()
109
+ self.added_items = util.OrderedIdentitySet()
110
+ self.unchanged_items = util.OrderedIdentitySet()
111
+ self._reconcile_collection = False
112
+
113
+ @property
114
+ def added_plus_unchanged(self) -> List[_T]:
115
+ return list(self.added_items.union(self.unchanged_items))
116
+
117
+ @property
118
+ def all_items(self) -> List[_T]:
119
+ return list(
120
+ self.added_items.union(self.unchanged_items).union(
121
+ self.deleted_items
122
+ )
123
+ )
124
+
125
+ def as_history(self) -> attributes.History:
126
+ if self._reconcile_collection:
127
+ added = self.added_items.difference(self.unchanged_items)
128
+ deleted = self.deleted_items.intersection(self.unchanged_items)
129
+ unchanged = self.unchanged_items.difference(deleted)
130
+ else:
131
+ added, unchanged, deleted = (
132
+ self.added_items,
133
+ self.unchanged_items,
134
+ self.deleted_items,
135
+ )
136
+ return attributes.History(list(added), list(unchanged), list(deleted))
137
+
138
+ def indexed(self, index: Union[int, slice]) -> Union[List[_T], _T]:
139
+ return list(self.added_items)[index]
140
+
141
+ def add_added(self, value: _T) -> None:
142
+ self.added_items.add(value)
143
+
144
+ def add_removed(self, value: _T) -> None:
145
+ if value in self.added_items:
146
+ self.added_items.remove(value)
147
+ else:
148
+ self.deleted_items.add(value)
149
+
150
+
151
+ class _WriteOnlyAttributeImpl(
152
+ attributes._HasCollectionAdapter, attributes._AttributeImpl
153
+ ):
154
+ uses_objects: bool = True
155
+ default_accepts_scalar_loader: bool = False
156
+ supports_population: bool = False
157
+ _supports_dynamic_iteration: bool = False
158
+ collection: bool = False
159
+ dynamic: bool = True
160
+ order_by: _RelationshipOrderByArg = ()
161
+ collection_history_cls: Type[WriteOnlyHistory[Any]] = WriteOnlyHistory
162
+
163
+ query_class: Type[WriteOnlyCollection[Any]]
164
+
165
+ def __init__(
166
+ self,
167
+ class_: Union[Type[Any], AliasedClass[Any]],
168
+ key: str,
169
+ dispatch: _Dispatch[QueryableAttribute[Any]],
170
+ target_mapper: Mapper[_T],
171
+ order_by: _RelationshipOrderByArg,
172
+ **kw: Any,
173
+ ):
174
+ super().__init__(class_, key, None, dispatch, **kw)
175
+ self.target_mapper = target_mapper
176
+ self.query_class = WriteOnlyCollection
177
+ if order_by:
178
+ self.order_by = tuple(order_by)
179
+
180
+ def get(
181
+ self,
182
+ state: InstanceState[Any],
183
+ dict_: _InstanceDict,
184
+ passive: PassiveFlag = PassiveFlag.PASSIVE_OFF,
185
+ ) -> Union[util.OrderedIdentitySet, WriteOnlyCollection[Any]]:
186
+ if not passive & PassiveFlag.SQL_OK:
187
+ return self._get_collection_history(
188
+ state, PassiveFlag.PASSIVE_NO_INITIALIZE
189
+ ).added_items
190
+ else:
191
+ return self.query_class(self, state)
192
+
193
+ @overload
194
+ def get_collection(
195
+ self,
196
+ state: InstanceState[Any],
197
+ dict_: _InstanceDict,
198
+ user_data: Literal[None] = ...,
199
+ passive: Literal[PassiveFlag.PASSIVE_OFF] = ...,
200
+ ) -> CollectionAdapter: ...
201
+
202
+ @overload
203
+ def get_collection(
204
+ self,
205
+ state: InstanceState[Any],
206
+ dict_: _InstanceDict,
207
+ user_data: _AdaptedCollectionProtocol = ...,
208
+ passive: PassiveFlag = ...,
209
+ ) -> CollectionAdapter: ...
210
+
211
+ @overload
212
+ def get_collection(
213
+ self,
214
+ state: InstanceState[Any],
215
+ dict_: _InstanceDict,
216
+ user_data: Optional[_AdaptedCollectionProtocol] = ...,
217
+ passive: PassiveFlag = ...,
218
+ ) -> Union[
219
+ Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter
220
+ ]: ...
221
+
222
+ def get_collection(
223
+ self,
224
+ state: InstanceState[Any],
225
+ dict_: _InstanceDict,
226
+ user_data: Optional[_AdaptedCollectionProtocol] = None,
227
+ passive: PassiveFlag = PassiveFlag.PASSIVE_OFF,
228
+ ) -> Union[
229
+ Literal[LoaderCallableStatus.PASSIVE_NO_RESULT], CollectionAdapter
230
+ ]:
231
+ data: Collection[Any]
232
+ if not passive & PassiveFlag.SQL_OK:
233
+ data = self._get_collection_history(state, passive).added_items
234
+ else:
235
+ history = self._get_collection_history(state, passive)
236
+ data = history.added_plus_unchanged
237
+ return _DynamicCollectionAdapter(data) # type: ignore[return-value]
238
+
239
+ @util.memoized_property
240
+ def _append_token(self) -> attributes.AttributeEventToken:
241
+ return attributes.AttributeEventToken(self, attributes.OP_APPEND)
242
+
243
+ @util.memoized_property
244
+ def _remove_token(self) -> attributes.AttributeEventToken:
245
+ return attributes.AttributeEventToken(self, attributes.OP_REMOVE)
246
+
247
+ def fire_append_event(
248
+ self,
249
+ state: InstanceState[Any],
250
+ dict_: _InstanceDict,
251
+ value: Any,
252
+ initiator: Optional[AttributeEventToken],
253
+ collection_history: Optional[WriteOnlyHistory[Any]] = None,
254
+ ) -> None:
255
+ if collection_history is None:
256
+ collection_history = self._modified_event(state, dict_)
257
+
258
+ collection_history.add_added(value)
259
+
260
+ for fn in self.dispatch.append:
261
+ value = fn(state, value, initiator or self._append_token)
262
+
263
+ if self.trackparent and value is not None:
264
+ self.sethasparent(attributes.instance_state(value), state, True)
265
+
266
+ def fire_remove_event(
267
+ self,
268
+ state: InstanceState[Any],
269
+ dict_: _InstanceDict,
270
+ value: Any,
271
+ initiator: Optional[AttributeEventToken],
272
+ collection_history: Optional[WriteOnlyHistory[Any]] = None,
273
+ ) -> None:
274
+ if collection_history is None:
275
+ collection_history = self._modified_event(state, dict_)
276
+
277
+ collection_history.add_removed(value)
278
+
279
+ if self.trackparent and value is not None:
280
+ self.sethasparent(attributes.instance_state(value), state, False)
281
+
282
+ for fn in self.dispatch.remove:
283
+ fn(state, value, initiator or self._remove_token)
284
+
285
+ def _modified_event(
286
+ self, state: InstanceState[Any], dict_: _InstanceDict
287
+ ) -> WriteOnlyHistory[Any]:
288
+ if self.key not in state.committed_state:
289
+ state.committed_state[self.key] = self.collection_history_cls(
290
+ self, state, PassiveFlag.PASSIVE_NO_FETCH
291
+ )
292
+
293
+ state._modified_event(dict_, self, NEVER_SET)
294
+
295
+ # this is a hack to allow the entities.ComparableEntity fixture
296
+ # to work
297
+ dict_[self.key] = True
298
+ return state.committed_state[self.key] # type: ignore[no-any-return]
299
+
300
+ def set(
301
+ self,
302
+ state: InstanceState[Any],
303
+ dict_: _InstanceDict,
304
+ value: Any,
305
+ initiator: Optional[AttributeEventToken] = None,
306
+ passive: PassiveFlag = PassiveFlag.PASSIVE_OFF,
307
+ check_old: Any = None,
308
+ pop: bool = False,
309
+ _adapt: bool = True,
310
+ ) -> None:
311
+ if initiator and initiator.parent_token is self.parent_token:
312
+ return
313
+
314
+ if pop and value is None:
315
+ return
316
+
317
+ iterable = value
318
+ new_values = list(iterable)
319
+ if state.has_identity:
320
+ if not self._supports_dynamic_iteration:
321
+ raise exc.InvalidRequestError(
322
+ f'Collection "{self}" does not support implicit '
323
+ "iteration; collection replacement operations "
324
+ "can't be used"
325
+ )
326
+ old_collection = util.IdentitySet(
327
+ self.get(state, dict_, passive=passive)
328
+ )
329
+
330
+ collection_history = self._modified_event(state, dict_)
331
+ if not state.has_identity:
332
+ old_collection = collection_history.added_items
333
+ else:
334
+ old_collection = old_collection.union(
335
+ collection_history.added_items
336
+ )
337
+
338
+ constants = old_collection.intersection(new_values)
339
+ additions = util.IdentitySet(new_values).difference(constants)
340
+ removals = old_collection.difference(constants)
341
+
342
+ for member in new_values:
343
+ if member in additions:
344
+ self.fire_append_event(
345
+ state,
346
+ dict_,
347
+ member,
348
+ None,
349
+ collection_history=collection_history,
350
+ )
351
+
352
+ for member in removals:
353
+ self.fire_remove_event(
354
+ state,
355
+ dict_,
356
+ member,
357
+ None,
358
+ collection_history=collection_history,
359
+ )
360
+
361
+ def delete(self, *args: Any, **kwargs: Any) -> NoReturn:
362
+ raise NotImplementedError()
363
+
364
+ def set_committed_value(
365
+ self, state: InstanceState[Any], dict_: _InstanceDict, value: Any
366
+ ) -> NoReturn:
367
+ raise NotImplementedError(
368
+ "Dynamic attributes don't support collection population."
369
+ )
370
+
371
+ def get_history(
372
+ self,
373
+ state: InstanceState[Any],
374
+ dict_: _InstanceDict,
375
+ passive: PassiveFlag = PassiveFlag.PASSIVE_NO_FETCH,
376
+ ) -> attributes.History:
377
+ c = self._get_collection_history(state, passive)
378
+ return c.as_history()
379
+
380
+ def get_all_pending(
381
+ self,
382
+ state: InstanceState[Any],
383
+ dict_: _InstanceDict,
384
+ passive: PassiveFlag = PassiveFlag.PASSIVE_NO_INITIALIZE,
385
+ ) -> List[Tuple[InstanceState[Any], Any]]:
386
+ c = self._get_collection_history(state, passive)
387
+ return [(attributes.instance_state(x), x) for x in c.all_items]
388
+
389
+ def _default_value(
390
+ self, state: InstanceState[Any], dict_: _InstanceDict
391
+ ) -> Any:
392
+ value = None
393
+ for fn in self.dispatch.init_scalar:
394
+ ret = fn(state, value, dict_)
395
+ if ret is not ATTR_EMPTY:
396
+ value = ret
397
+
398
+ return value
399
+
400
+ def _get_collection_history(
401
+ self, state: InstanceState[Any], passive: PassiveFlag
402
+ ) -> WriteOnlyHistory[Any]:
403
+ c: WriteOnlyHistory[Any]
404
+ if self.key in state.committed_state:
405
+ c = state.committed_state[self.key]
406
+ else:
407
+ c = self.collection_history_cls(
408
+ self, state, PassiveFlag.PASSIVE_NO_FETCH
409
+ )
410
+
411
+ if state.has_identity and (passive & PassiveFlag.INIT_OK):
412
+ return self.collection_history_cls(
413
+ self, state, passive, apply_to=c
414
+ )
415
+ else:
416
+ return c
417
+
418
+ def append(
419
+ self,
420
+ state: InstanceState[Any],
421
+ dict_: _InstanceDict,
422
+ value: Any,
423
+ initiator: Optional[AttributeEventToken],
424
+ passive: PassiveFlag = PassiveFlag.PASSIVE_NO_FETCH,
425
+ ) -> None:
426
+ if initiator is not self: # type: ignore[comparison-overlap]
427
+ self.fire_append_event(state, dict_, value, initiator)
428
+
429
+ def remove(
430
+ self,
431
+ state: InstanceState[Any],
432
+ dict_: _InstanceDict,
433
+ value: Any,
434
+ initiator: Optional[AttributeEventToken],
435
+ passive: PassiveFlag = PassiveFlag.PASSIVE_NO_FETCH,
436
+ ) -> None:
437
+ if initiator is not self: # type: ignore[comparison-overlap]
438
+ self.fire_remove_event(state, dict_, value, initiator)
439
+
440
+ def pop(
441
+ self,
442
+ state: InstanceState[Any],
443
+ dict_: _InstanceDict,
444
+ value: Any,
445
+ initiator: Optional[AttributeEventToken],
446
+ passive: PassiveFlag = PassiveFlag.PASSIVE_NO_FETCH,
447
+ ) -> None:
448
+ self.remove(state, dict_, value, initiator, passive=passive)
449
+
450
+
451
+ @log.class_logger
452
+ @relationships.RelationshipProperty.strategy_for(lazy="write_only")
453
+ class _WriteOnlyLoader(strategies._AbstractRelationshipLoader, log.Identified):
454
+ impl_class = _WriteOnlyAttributeImpl
455
+
456
+ def init_class_attribute(self, mapper: Mapper[Any]) -> None:
457
+ self.is_class_level = True
458
+ if not self.uselist or self.parent_property.direction not in (
459
+ interfaces.ONETOMANY,
460
+ interfaces.MANYTOMANY,
461
+ ):
462
+ raise exc.InvalidRequestError(
463
+ "On relationship %s, 'dynamic' loaders cannot be used with "
464
+ "many-to-one/one-to-one relationships and/or "
465
+ "uselist=False." % self.parent_property
466
+ )
467
+
468
+ strategies._register_attribute( # type: ignore[no-untyped-call]
469
+ self.parent_property,
470
+ mapper,
471
+ useobject=True,
472
+ impl_class=self.impl_class,
473
+ target_mapper=self.parent_property.mapper,
474
+ order_by=self.parent_property.order_by,
475
+ query_class=self.parent_property.query_class,
476
+ )
477
+
478
+
479
+ class _DynamicCollectionAdapter:
480
+ """simplified CollectionAdapter for internal API consistency"""
481
+
482
+ data: Collection[Any]
483
+
484
+ def __init__(self, data: Collection[Any]):
485
+ self.data = data
486
+
487
+ def __iter__(self) -> Iterator[Any]:
488
+ return iter(self.data)
489
+
490
+ def _reset_empty(self) -> None:
491
+ pass
492
+
493
+ def __len__(self) -> int:
494
+ return len(self.data)
495
+
496
+ def __bool__(self) -> bool:
497
+ return True
498
+
499
+
500
+ class _AbstractCollectionWriter(Generic[_T]):
501
+ """Virtual collection which includes append/remove methods that synchronize
502
+ into the attribute event system.
503
+
504
+ """
505
+
506
+ if not TYPE_CHECKING:
507
+ __slots__ = ()
508
+
509
+ instance: _T
510
+ _from_obj: Tuple[FromClause, ...]
511
+
512
+ def __init__(
513
+ self, attr: _WriteOnlyAttributeImpl, state: InstanceState[_T]
514
+ ):
515
+ instance = state.obj()
516
+ if TYPE_CHECKING:
517
+ assert instance
518
+ self.instance = instance
519
+ self.attr = attr
520
+
521
+ mapper = object_mapper(instance)
522
+ prop = mapper._props[self.attr.key]
523
+
524
+ if prop.secondary is not None:
525
+ # this is a hack right now. The Query only knows how to
526
+ # make subsequent joins() without a given left-hand side
527
+ # from self._from_obj[0]. We need to ensure prop.secondary
528
+ # is in the FROM. So we purposely put the mapper selectable
529
+ # in _from_obj[0] to ensure a user-defined join() later on
530
+ # doesn't fail, and secondary is then in _from_obj[1].
531
+
532
+ # note also, we are using the official ORM-annotated selectable
533
+ # from __clause_element__(), see #7868
534
+
535
+ # _no_filter_by annotation is to prevent this table from being
536
+ # considered by filter_by() as part of #8601
537
+ self._from_obj = (
538
+ prop.mapper.__clause_element__(),
539
+ prop.secondary._annotate({"_no_filter_by": True}),
540
+ )
541
+ else:
542
+ self._from_obj = ()
543
+
544
+ self._where_criteria = (
545
+ prop._with_parent(instance, alias_secondary=False),
546
+ )
547
+
548
+ if self.attr.order_by:
549
+ self._order_by_clauses = self.attr.order_by
550
+ else:
551
+ self._order_by_clauses = ()
552
+
553
+ def _add_all_impl(self, iterator: Iterable[_T]) -> None:
554
+ for item in iterator:
555
+ self.attr.append(
556
+ attributes.instance_state(self.instance),
557
+ attributes.instance_dict(self.instance),
558
+ item,
559
+ None,
560
+ )
561
+
562
+ def _remove_impl(self, item: _T) -> None:
563
+ self.attr.remove(
564
+ attributes.instance_state(self.instance),
565
+ attributes.instance_dict(self.instance),
566
+ item,
567
+ None,
568
+ )
569
+
570
+
571
+ class WriteOnlyCollection(_AbstractCollectionWriter[_T]):
572
+ """Write-only collection which can synchronize changes into the
573
+ attribute event system.
574
+
575
+ The :class:`.WriteOnlyCollection` is used in a mapping by
576
+ using the ``"write_only"`` lazy loading strategy with
577
+ :func:`_orm.relationship`. For background on this configuration,
578
+ see :ref:`write_only_relationship`.
579
+
580
+ .. versionadded:: 2.0
581
+
582
+ .. seealso::
583
+
584
+ :ref:`write_only_relationship`
585
+
586
+ """
587
+
588
+ __slots__ = (
589
+ "instance",
590
+ "attr",
591
+ "_where_criteria",
592
+ "_from_obj",
593
+ "_order_by_clauses",
594
+ )
595
+
596
+ def __iter__(self) -> NoReturn:
597
+ raise TypeError(
598
+ "WriteOnly collections don't support iteration in-place; "
599
+ "to query for collection items, use the select() method to "
600
+ "produce a SQL statement and execute it with session.scalars()."
601
+ )
602
+
603
+ def select(self) -> Select[_T]:
604
+ """Produce a :class:`_sql.Select` construct that represents the
605
+ rows within this instance-local :class:`_orm.WriteOnlyCollection`.
606
+
607
+ """
608
+ stmt = select(self.attr.target_mapper).where(*self._where_criteria)
609
+ if self._from_obj:
610
+ stmt = stmt.select_from(*self._from_obj)
611
+ if self._order_by_clauses:
612
+ stmt = stmt.order_by(*self._order_by_clauses)
613
+ return stmt
614
+
615
+ def insert(self) -> Insert:
616
+ """For one-to-many collections, produce a :class:`_dml.Insert` which
617
+ will insert new rows in terms of this this instance-local
618
+ :class:`_orm.WriteOnlyCollection`.
619
+
620
+ This construct is only supported for a :class:`_orm.Relationship`
621
+ that does **not** include the :paramref:`_orm.relationship.secondary`
622
+ parameter. For relationships that refer to a many-to-many table,
623
+ use ordinary bulk insert techniques to produce new objects, then
624
+ use :meth:`_orm.AbstractCollectionWriter.add_all` to associate them
625
+ with the collection.
626
+
627
+
628
+ """
629
+
630
+ state = inspect(self.instance)
631
+ mapper = state.mapper
632
+ prop = mapper._props[self.attr.key]
633
+
634
+ if prop.direction is not RelationshipDirection.ONETOMANY:
635
+ raise exc.InvalidRequestError(
636
+ "Write only bulk INSERT only supported for one-to-many "
637
+ "collections; for many-to-many, use a separate bulk "
638
+ "INSERT along with add_all()."
639
+ )
640
+
641
+ dict_: Dict[str, Any] = {}
642
+
643
+ for l, r in prop.synchronize_pairs:
644
+ fn = prop._get_attr_w_warn_on_none(
645
+ mapper,
646
+ state,
647
+ state.dict,
648
+ l,
649
+ )
650
+
651
+ dict_[r.key] = bindparam(None, callable_=fn)
652
+
653
+ return insert(self.attr.target_mapper).values(**dict_)
654
+
655
+ def update(self) -> Update:
656
+ """Produce a :class:`_dml.Update` which will refer to rows in terms
657
+ of this instance-local :class:`_orm.WriteOnlyCollection`.
658
+
659
+ """
660
+ return update(self.attr.target_mapper).where(*self._where_criteria)
661
+
662
+ def delete(self) -> Delete:
663
+ """Produce a :class:`_dml.Delete` which will refer to rows in terms
664
+ of this instance-local :class:`_orm.WriteOnlyCollection`.
665
+
666
+ """
667
+ return delete(self.attr.target_mapper).where(*self._where_criteria)
668
+
669
+ def add_all(self, iterator: Iterable[_T]) -> None:
670
+ """Add an iterable of items to this :class:`_orm.WriteOnlyCollection`.
671
+
672
+ The given items will be persisted to the database in terms of
673
+ the parent instance's collection on the next flush.
674
+
675
+ """
676
+ self._add_all_impl(iterator)
677
+
678
+ def add(self, item: _T) -> None:
679
+ """Add an item to this :class:`_orm.WriteOnlyCollection`.
680
+
681
+ The given item will be persisted to the database in terms of
682
+ the parent instance's collection on the next flush.
683
+
684
+ """
685
+ self._add_all_impl([item])
686
+
687
+ def remove(self, item: _T) -> None:
688
+ """Remove an item from this :class:`_orm.WriteOnlyCollection`.
689
+
690
+ The given item will be removed from the parent instance's collection on
691
+ the next flush.
692
+
693
+ """
694
+ self._remove_impl(item)