SQLAlchemy 2.0.47__cp313-cp313t-win32.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 (274) hide show
  1. sqlalchemy/__init__.py +283 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +184 -0
  4. sqlalchemy/connectors/asyncio.py +429 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/cyextension/__init__.py +6 -0
  7. sqlalchemy/cyextension/collections.cp313t-win32.pyd +0 -0
  8. sqlalchemy/cyextension/collections.pyx +409 -0
  9. sqlalchemy/cyextension/immutabledict.cp313t-win32.pyd +0 -0
  10. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  11. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  12. sqlalchemy/cyextension/processors.cp313t-win32.pyd +0 -0
  13. sqlalchemy/cyextension/processors.pyx +68 -0
  14. sqlalchemy/cyextension/resultproxy.cp313t-win32.pyd +0 -0
  15. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  16. sqlalchemy/cyextension/util.cp313t-win32.pyd +0 -0
  17. sqlalchemy/cyextension/util.pyx +90 -0
  18. sqlalchemy/dialects/__init__.py +62 -0
  19. sqlalchemy/dialects/_typing.py +30 -0
  20. sqlalchemy/dialects/mssql/__init__.py +88 -0
  21. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  22. sqlalchemy/dialects/mssql/base.py +4093 -0
  23. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  24. sqlalchemy/dialects/mssql/json.py +129 -0
  25. sqlalchemy/dialects/mssql/provision.py +185 -0
  26. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  27. sqlalchemy/dialects/mssql/pyodbc.py +760 -0
  28. sqlalchemy/dialects/mysql/__init__.py +104 -0
  29. sqlalchemy/dialects/mysql/aiomysql.py +250 -0
  30. sqlalchemy/dialects/mysql/asyncmy.py +231 -0
  31. sqlalchemy/dialects/mysql/base.py +3949 -0
  32. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  33. sqlalchemy/dialects/mysql/dml.py +225 -0
  34. sqlalchemy/dialects/mysql/enumerated.py +282 -0
  35. sqlalchemy/dialects/mysql/expression.py +146 -0
  36. sqlalchemy/dialects/mysql/json.py +91 -0
  37. sqlalchemy/dialects/mysql/mariadb.py +72 -0
  38. sqlalchemy/dialects/mysql/mariadbconnector.py +322 -0
  39. sqlalchemy/dialects/mysql/mysqlconnector.py +302 -0
  40. sqlalchemy/dialects/mysql/mysqldb.py +314 -0
  41. sqlalchemy/dialects/mysql/provision.py +153 -0
  42. sqlalchemy/dialects/mysql/pymysql.py +158 -0
  43. sqlalchemy/dialects/mysql/pyodbc.py +157 -0
  44. sqlalchemy/dialects/mysql/reflection.py +727 -0
  45. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  46. sqlalchemy/dialects/mysql/types.py +835 -0
  47. sqlalchemy/dialects/oracle/__init__.py +81 -0
  48. sqlalchemy/dialects/oracle/base.py +3802 -0
  49. sqlalchemy/dialects/oracle/cx_oracle.py +1555 -0
  50. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  51. sqlalchemy/dialects/oracle/oracledb.py +941 -0
  52. sqlalchemy/dialects/oracle/provision.py +297 -0
  53. sqlalchemy/dialects/oracle/types.py +316 -0
  54. sqlalchemy/dialects/oracle/vector.py +365 -0
  55. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  56. sqlalchemy/dialects/postgresql/_psycopg_common.py +189 -0
  57. sqlalchemy/dialects/postgresql/array.py +519 -0
  58. sqlalchemy/dialects/postgresql/asyncpg.py +1284 -0
  59. sqlalchemy/dialects/postgresql/base.py +5378 -0
  60. sqlalchemy/dialects/postgresql/dml.py +339 -0
  61. sqlalchemy/dialects/postgresql/ext.py +540 -0
  62. sqlalchemy/dialects/postgresql/hstore.py +406 -0
  63. sqlalchemy/dialects/postgresql/json.py +404 -0
  64. sqlalchemy/dialects/postgresql/named_types.py +524 -0
  65. sqlalchemy/dialects/postgresql/operators.py +129 -0
  66. sqlalchemy/dialects/postgresql/pg8000.py +669 -0
  67. sqlalchemy/dialects/postgresql/pg_catalog.py +326 -0
  68. sqlalchemy/dialects/postgresql/provision.py +183 -0
  69. sqlalchemy/dialects/postgresql/psycopg.py +862 -0
  70. sqlalchemy/dialects/postgresql/psycopg2.py +892 -0
  71. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  72. sqlalchemy/dialects/postgresql/ranges.py +1031 -0
  73. sqlalchemy/dialects/postgresql/types.py +313 -0
  74. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  75. sqlalchemy/dialects/sqlite/aiosqlite.py +482 -0
  76. sqlalchemy/dialects/sqlite/base.py +3056 -0
  77. sqlalchemy/dialects/sqlite/dml.py +263 -0
  78. sqlalchemy/dialects/sqlite/json.py +92 -0
  79. sqlalchemy/dialects/sqlite/provision.py +229 -0
  80. sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
  81. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  82. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  83. sqlalchemy/engine/__init__.py +62 -0
  84. sqlalchemy/engine/_py_processors.py +136 -0
  85. sqlalchemy/engine/_py_row.py +128 -0
  86. sqlalchemy/engine/_py_util.py +74 -0
  87. sqlalchemy/engine/base.py +3390 -0
  88. sqlalchemy/engine/characteristics.py +155 -0
  89. sqlalchemy/engine/create.py +893 -0
  90. sqlalchemy/engine/cursor.py +2298 -0
  91. sqlalchemy/engine/default.py +2394 -0
  92. sqlalchemy/engine/events.py +965 -0
  93. sqlalchemy/engine/interfaces.py +3471 -0
  94. sqlalchemy/engine/mock.py +134 -0
  95. sqlalchemy/engine/processors.py +61 -0
  96. sqlalchemy/engine/reflection.py +2102 -0
  97. sqlalchemy/engine/result.py +2399 -0
  98. sqlalchemy/engine/row.py +400 -0
  99. sqlalchemy/engine/strategies.py +16 -0
  100. sqlalchemy/engine/url.py +924 -0
  101. sqlalchemy/engine/util.py +167 -0
  102. sqlalchemy/event/__init__.py +26 -0
  103. sqlalchemy/event/api.py +220 -0
  104. sqlalchemy/event/attr.py +676 -0
  105. sqlalchemy/event/base.py +472 -0
  106. sqlalchemy/event/legacy.py +258 -0
  107. sqlalchemy/event/registry.py +390 -0
  108. sqlalchemy/events.py +17 -0
  109. sqlalchemy/exc.py +832 -0
  110. sqlalchemy/ext/__init__.py +11 -0
  111. sqlalchemy/ext/associationproxy.py +2027 -0
  112. sqlalchemy/ext/asyncio/__init__.py +25 -0
  113. sqlalchemy/ext/asyncio/base.py +281 -0
  114. sqlalchemy/ext/asyncio/engine.py +1471 -0
  115. sqlalchemy/ext/asyncio/exc.py +21 -0
  116. sqlalchemy/ext/asyncio/result.py +965 -0
  117. sqlalchemy/ext/asyncio/scoping.py +1599 -0
  118. sqlalchemy/ext/asyncio/session.py +1947 -0
  119. sqlalchemy/ext/automap.py +1701 -0
  120. sqlalchemy/ext/baked.py +570 -0
  121. sqlalchemy/ext/compiler.py +600 -0
  122. sqlalchemy/ext/declarative/__init__.py +65 -0
  123. sqlalchemy/ext/declarative/extensions.py +564 -0
  124. sqlalchemy/ext/horizontal_shard.py +478 -0
  125. sqlalchemy/ext/hybrid.py +1535 -0
  126. sqlalchemy/ext/indexable.py +364 -0
  127. sqlalchemy/ext/instrumentation.py +450 -0
  128. sqlalchemy/ext/mutable.py +1085 -0
  129. sqlalchemy/ext/mypy/__init__.py +6 -0
  130. sqlalchemy/ext/mypy/apply.py +324 -0
  131. sqlalchemy/ext/mypy/decl_class.py +515 -0
  132. sqlalchemy/ext/mypy/infer.py +590 -0
  133. sqlalchemy/ext/mypy/names.py +335 -0
  134. sqlalchemy/ext/mypy/plugin.py +303 -0
  135. sqlalchemy/ext/mypy/util.py +357 -0
  136. sqlalchemy/ext/orderinglist.py +439 -0
  137. sqlalchemy/ext/serializer.py +185 -0
  138. sqlalchemy/future/__init__.py +16 -0
  139. sqlalchemy/future/engine.py +15 -0
  140. sqlalchemy/inspection.py +174 -0
  141. sqlalchemy/log.py +288 -0
  142. sqlalchemy/orm/__init__.py +171 -0
  143. sqlalchemy/orm/_orm_constructors.py +2661 -0
  144. sqlalchemy/orm/_typing.py +179 -0
  145. sqlalchemy/orm/attributes.py +2845 -0
  146. sqlalchemy/orm/base.py +971 -0
  147. sqlalchemy/orm/bulk_persistence.py +2135 -0
  148. sqlalchemy/orm/clsregistry.py +571 -0
  149. sqlalchemy/orm/collections.py +1627 -0
  150. sqlalchemy/orm/context.py +3334 -0
  151. sqlalchemy/orm/decl_api.py +2004 -0
  152. sqlalchemy/orm/decl_base.py +2192 -0
  153. sqlalchemy/orm/dependency.py +1302 -0
  154. sqlalchemy/orm/descriptor_props.py +1092 -0
  155. sqlalchemy/orm/dynamic.py +300 -0
  156. sqlalchemy/orm/evaluator.py +379 -0
  157. sqlalchemy/orm/events.py +3252 -0
  158. sqlalchemy/orm/exc.py +237 -0
  159. sqlalchemy/orm/identity.py +302 -0
  160. sqlalchemy/orm/instrumentation.py +754 -0
  161. sqlalchemy/orm/interfaces.py +1496 -0
  162. sqlalchemy/orm/loading.py +1686 -0
  163. sqlalchemy/orm/mapped_collection.py +557 -0
  164. sqlalchemy/orm/mapper.py +4444 -0
  165. sqlalchemy/orm/path_registry.py +809 -0
  166. sqlalchemy/orm/persistence.py +1788 -0
  167. sqlalchemy/orm/properties.py +935 -0
  168. sqlalchemy/orm/query.py +3459 -0
  169. sqlalchemy/orm/relationships.py +3508 -0
  170. sqlalchemy/orm/scoping.py +2148 -0
  171. sqlalchemy/orm/session.py +5280 -0
  172. sqlalchemy/orm/state.py +1168 -0
  173. sqlalchemy/orm/state_changes.py +196 -0
  174. sqlalchemy/orm/strategies.py +3470 -0
  175. sqlalchemy/orm/strategy_options.py +2568 -0
  176. sqlalchemy/orm/sync.py +164 -0
  177. sqlalchemy/orm/unitofwork.py +796 -0
  178. sqlalchemy/orm/util.py +2403 -0
  179. sqlalchemy/orm/writeonly.py +674 -0
  180. sqlalchemy/pool/__init__.py +44 -0
  181. sqlalchemy/pool/base.py +1524 -0
  182. sqlalchemy/pool/events.py +375 -0
  183. sqlalchemy/pool/impl.py +588 -0
  184. sqlalchemy/py.typed +0 -0
  185. sqlalchemy/schema.py +69 -0
  186. sqlalchemy/sql/__init__.py +145 -0
  187. sqlalchemy/sql/_dml_constructors.py +132 -0
  188. sqlalchemy/sql/_elements_constructors.py +1872 -0
  189. sqlalchemy/sql/_orm_types.py +20 -0
  190. sqlalchemy/sql/_py_util.py +75 -0
  191. sqlalchemy/sql/_selectable_constructors.py +763 -0
  192. sqlalchemy/sql/_typing.py +482 -0
  193. sqlalchemy/sql/annotation.py +587 -0
  194. sqlalchemy/sql/base.py +2293 -0
  195. sqlalchemy/sql/cache_key.py +1057 -0
  196. sqlalchemy/sql/coercions.py +1404 -0
  197. sqlalchemy/sql/compiler.py +8081 -0
  198. sqlalchemy/sql/crud.py +1752 -0
  199. sqlalchemy/sql/ddl.py +1444 -0
  200. sqlalchemy/sql/default_comparator.py +551 -0
  201. sqlalchemy/sql/dml.py +1850 -0
  202. sqlalchemy/sql/elements.py +5589 -0
  203. sqlalchemy/sql/events.py +458 -0
  204. sqlalchemy/sql/expression.py +159 -0
  205. sqlalchemy/sql/functions.py +2158 -0
  206. sqlalchemy/sql/lambdas.py +1442 -0
  207. sqlalchemy/sql/naming.py +209 -0
  208. sqlalchemy/sql/operators.py +2623 -0
  209. sqlalchemy/sql/roles.py +323 -0
  210. sqlalchemy/sql/schema.py +6222 -0
  211. sqlalchemy/sql/selectable.py +7265 -0
  212. sqlalchemy/sql/sqltypes.py +3930 -0
  213. sqlalchemy/sql/traversals.py +1024 -0
  214. sqlalchemy/sql/type_api.py +2368 -0
  215. sqlalchemy/sql/util.py +1485 -0
  216. sqlalchemy/sql/visitors.py +1164 -0
  217. sqlalchemy/testing/__init__.py +96 -0
  218. sqlalchemy/testing/assertions.py +994 -0
  219. sqlalchemy/testing/assertsql.py +520 -0
  220. sqlalchemy/testing/asyncio.py +135 -0
  221. sqlalchemy/testing/config.py +434 -0
  222. sqlalchemy/testing/engines.py +483 -0
  223. sqlalchemy/testing/entities.py +117 -0
  224. sqlalchemy/testing/exclusions.py +476 -0
  225. sqlalchemy/testing/fixtures/__init__.py +28 -0
  226. sqlalchemy/testing/fixtures/base.py +384 -0
  227. sqlalchemy/testing/fixtures/mypy.py +332 -0
  228. sqlalchemy/testing/fixtures/orm.py +227 -0
  229. sqlalchemy/testing/fixtures/sql.py +482 -0
  230. sqlalchemy/testing/pickleable.py +155 -0
  231. sqlalchemy/testing/plugin/__init__.py +6 -0
  232. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  233. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  234. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  235. sqlalchemy/testing/profiling.py +329 -0
  236. sqlalchemy/testing/provision.py +603 -0
  237. sqlalchemy/testing/requirements.py +1945 -0
  238. sqlalchemy/testing/schema.py +198 -0
  239. sqlalchemy/testing/suite/__init__.py +19 -0
  240. sqlalchemy/testing/suite/test_cte.py +237 -0
  241. sqlalchemy/testing/suite/test_ddl.py +389 -0
  242. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  243. sqlalchemy/testing/suite/test_dialect.py +776 -0
  244. sqlalchemy/testing/suite/test_insert.py +630 -0
  245. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  246. sqlalchemy/testing/suite/test_results.py +504 -0
  247. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  248. sqlalchemy/testing/suite/test_select.py +2010 -0
  249. sqlalchemy/testing/suite/test_sequence.py +317 -0
  250. sqlalchemy/testing/suite/test_types.py +2147 -0
  251. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  252. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  253. sqlalchemy/testing/util.py +535 -0
  254. sqlalchemy/testing/warnings.py +52 -0
  255. sqlalchemy/types.py +74 -0
  256. sqlalchemy/util/__init__.py +162 -0
  257. sqlalchemy/util/_collections.py +712 -0
  258. sqlalchemy/util/_concurrency_py3k.py +288 -0
  259. sqlalchemy/util/_has_cy.py +40 -0
  260. sqlalchemy/util/_py_collections.py +541 -0
  261. sqlalchemy/util/compat.py +421 -0
  262. sqlalchemy/util/concurrency.py +110 -0
  263. sqlalchemy/util/deprecations.py +401 -0
  264. sqlalchemy/util/langhelpers.py +2203 -0
  265. sqlalchemy/util/preloaded.py +150 -0
  266. sqlalchemy/util/queue.py +322 -0
  267. sqlalchemy/util/tool_support.py +201 -0
  268. sqlalchemy/util/topological.py +120 -0
  269. sqlalchemy/util/typing.py +734 -0
  270. sqlalchemy-2.0.47.dist-info/METADATA +243 -0
  271. sqlalchemy-2.0.47.dist-info/RECORD +274 -0
  272. sqlalchemy-2.0.47.dist-info/WHEEL +5 -0
  273. sqlalchemy-2.0.47.dist-info/licenses/LICENSE +19 -0
  274. sqlalchemy-2.0.47.dist-info/top_level.txt +1 -0
@@ -0,0 +1,754 @@
1
+ # orm/instrumentation.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
+ # mypy: allow-untyped-defs, allow-untyped-calls
8
+
9
+ """Defines SQLAlchemy's system of class instrumentation.
10
+
11
+ This module is usually not directly visible to user applications, but
12
+ defines a large part of the ORM's interactivity.
13
+
14
+ instrumentation.py deals with registration of end-user classes
15
+ for state tracking. It interacts closely with state.py
16
+ and attributes.py which establish per-instance and per-class-attribute
17
+ instrumentation, respectively.
18
+
19
+ The class instrumentation system can be customized on a per-class
20
+ or global basis using the :mod:`sqlalchemy.ext.instrumentation`
21
+ module, which provides the means to build and specify
22
+ alternate instrumentation forms.
23
+
24
+ .. versionchanged: 0.8
25
+ The instrumentation extension system was moved out of the
26
+ ORM and into the external :mod:`sqlalchemy.ext.instrumentation`
27
+ package. When that package is imported, it installs
28
+ itself within sqlalchemy.orm so that its more comprehensive
29
+ resolution mechanics take effect.
30
+
31
+ """
32
+
33
+
34
+ from __future__ import annotations
35
+
36
+ from typing import Any
37
+ from typing import Callable
38
+ from typing import cast
39
+ from typing import Collection
40
+ from typing import Dict
41
+ from typing import Generic
42
+ from typing import Iterable
43
+ from typing import List
44
+ from typing import Optional
45
+ from typing import Set
46
+ from typing import Tuple
47
+ from typing import Type
48
+ from typing import TYPE_CHECKING
49
+ from typing import TypeVar
50
+ from typing import Union
51
+ import weakref
52
+
53
+ from . import base
54
+ from . import collections
55
+ from . import exc
56
+ from . import interfaces
57
+ from . import state
58
+ from ._typing import _O
59
+ from .attributes import _is_collection_attribute_impl
60
+ from .. import util
61
+ from ..event import EventTarget
62
+ from ..util import HasMemoized
63
+ from ..util.typing import Literal
64
+ from ..util.typing import Protocol
65
+
66
+ if TYPE_CHECKING:
67
+ from ._typing import _RegistryType
68
+ from .attributes import AttributeImpl
69
+ from .attributes import QueryableAttribute
70
+ from .collections import _AdaptedCollectionProtocol
71
+ from .collections import _CollectionFactoryType
72
+ from .decl_base import _MapperConfig
73
+ from .events import InstanceEvents
74
+ from .mapper import Mapper
75
+ from .state import InstanceState
76
+ from ..event import dispatcher
77
+
78
+ _T = TypeVar("_T", bound=Any)
79
+ DEL_ATTR = util.symbol("DEL_ATTR")
80
+
81
+
82
+ class _ExpiredAttributeLoaderProto(Protocol):
83
+ def __call__(
84
+ self,
85
+ state: state.InstanceState[Any],
86
+ toload: Set[str],
87
+ passive: base.PassiveFlag,
88
+ ) -> None: ...
89
+
90
+
91
+ class _ManagerFactory(Protocol):
92
+ def __call__(self, class_: Type[_O]) -> ClassManager[_O]: ...
93
+
94
+
95
+ class ClassManager(
96
+ HasMemoized,
97
+ Dict[str, "QueryableAttribute[Any]"],
98
+ Generic[_O],
99
+ EventTarget,
100
+ ):
101
+ """Tracks state information at the class level."""
102
+
103
+ dispatch: dispatcher[ClassManager[_O]]
104
+
105
+ MANAGER_ATTR = base.DEFAULT_MANAGER_ATTR
106
+ STATE_ATTR = base.DEFAULT_STATE_ATTR
107
+
108
+ _state_setter = staticmethod(util.attrsetter(STATE_ATTR))
109
+
110
+ expired_attribute_loader: _ExpiredAttributeLoaderProto
111
+ "previously known as deferred_scalar_loader"
112
+
113
+ init_method: Optional[Callable[..., None]]
114
+ original_init: Optional[Callable[..., None]] = None
115
+
116
+ factory: Optional[_ManagerFactory]
117
+
118
+ declarative_scan: Optional[weakref.ref[_MapperConfig]] = None
119
+
120
+ registry: _RegistryType
121
+
122
+ if not TYPE_CHECKING:
123
+ # starts as None during setup
124
+ registry = None
125
+
126
+ class_: Type[_O]
127
+
128
+ _bases: List[ClassManager[Any]]
129
+
130
+ @property
131
+ @util.deprecated(
132
+ "1.4",
133
+ message="The ClassManager.deferred_scalar_loader attribute is now "
134
+ "named expired_attribute_loader",
135
+ )
136
+ def deferred_scalar_loader(self):
137
+ return self.expired_attribute_loader
138
+
139
+ @deferred_scalar_loader.setter
140
+ @util.deprecated(
141
+ "1.4",
142
+ message="The ClassManager.deferred_scalar_loader attribute is now "
143
+ "named expired_attribute_loader",
144
+ )
145
+ def deferred_scalar_loader(self, obj):
146
+ self.expired_attribute_loader = obj
147
+
148
+ def __init__(self, class_):
149
+ self.class_ = class_
150
+ self.info = {}
151
+ self.new_init = None
152
+ self.local_attrs = {}
153
+ self.originals = {}
154
+ self._finalized = False
155
+ self.factory = None
156
+ self.init_method = None
157
+
158
+ self._bases = [
159
+ mgr
160
+ for mgr in cast(
161
+ "List[Optional[ClassManager[Any]]]",
162
+ [
163
+ opt_manager_of_class(base)
164
+ for base in self.class_.__bases__
165
+ if isinstance(base, type)
166
+ ],
167
+ )
168
+ if mgr is not None
169
+ ]
170
+
171
+ for base_ in self._bases:
172
+ self.update(base_)
173
+
174
+ cast(
175
+ "InstanceEvents", self.dispatch._events
176
+ )._new_classmanager_instance(class_, self)
177
+
178
+ for basecls in class_.__mro__:
179
+ mgr = opt_manager_of_class(basecls)
180
+ if mgr is not None:
181
+ self.dispatch._update(mgr.dispatch)
182
+
183
+ self.manage()
184
+
185
+ if "__del__" in class_.__dict__:
186
+ util.warn(
187
+ "__del__() method on class %s will "
188
+ "cause unreachable cycles and memory leaks, "
189
+ "as SQLAlchemy instrumentation often creates "
190
+ "reference cycles. Please remove this method." % class_
191
+ )
192
+
193
+ def _update_state(
194
+ self,
195
+ finalize: bool = False,
196
+ mapper: Optional[Mapper[_O]] = None,
197
+ registry: Optional[_RegistryType] = None,
198
+ declarative_scan: Optional[_MapperConfig] = None,
199
+ expired_attribute_loader: Optional[
200
+ _ExpiredAttributeLoaderProto
201
+ ] = None,
202
+ init_method: Optional[Callable[..., None]] = None,
203
+ ) -> None:
204
+ if mapper:
205
+ self.mapper = mapper #
206
+ if registry:
207
+ registry._add_manager(self)
208
+ if declarative_scan:
209
+ self.declarative_scan = weakref.ref(declarative_scan)
210
+ if expired_attribute_loader:
211
+ self.expired_attribute_loader = expired_attribute_loader
212
+
213
+ if init_method:
214
+ assert not self._finalized, (
215
+ "class is already instrumented, "
216
+ "init_method %s can't be applied" % init_method
217
+ )
218
+ self.init_method = init_method
219
+
220
+ if not self._finalized:
221
+ self.original_init = (
222
+ self.init_method
223
+ if self.init_method is not None
224
+ and self.class_.__init__ is object.__init__
225
+ else self.class_.__init__
226
+ )
227
+
228
+ if finalize and not self._finalized:
229
+ self._finalize()
230
+
231
+ def _finalize(self) -> None:
232
+ if self._finalized:
233
+ return
234
+ self._finalized = True
235
+
236
+ self._instrument_init()
237
+
238
+ _instrumentation_factory.dispatch.class_instrument(self.class_)
239
+
240
+ def __hash__(self) -> int: # type: ignore[override]
241
+ return id(self)
242
+
243
+ def __eq__(self, other: Any) -> bool:
244
+ return other is self
245
+
246
+ @property
247
+ def is_mapped(self) -> bool:
248
+ return "mapper" in self.__dict__
249
+
250
+ @HasMemoized.memoized_attribute
251
+ def _all_key_set(self):
252
+ return frozenset(self)
253
+
254
+ @HasMemoized.memoized_attribute
255
+ def _collection_impl_keys(self):
256
+ return frozenset(
257
+ [attr.key for attr in self.values() if attr.impl.collection]
258
+ )
259
+
260
+ @HasMemoized.memoized_attribute
261
+ def _scalar_loader_impls(self):
262
+ return frozenset(
263
+ [
264
+ attr.impl
265
+ for attr in self.values()
266
+ if attr.impl.accepts_scalar_loader
267
+ ]
268
+ )
269
+
270
+ @HasMemoized.memoized_attribute
271
+ def _loader_impls(self):
272
+ return frozenset([attr.impl for attr in self.values()])
273
+
274
+ @util.memoized_property
275
+ def mapper(self) -> Mapper[_O]:
276
+ # raises unless self.mapper has been assigned
277
+ raise exc.UnmappedClassError(self.class_)
278
+
279
+ def _all_sqla_attributes(self, exclude=None):
280
+ """return an iterator of all classbound attributes that are
281
+ implement :class:`.InspectionAttr`.
282
+
283
+ This includes :class:`.QueryableAttribute` as well as extension
284
+ types such as :class:`.hybrid_property` and
285
+ :class:`.AssociationProxy`.
286
+
287
+ """
288
+
289
+ found: Dict[str, Any] = {}
290
+
291
+ # constraints:
292
+ # 1. yield keys in cls.__dict__ order
293
+ # 2. if a subclass has the same key as a superclass, include that
294
+ # key as part of the ordering of the superclass, because an
295
+ # overridden key is usually installed by the mapper which is going
296
+ # on a different ordering
297
+ # 3. don't use getattr() as this fires off descriptors
298
+
299
+ for supercls in self.class_.__mro__[0:-1]:
300
+ inherits = supercls.__mro__[1]
301
+ for key in supercls.__dict__:
302
+ found.setdefault(key, supercls)
303
+ if key in inherits.__dict__:
304
+ continue
305
+ val = found[key].__dict__[key]
306
+ if (
307
+ isinstance(val, interfaces.InspectionAttr)
308
+ and val.is_attribute
309
+ ):
310
+ yield key, val
311
+
312
+ def _get_class_attr_mro(self, key, default=None):
313
+ """return an attribute on the class without tripping it."""
314
+
315
+ for supercls in self.class_.__mro__:
316
+ if key in supercls.__dict__:
317
+ return supercls.__dict__[key]
318
+ else:
319
+ return default
320
+
321
+ def _attr_has_impl(self, key: str) -> bool:
322
+ """Return True if the given attribute is fully initialized.
323
+
324
+ i.e. has an impl.
325
+ """
326
+
327
+ return key in self and self[key].impl is not None
328
+
329
+ def _subclass_manager(self, cls: Type[_T]) -> ClassManager[_T]:
330
+ """Create a new ClassManager for a subclass of this ClassManager's
331
+ class.
332
+
333
+ This is called automatically when attributes are instrumented so that
334
+ the attributes can be propagated to subclasses against their own
335
+ class-local manager, without the need for mappers etc. to have already
336
+ pre-configured managers for the full class hierarchy. Mappers
337
+ can post-configure the auto-generated ClassManager when needed.
338
+
339
+ """
340
+ return register_class(cls, finalize=False)
341
+
342
+ def _instrument_init(self):
343
+ self.new_init = _generate_init(self.class_, self, self.original_init)
344
+ self.install_member("__init__", self.new_init)
345
+
346
+ @util.memoized_property
347
+ def _state_constructor(self) -> Type[state.InstanceState[_O]]:
348
+ self.dispatch.first_init(self, self.class_)
349
+ return state.InstanceState
350
+
351
+ def manage(self):
352
+ """Mark this instance as the manager for its class."""
353
+
354
+ setattr(self.class_, self.MANAGER_ATTR, self)
355
+
356
+ @util.hybridmethod
357
+ def manager_getter(self):
358
+ return _default_manager_getter
359
+
360
+ @util.hybridmethod
361
+ def state_getter(self):
362
+ """Return a (instance) -> InstanceState callable.
363
+
364
+ "state getter" callables should raise either KeyError or
365
+ AttributeError if no InstanceState could be found for the
366
+ instance.
367
+ """
368
+
369
+ return _default_state_getter
370
+
371
+ @util.hybridmethod
372
+ def dict_getter(self):
373
+ return _default_dict_getter
374
+
375
+ def instrument_attribute(
376
+ self,
377
+ key: str,
378
+ inst: QueryableAttribute[Any],
379
+ propagated: bool = False,
380
+ ) -> None:
381
+ if propagated:
382
+ if key in self.local_attrs:
383
+ return # don't override local attr with inherited attr
384
+ else:
385
+ self.local_attrs[key] = inst
386
+ self.install_descriptor(key, inst)
387
+ self._reset_memoizations()
388
+ self[key] = inst
389
+
390
+ for cls in self.class_.__subclasses__():
391
+ manager = self._subclass_manager(cls)
392
+ manager.instrument_attribute(key, inst, True)
393
+
394
+ def subclass_managers(self, recursive):
395
+ for cls in self.class_.__subclasses__():
396
+ mgr = opt_manager_of_class(cls)
397
+ if mgr is not None and mgr is not self:
398
+ yield mgr
399
+ if recursive:
400
+ yield from mgr.subclass_managers(True)
401
+
402
+ def post_configure_attribute(self, key):
403
+ _instrumentation_factory.dispatch.attribute_instrument(
404
+ self.class_, key, self[key]
405
+ )
406
+
407
+ def uninstrument_attribute(self, key, propagated=False):
408
+ if key not in self:
409
+ return
410
+ if propagated:
411
+ if key in self.local_attrs:
412
+ return # don't get rid of local attr
413
+ else:
414
+ del self.local_attrs[key]
415
+ self.uninstall_descriptor(key)
416
+ self._reset_memoizations()
417
+ del self[key]
418
+ for cls in self.class_.__subclasses__():
419
+ manager = opt_manager_of_class(cls)
420
+ if manager:
421
+ manager.uninstrument_attribute(key, True)
422
+
423
+ def unregister(self) -> None:
424
+ """remove all instrumentation established by this ClassManager."""
425
+
426
+ for key in list(self.originals):
427
+ self.uninstall_member(key)
428
+
429
+ self.mapper = None
430
+ self.dispatch = None # type: ignore
431
+ self.new_init = None
432
+ self.info.clear()
433
+
434
+ for key in list(self):
435
+ if key in self.local_attrs:
436
+ self.uninstrument_attribute(key)
437
+
438
+ if self.MANAGER_ATTR in self.class_.__dict__:
439
+ delattr(self.class_, self.MANAGER_ATTR)
440
+
441
+ def install_descriptor(
442
+ self, key: str, inst: QueryableAttribute[Any]
443
+ ) -> None:
444
+ if key in (self.STATE_ATTR, self.MANAGER_ATTR):
445
+ raise KeyError(
446
+ "%r: requested attribute name conflicts with "
447
+ "instrumentation attribute of the same name." % key
448
+ )
449
+ setattr(self.class_, key, inst)
450
+
451
+ def uninstall_descriptor(self, key: str) -> None:
452
+ delattr(self.class_, key)
453
+
454
+ def install_member(self, key: str, implementation: Any) -> None:
455
+ if key in (self.STATE_ATTR, self.MANAGER_ATTR):
456
+ raise KeyError(
457
+ "%r: requested attribute name conflicts with "
458
+ "instrumentation attribute of the same name." % key
459
+ )
460
+ self.originals.setdefault(key, self.class_.__dict__.get(key, DEL_ATTR))
461
+ setattr(self.class_, key, implementation)
462
+
463
+ def uninstall_member(self, key: str) -> None:
464
+ original = self.originals.pop(key, None)
465
+ if original is not DEL_ATTR:
466
+ setattr(self.class_, key, original)
467
+ else:
468
+ delattr(self.class_, key)
469
+
470
+ def instrument_collection_class(
471
+ self, key: str, collection_class: Type[Collection[Any]]
472
+ ) -> _CollectionFactoryType:
473
+ return collections.prepare_instrumentation(collection_class)
474
+
475
+ def initialize_collection(
476
+ self,
477
+ key: str,
478
+ state: InstanceState[_O],
479
+ factory: _CollectionFactoryType,
480
+ ) -> Tuple[collections.CollectionAdapter, _AdaptedCollectionProtocol]:
481
+ user_data = factory()
482
+ impl = self.get_impl(key)
483
+ assert _is_collection_attribute_impl(impl)
484
+ adapter = collections.CollectionAdapter(impl, state, user_data)
485
+ return adapter, user_data
486
+
487
+ def is_instrumented(self, key: str, search: bool = False) -> bool:
488
+ if search:
489
+ return key in self
490
+ else:
491
+ return key in self.local_attrs
492
+
493
+ def get_impl(self, key: str) -> AttributeImpl:
494
+ return self[key].impl
495
+
496
+ @property
497
+ def attributes(self) -> Iterable[Any]:
498
+ return iter(self.values())
499
+
500
+ # InstanceState management
501
+
502
+ def new_instance(self, state: Optional[InstanceState[_O]] = None) -> _O:
503
+ # here, we would prefer _O to be bound to "object"
504
+ # so that mypy sees that __new__ is present. currently
505
+ # it's bound to Any as there were other problems not having
506
+ # it that way but these can be revisited
507
+ instance = self.class_.__new__(self.class_)
508
+ if state is None:
509
+ state = self._state_constructor(instance, self)
510
+ self._state_setter(instance, state)
511
+ return instance
512
+
513
+ def setup_instance(
514
+ self, instance: _O, state: Optional[InstanceState[_O]] = None
515
+ ) -> None:
516
+ if state is None:
517
+ state = self._state_constructor(instance, self)
518
+ self._state_setter(instance, state)
519
+
520
+ def teardown_instance(self, instance: _O) -> None:
521
+ delattr(instance, self.STATE_ATTR)
522
+
523
+ def _serialize(
524
+ self, state: InstanceState[_O], state_dict: Dict[str, Any]
525
+ ) -> _SerializeManager:
526
+ return _SerializeManager(state, state_dict)
527
+
528
+ def _new_state_if_none(
529
+ self, instance: _O
530
+ ) -> Union[Literal[False], InstanceState[_O]]:
531
+ """Install a default InstanceState if none is present.
532
+
533
+ A private convenience method used by the __init__ decorator.
534
+
535
+ """
536
+ if hasattr(instance, self.STATE_ATTR):
537
+ return False
538
+ elif self.class_ is not instance.__class__ and self.is_mapped:
539
+ # this will create a new ClassManager for the
540
+ # subclass, without a mapper. This is likely a
541
+ # user error situation but allow the object
542
+ # to be constructed, so that it is usable
543
+ # in a non-ORM context at least.
544
+ return self._subclass_manager(
545
+ instance.__class__
546
+ )._new_state_if_none(instance)
547
+ else:
548
+ state = self._state_constructor(instance, self)
549
+ self._state_setter(instance, state)
550
+ return state
551
+
552
+ def has_state(self, instance: _O) -> bool:
553
+ return hasattr(instance, self.STATE_ATTR)
554
+
555
+ def has_parent(
556
+ self, state: InstanceState[_O], key: str, optimistic: bool = False
557
+ ) -> bool:
558
+ """TODO"""
559
+ return self.get_impl(key).hasparent(state, optimistic=optimistic)
560
+
561
+ def __bool__(self) -> bool:
562
+ """All ClassManagers are non-zero regardless of attribute state."""
563
+ return True
564
+
565
+ def __repr__(self) -> str:
566
+ return "<%s of %r at %x>" % (
567
+ self.__class__.__name__,
568
+ self.class_,
569
+ id(self),
570
+ )
571
+
572
+
573
+ class _SerializeManager:
574
+ """Provide serialization of a :class:`.ClassManager`.
575
+
576
+ The :class:`.InstanceState` uses ``__init__()`` on serialize
577
+ and ``__call__()`` on deserialize.
578
+
579
+ """
580
+
581
+ def __init__(self, state: state.InstanceState[Any], d: Dict[str, Any]):
582
+ self.class_ = state.class_
583
+ manager = state.manager
584
+ manager.dispatch.pickle(state, d)
585
+
586
+ def __call__(self, state, inst, state_dict):
587
+ state.manager = manager = opt_manager_of_class(self.class_)
588
+ if manager is None:
589
+ raise exc.UnmappedInstanceError(
590
+ inst,
591
+ "Cannot deserialize object of type %r - "
592
+ "no mapper() has "
593
+ "been configured for this class within the current "
594
+ "Python process!" % self.class_,
595
+ )
596
+ elif manager.is_mapped and not manager.mapper.configured:
597
+ manager.mapper._check_configure()
598
+
599
+ # setup _sa_instance_state ahead of time so that
600
+ # unpickle events can access the object normally.
601
+ # see [ticket:2362]
602
+ if inst is not None:
603
+ manager.setup_instance(inst, state)
604
+ manager.dispatch.unpickle(state, state_dict)
605
+
606
+
607
+ class InstrumentationFactory(EventTarget):
608
+ """Factory for new ClassManager instances."""
609
+
610
+ dispatch: dispatcher[InstrumentationFactory]
611
+
612
+ def create_manager_for_cls(self, class_: Type[_O]) -> ClassManager[_O]:
613
+ assert class_ is not None
614
+ assert opt_manager_of_class(class_) is None
615
+
616
+ # give a more complicated subclass
617
+ # a chance to do what it wants here
618
+ manager, factory = self._locate_extended_factory(class_)
619
+
620
+ if factory is None:
621
+ factory = ClassManager
622
+ manager = ClassManager(class_)
623
+ else:
624
+ assert manager is not None
625
+
626
+ self._check_conflicts(class_, factory)
627
+
628
+ manager.factory = factory
629
+
630
+ return manager
631
+
632
+ def _locate_extended_factory(
633
+ self, class_: Type[_O]
634
+ ) -> Tuple[Optional[ClassManager[_O]], Optional[_ManagerFactory]]:
635
+ """Overridden by a subclass to do an extended lookup."""
636
+ return None, None
637
+
638
+ def _check_conflicts(
639
+ self, class_: Type[_O], factory: Callable[[Type[_O]], ClassManager[_O]]
640
+ ) -> None:
641
+ """Overridden by a subclass to test for conflicting factories."""
642
+
643
+ def unregister(self, class_: Type[_O]) -> None:
644
+ manager = manager_of_class(class_)
645
+ manager.unregister()
646
+ self.dispatch.class_uninstrument(class_)
647
+
648
+
649
+ # this attribute is replaced by sqlalchemy.ext.instrumentation
650
+ # when imported.
651
+ _instrumentation_factory = InstrumentationFactory()
652
+
653
+ # these attributes are replaced by sqlalchemy.ext.instrumentation
654
+ # when a non-standard InstrumentationManager class is first
655
+ # used to instrument a class.
656
+ instance_state = _default_state_getter = base.instance_state
657
+
658
+ instance_dict = _default_dict_getter = base.instance_dict
659
+
660
+ manager_of_class = _default_manager_getter = base.manager_of_class
661
+ opt_manager_of_class = _default_opt_manager_getter = base.opt_manager_of_class
662
+
663
+
664
+ def register_class(
665
+ class_: Type[_O],
666
+ finalize: bool = True,
667
+ mapper: Optional[Mapper[_O]] = None,
668
+ registry: Optional[_RegistryType] = None,
669
+ declarative_scan: Optional[_MapperConfig] = None,
670
+ expired_attribute_loader: Optional[_ExpiredAttributeLoaderProto] = None,
671
+ init_method: Optional[Callable[..., None]] = None,
672
+ ) -> ClassManager[_O]:
673
+ """Register class instrumentation.
674
+
675
+ Returns the existing or newly created class manager.
676
+
677
+ """
678
+
679
+ manager = opt_manager_of_class(class_)
680
+ if manager is None:
681
+ manager = _instrumentation_factory.create_manager_for_cls(class_)
682
+ manager._update_state(
683
+ mapper=mapper,
684
+ registry=registry,
685
+ declarative_scan=declarative_scan,
686
+ expired_attribute_loader=expired_attribute_loader,
687
+ init_method=init_method,
688
+ finalize=finalize,
689
+ )
690
+
691
+ return manager
692
+
693
+
694
+ def unregister_class(class_):
695
+ """Unregister class instrumentation."""
696
+
697
+ _instrumentation_factory.unregister(class_)
698
+
699
+
700
+ def is_instrumented(instance, key):
701
+ """Return True if the given attribute on the given instance is
702
+ instrumented by the attributes package.
703
+
704
+ This function may be used regardless of instrumentation
705
+ applied directly to the class, i.e. no descriptors are required.
706
+
707
+ """
708
+ return manager_of_class(instance.__class__).is_instrumented(
709
+ key, search=True
710
+ )
711
+
712
+
713
+ def _generate_init(class_, class_manager, original_init):
714
+ """Build an __init__ decorator that triggers ClassManager events."""
715
+
716
+ # TODO: we should use the ClassManager's notion of the
717
+ # original '__init__' method, once ClassManager is fixed
718
+ # to always reference that.
719
+
720
+ if original_init is None:
721
+ original_init = class_.__init__
722
+
723
+ # Go through some effort here and don't change the user's __init__
724
+ # calling signature, including the unlikely case that it has
725
+ # a return value.
726
+ # FIXME: need to juggle local names to avoid constructor argument
727
+ # clashes.
728
+ func_body = """\
729
+ def __init__(%(apply_pos)s):
730
+ new_state = class_manager._new_state_if_none(%(self_arg)s)
731
+ if new_state:
732
+ return new_state._initialize_instance(%(apply_kw)s)
733
+ else:
734
+ return original_init(%(apply_kw)s)
735
+ """
736
+ func_vars = util.format_argspec_init(original_init, grouped=False)
737
+ func_text = func_body % func_vars
738
+
739
+ func_defaults = getattr(original_init, "__defaults__", None)
740
+ func_kw_defaults = getattr(original_init, "__kwdefaults__", None)
741
+
742
+ env = locals().copy()
743
+ env["__name__"] = __name__
744
+ exec(func_text, env)
745
+ __init__ = env["__init__"]
746
+ __init__.__doc__ = original_init.__doc__
747
+ __init__._sa_original_init = original_init
748
+
749
+ if func_defaults:
750
+ __init__.__defaults__ = func_defaults
751
+ if func_kw_defaults:
752
+ __init__.__kwdefaults__ = func_kw_defaults
753
+
754
+ return __init__