SQLAlchemy 2.1.0rc1__cp315-cp315-win_amd64.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 (276) hide show
  1. sqlalchemy/__init__.py +299 -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 +248 -0
  6. sqlalchemy/dialects/__init__.py +62 -0
  7. sqlalchemy/dialects/_typing.py +29 -0
  8. sqlalchemy/dialects/mssql/__init__.py +88 -0
  9. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  10. sqlalchemy/dialects/mssql/base.py +4833 -0
  11. sqlalchemy/dialects/mssql/information_schema.py +345 -0
  12. sqlalchemy/dialects/mssql/json.py +140 -0
  13. sqlalchemy/dialects/mssql/mssqlpython.py +242 -0
  14. sqlalchemy/dialects/mssql/provision.py +196 -0
  15. sqlalchemy/dialects/mssql/pymssql.py +130 -0
  16. sqlalchemy/dialects/mssql/pyodbc.py +697 -0
  17. sqlalchemy/dialects/mysql/__init__.py +106 -0
  18. sqlalchemy/dialects/mysql/_mariadb_shim.py +312 -0
  19. sqlalchemy/dialects/mysql/aiomysql.py +260 -0
  20. sqlalchemy/dialects/mysql/asyncmy.py +241 -0
  21. sqlalchemy/dialects/mysql/base.py +3896 -0
  22. sqlalchemy/dialects/mysql/cymysql.py +107 -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 +314 -0
  29. sqlalchemy/dialects/mysql/mysqlconnector.py +291 -0
  30. sqlalchemy/dialects/mysql/mysqldb.py +318 -0
  31. sqlalchemy/dialects/mysql/provision.py +153 -0
  32. sqlalchemy/dialects/mysql/pymysql.py +188 -0
  33. sqlalchemy/dialects/mysql/pyodbc.py +157 -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 +3847 -0
  39. sqlalchemy/dialects/oracle/cx_oracle.py +1736 -0
  40. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  41. sqlalchemy/dialects/oracle/json.py +157 -0
  42. sqlalchemy/dialects/oracle/oracledb.py +898 -0
  43. sqlalchemy/dialects/oracle/provision.py +288 -0
  44. sqlalchemy/dialects/oracle/types.py +367 -0
  45. sqlalchemy/dialects/oracle/vector.py +366 -0
  46. sqlalchemy/dialects/postgresql/__init__.py +170 -0
  47. sqlalchemy/dialects/postgresql/_psycopg_common.py +232 -0
  48. sqlalchemy/dialects/postgresql/array.py +534 -0
  49. sqlalchemy/dialects/postgresql/asyncpg.py +1318 -0
  50. sqlalchemy/dialects/postgresql/base.py +5935 -0
  51. sqlalchemy/dialects/postgresql/bitstring.py +327 -0
  52. sqlalchemy/dialects/postgresql/dml.py +360 -0
  53. sqlalchemy/dialects/postgresql/ext.py +599 -0
  54. sqlalchemy/dialects/postgresql/hstore.py +422 -0
  55. sqlalchemy/dialects/postgresql/json.py +411 -0
  56. sqlalchemy/dialects/postgresql/named_types.py +535 -0
  57. sqlalchemy/dialects/postgresql/operators.py +129 -0
  58. sqlalchemy/dialects/postgresql/pg8000.py +655 -0
  59. sqlalchemy/dialects/postgresql/pg_catalog.py +345 -0
  60. sqlalchemy/dialects/postgresql/provision.py +202 -0
  61. sqlalchemy/dialects/postgresql/psycopg.py +800 -0
  62. sqlalchemy/dialects/postgresql/psycopg2.py +860 -0
  63. sqlalchemy/dialects/postgresql/psycopg2cffi.py +62 -0
  64. sqlalchemy/dialects/postgresql/ranges.py +1002 -0
  65. sqlalchemy/dialects/postgresql/types.py +388 -0
  66. sqlalchemy/dialects/sqlite/__init__.py +59 -0
  67. sqlalchemy/dialects/sqlite/aiosqlite.py +375 -0
  68. sqlalchemy/dialects/sqlite/base.py +3103 -0
  69. sqlalchemy/dialects/sqlite/dml.py +314 -0
  70. sqlalchemy/dialects/sqlite/json.py +134 -0
  71. sqlalchemy/dialects/sqlite/provision.py +237 -0
  72. sqlalchemy/dialects/sqlite/pysqlcipher.py +166 -0
  73. sqlalchemy/dialects/sqlite/pysqlite.py +959 -0
  74. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  75. sqlalchemy/engine/__init__.py +62 -0
  76. sqlalchemy/engine/_processors_cy.cp315-win_amd64.pyd +0 -0
  77. sqlalchemy/engine/_processors_cy.py +92 -0
  78. sqlalchemy/engine/_result_cy.cp315-win_amd64.pyd +0 -0
  79. sqlalchemy/engine/_result_cy.py +711 -0
  80. sqlalchemy/engine/_row_cy.cp315-win_amd64.pyd +0 -0
  81. sqlalchemy/engine/_row_cy.py +232 -0
  82. sqlalchemy/engine/_util_cy.cp315-win_amd64.pyd +0 -0
  83. sqlalchemy/engine/_util_cy.py +136 -0
  84. sqlalchemy/engine/base.py +3357 -0
  85. sqlalchemy/engine/characteristics.py +155 -0
  86. sqlalchemy/engine/create.py +877 -0
  87. sqlalchemy/engine/cursor.py +2425 -0
  88. sqlalchemy/engine/default.py +2627 -0
  89. sqlalchemy/engine/events.py +965 -0
  90. sqlalchemy/engine/interfaces.py +3636 -0
  91. sqlalchemy/engine/mock.py +133 -0
  92. sqlalchemy/engine/processors.py +83 -0
  93. sqlalchemy/engine/reflection.py +2141 -0
  94. sqlalchemy/engine/result.py +2012 -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 +164 -0
  99. sqlalchemy/event/__init__.py +26 -0
  100. sqlalchemy/event/api.py +220 -0
  101. sqlalchemy/event/attr.py +675 -0
  102. sqlalchemy/event/base.py +473 -0
  103. sqlalchemy/event/legacy.py +259 -0
  104. sqlalchemy/event/registry.py +391 -0
  105. sqlalchemy/events.py +17 -0
  106. sqlalchemy/exc.py +939 -0
  107. sqlalchemy/ext/__init__.py +10 -0
  108. sqlalchemy/ext/associationproxy.py +2073 -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 +2006 -0
  116. sqlalchemy/ext/automap.py +1702 -0
  117. sqlalchemy/ext/baked.py +558 -0
  118. sqlalchemy/ext/compiler.py +601 -0
  119. sqlalchemy/ext/declarative/__init__.py +65 -0
  120. sqlalchemy/ext/declarative/extensions.py +561 -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 +440 -0
  127. sqlalchemy/ext/serializer.py +184 -0
  128. sqlalchemy/future/__init__.py +17 -0
  129. sqlalchemy/future/engine.py +15 -0
  130. sqlalchemy/inspection.py +188 -0
  131. sqlalchemy/log.py +279 -0
  132. sqlalchemy/orm/__init__.py +176 -0
  133. sqlalchemy/orm/_orm_constructors.py +2694 -0
  134. sqlalchemy/orm/_typing.py +180 -0
  135. sqlalchemy/orm/attributes.py +2868 -0
  136. sqlalchemy/orm/base.py +991 -0
  137. sqlalchemy/orm/bulk_persistence.py +2168 -0
  138. sqlalchemy/orm/clsregistry.py +630 -0
  139. sqlalchemy/orm/collections.py +1569 -0
  140. sqlalchemy/orm/context.py +3475 -0
  141. sqlalchemy/orm/decl_api.py +2283 -0
  142. sqlalchemy/orm/decl_base.py +2320 -0
  143. sqlalchemy/orm/dependency.py +1306 -0
  144. sqlalchemy/orm/descriptor_props.py +1183 -0
  145. sqlalchemy/orm/dynamic.py +306 -0
  146. sqlalchemy/orm/evaluator.py +378 -0
  147. sqlalchemy/orm/events.py +3387 -0
  148. sqlalchemy/orm/exc.py +237 -0
  149. sqlalchemy/orm/identity.py +302 -0
  150. sqlalchemy/orm/instrumentation.py +749 -0
  151. sqlalchemy/orm/interfaces.py +1595 -0
  152. sqlalchemy/orm/loading.py +1712 -0
  153. sqlalchemy/orm/mapped_collection.py +557 -0
  154. sqlalchemy/orm/mapper.py +4465 -0
  155. sqlalchemy/orm/path_registry.py +907 -0
  156. sqlalchemy/orm/persistence.py +1790 -0
  157. sqlalchemy/orm/properties.py +972 -0
  158. sqlalchemy/orm/query.py +3528 -0
  159. sqlalchemy/orm/relationships.py +3608 -0
  160. sqlalchemy/orm/scoping.py +2233 -0
  161. sqlalchemy/orm/session.py +5468 -0
  162. sqlalchemy/orm/state.py +1175 -0
  163. sqlalchemy/orm/state_changes.py +196 -0
  164. sqlalchemy/orm/strategies.py +3552 -0
  165. sqlalchemy/orm/strategy_options.py +2648 -0
  166. sqlalchemy/orm/sync.py +164 -0
  167. sqlalchemy/orm/unitofwork.py +797 -0
  168. sqlalchemy/orm/util.py +2461 -0
  169. sqlalchemy/orm/writeonly.py +701 -0
  170. sqlalchemy/pool/__init__.py +41 -0
  171. sqlalchemy/pool/base.py +1540 -0
  172. sqlalchemy/pool/events.py +375 -0
  173. sqlalchemy/pool/impl.py +583 -0
  174. sqlalchemy/py.typed +0 -0
  175. sqlalchemy/schema.py +75 -0
  176. sqlalchemy/sql/__init__.py +156 -0
  177. sqlalchemy/sql/_annotated_cols.py +402 -0
  178. sqlalchemy/sql/_cache_key_cy.cp315-win_amd64.pyd +0 -0
  179. sqlalchemy/sql/_cache_key_cy.py +363 -0
  180. sqlalchemy/sql/_dml_constructors.py +132 -0
  181. sqlalchemy/sql/_elements_constructors.py +2190 -0
  182. sqlalchemy/sql/_orm_types.py +19 -0
  183. sqlalchemy/sql/_selectable_constructors.py +840 -0
  184. sqlalchemy/sql/_typing.py +500 -0
  185. sqlalchemy/sql/_util_cy.cp315-win_amd64.pyd +0 -0
  186. sqlalchemy/sql/_util_cy.pxd +11 -0
  187. sqlalchemy/sql/_util_cy.py +127 -0
  188. sqlalchemy/sql/annotation.py +590 -0
  189. sqlalchemy/sql/base.py +2702 -0
  190. sqlalchemy/sql/cache_key.py +915 -0
  191. sqlalchemy/sql/coercions.py +1373 -0
  192. sqlalchemy/sql/compiler.py +8453 -0
  193. sqlalchemy/sql/crud.py +1816 -0
  194. sqlalchemy/sql/ddl.py +1962 -0
  195. sqlalchemy/sql/default_comparator.py +660 -0
  196. sqlalchemy/sql/dml.py +2018 -0
  197. sqlalchemy/sql/elements.py +6057 -0
  198. sqlalchemy/sql/events.py +458 -0
  199. sqlalchemy/sql/expression.py +171 -0
  200. sqlalchemy/sql/functions.py +2380 -0
  201. sqlalchemy/sql/lambdas.py +1442 -0
  202. sqlalchemy/sql/naming.py +204 -0
  203. sqlalchemy/sql/operators.py +2909 -0
  204. sqlalchemy/sql/roles.py +332 -0
  205. sqlalchemy/sql/schema.py +7075 -0
  206. sqlalchemy/sql/selectable.py +7634 -0
  207. sqlalchemy/sql/sqltypes.py +4130 -0
  208. sqlalchemy/sql/traversals.py +1041 -0
  209. sqlalchemy/sql/type_api.py +2450 -0
  210. sqlalchemy/sql/util.py +1496 -0
  211. sqlalchemy/sql/visitors.py +1153 -0
  212. sqlalchemy/testing/__init__.py +97 -0
  213. sqlalchemy/testing/assertions.py +1007 -0
  214. sqlalchemy/testing/assertsql.py +519 -0
  215. sqlalchemy/testing/asyncio.py +128 -0
  216. sqlalchemy/testing/cancellation.py +237 -0
  217. sqlalchemy/testing/config.py +440 -0
  218. sqlalchemy/testing/engines.py +482 -0
  219. sqlalchemy/testing/entities.py +117 -0
  220. sqlalchemy/testing/exclusions.py +501 -0
  221. sqlalchemy/testing/fixtures/__init__.py +30 -0
  222. sqlalchemy/testing/fixtures/base.py +426 -0
  223. sqlalchemy/testing/fixtures/mypy.py +247 -0
  224. sqlalchemy/testing/fixtures/orm.py +227 -0
  225. sqlalchemy/testing/fixtures/sql.py +538 -0
  226. sqlalchemy/testing/pickleable.py +155 -0
  227. sqlalchemy/testing/plugin/__init__.py +6 -0
  228. sqlalchemy/testing/plugin/bootstrap.py +50 -0
  229. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  230. sqlalchemy/testing/plugin/pytestplugin.py +896 -0
  231. sqlalchemy/testing/profiles_file.py +350 -0
  232. sqlalchemy/testing/profiling.py +294 -0
  233. sqlalchemy/testing/provision.py +633 -0
  234. sqlalchemy/testing/requirements.py +1971 -0
  235. sqlalchemy/testing/schema.py +198 -0
  236. sqlalchemy/testing/suite/__init__.py +19 -0
  237. sqlalchemy/testing/suite/test_cte.py +237 -0
  238. sqlalchemy/testing/suite/test_ddl.py +420 -0
  239. sqlalchemy/testing/suite/test_dialect.py +776 -0
  240. sqlalchemy/testing/suite/test_insert.py +630 -0
  241. sqlalchemy/testing/suite/test_reflection.py +3815 -0
  242. sqlalchemy/testing/suite/test_results.py +660 -0
  243. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  244. sqlalchemy/testing/suite/test_select.py +2112 -0
  245. sqlalchemy/testing/suite/test_sequence.py +317 -0
  246. sqlalchemy/testing/suite/test_table_via_select.py +686 -0
  247. sqlalchemy/testing/suite/test_types.py +2271 -0
  248. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  249. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  250. sqlalchemy/testing/util.py +575 -0
  251. sqlalchemy/testing/warnings.py +52 -0
  252. sqlalchemy/types.py +75 -0
  253. sqlalchemy/util/__init__.py +165 -0
  254. sqlalchemy/util/_collections.py +688 -0
  255. sqlalchemy/util/_collections_cy.cp315-win_amd64.pyd +0 -0
  256. sqlalchemy/util/_collections_cy.pxd +8 -0
  257. sqlalchemy/util/_collections_cy.py +516 -0
  258. sqlalchemy/util/_has_cython.py +48 -0
  259. sqlalchemy/util/_immutabledict_cy.cp315-win_amd64.pyd +0 -0
  260. sqlalchemy/util/_immutabledict_cy.py +240 -0
  261. sqlalchemy/util/compat.py +298 -0
  262. sqlalchemy/util/concurrency.py +272 -0
  263. sqlalchemy/util/cython.py +95 -0
  264. sqlalchemy/util/deprecations.py +401 -0
  265. sqlalchemy/util/langhelpers.py +2797 -0
  266. sqlalchemy/util/preloaded.py +153 -0
  267. sqlalchemy/util/queue.py +304 -0
  268. sqlalchemy/util/tool_support.py +202 -0
  269. sqlalchemy/util/topological.py +120 -0
  270. sqlalchemy/util/typing.py +709 -0
  271. sqlalchemy-2.1.0rc1.dist-info/METADATA +270 -0
  272. sqlalchemy-2.1.0rc1.dist-info/RECORD +276 -0
  273. sqlalchemy-2.1.0rc1.dist-info/WHEEL +5 -0
  274. sqlalchemy-2.1.0rc1.dist-info/licenses/AUTHORS +30 -0
  275. sqlalchemy-2.1.0rc1.dist-info/licenses/LICENSE +19 -0
  276. sqlalchemy-2.1.0rc1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,630 @@
1
+ # orm/clsregistry.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
+ """Routines to handle the string class registry used by declarative.
9
+
10
+ This system allows specification of classes and expressions used in
11
+ :func:`_orm.relationship` using strings.
12
+
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import re
18
+ from typing import Any
19
+ from typing import Callable
20
+ from typing import cast
21
+ from typing import Dict
22
+ from typing import Generator
23
+ from typing import Iterable
24
+ from typing import List
25
+ from typing import Mapping
26
+ from typing import MutableMapping
27
+ from typing import NoReturn
28
+ from typing import Optional
29
+ from typing import Set
30
+ from typing import Tuple
31
+ from typing import Type
32
+ from typing import TYPE_CHECKING
33
+ from typing import TypeVar
34
+ from typing import Union
35
+ import weakref
36
+
37
+ from . import attributes
38
+ from . import interfaces
39
+ from .descriptor_props import SynonymProperty
40
+ from .properties import ColumnProperty
41
+ from .util import _metadata_for_cls
42
+ from .util import class_mapper
43
+ from .. import exc
44
+ from .. import inspection
45
+ from .. import util
46
+ from ..sql.schema import _get_table_key
47
+ from ..util.typing import CallableReference
48
+
49
+ if TYPE_CHECKING:
50
+ from .relationships import RelationshipProperty
51
+ from ..sql.schema import MetaData
52
+ from ..sql.schema import Table
53
+
54
+ _T = TypeVar("_T", bound=Any)
55
+
56
+ _ClsRegistryType = MutableMapping[str, Union[type, "_ClsRegistryToken"]]
57
+
58
+ # strong references to registries which we place in
59
+ # the _decl_class_registry, which is usually weak referencing.
60
+ # the internal registries here link to classes with weakrefs and remove
61
+ # themselves when all references to contained classes are removed.
62
+ _registries: Set[_ClsRegistryToken] = set()
63
+
64
+
65
+ def _add_class(
66
+ classname: str, cls: Type[_T], decl_class_registry: _ClsRegistryType
67
+ ) -> None:
68
+ """Add a class to the _decl_class_registry associated with the
69
+ given declarative class.
70
+
71
+ """
72
+ if classname in decl_class_registry:
73
+ # class already exists.
74
+ existing = decl_class_registry[classname]
75
+ if not isinstance(existing, _MultipleClassMarker):
76
+ decl_class_registry[classname] = _MultipleClassMarker(
77
+ [cls, cast("Type[Any]", existing)]
78
+ )
79
+ else:
80
+ decl_class_registry[classname] = cls
81
+
82
+ try:
83
+ root_module = cast(
84
+ _ModuleMarker, decl_class_registry["_sa_module_registry"]
85
+ )
86
+ except KeyError:
87
+ decl_class_registry["_sa_module_registry"] = root_module = (
88
+ _ModuleMarker("_sa_module_registry", None)
89
+ )
90
+
91
+ tokens = cls.__module__.split(".")
92
+
93
+ # build up a tree like this:
94
+ # modulename: myapp.snacks.nuts
95
+ #
96
+ # myapp->snack->nuts->(classes)
97
+ # snack->nuts->(classes)
98
+ # nuts->(classes)
99
+ #
100
+ # this allows partial token paths to be used.
101
+ while tokens:
102
+ token = tokens.pop(0)
103
+ module = root_module.get_module(token)
104
+ for token in tokens:
105
+ module = module.get_module(token)
106
+
107
+ try:
108
+ module.add_class(classname, cls)
109
+ except AttributeError as ae:
110
+ if not isinstance(module, _ModuleMarker):
111
+ raise exc.InvalidRequestError(
112
+ f'name "{classname}" matches both a '
113
+ "class name and a module name"
114
+ ) from ae
115
+ else:
116
+ raise
117
+
118
+
119
+ def _remove_class(
120
+ classname: str, cls: Type[Any], decl_class_registry: _ClsRegistryType
121
+ ) -> None:
122
+ if classname in decl_class_registry:
123
+ existing = decl_class_registry[classname]
124
+ if isinstance(existing, _MultipleClassMarker):
125
+ existing.remove_item(cls)
126
+ else:
127
+ del decl_class_registry[classname]
128
+
129
+ try:
130
+ root_module = cast(
131
+ _ModuleMarker, decl_class_registry["_sa_module_registry"]
132
+ )
133
+ except KeyError:
134
+ return
135
+
136
+ tokens = cls.__module__.split(".")
137
+
138
+ while tokens:
139
+ token = tokens.pop(0)
140
+ module = root_module.get_module(token)
141
+ for token in tokens:
142
+ module = module.get_module(token)
143
+ try:
144
+ module.remove_class(classname, cls)
145
+ except AttributeError:
146
+ if not isinstance(module, _ModuleMarker):
147
+ pass
148
+ else:
149
+ raise
150
+
151
+
152
+ def _key_is_empty(
153
+ key: str,
154
+ decl_class_registry: _ClsRegistryType,
155
+ test: Callable[[Any], bool],
156
+ ) -> bool:
157
+ """test if a key is empty of a certain object.
158
+
159
+ used for unit tests against the registry to see if garbage collection
160
+ is working.
161
+
162
+ "test" is a callable that will be passed an object should return True
163
+ if the given object is the one we were looking for.
164
+
165
+ We can't pass the actual object itself b.c. this is for testing garbage
166
+ collection; the caller will have to have removed references to the
167
+ object itself.
168
+
169
+ """
170
+ if key not in decl_class_registry:
171
+ return True
172
+
173
+ thing = decl_class_registry[key]
174
+ if isinstance(thing, _MultipleClassMarker):
175
+ for sub_thing in thing.contents:
176
+ if test(sub_thing):
177
+ return False
178
+ else:
179
+ raise NotImplementedError("unknown codepath")
180
+ else:
181
+ return not test(thing)
182
+
183
+
184
+ class _ClsRegistryToken:
185
+ """an object that can be in the registry._class_registry as a value."""
186
+
187
+ __slots__ = ()
188
+
189
+
190
+ class _MultipleClassMarker(_ClsRegistryToken):
191
+ """refers to multiple classes of the same name
192
+ within _decl_class_registry.
193
+
194
+ """
195
+
196
+ __slots__ = "on_remove", "contents", "__weakref__"
197
+
198
+ contents: Set[weakref.ref[Type[Any]]]
199
+ on_remove: CallableReference[Optional[Callable[[], None]]]
200
+
201
+ def __init__(
202
+ self,
203
+ classes: Iterable[Type[Any]],
204
+ on_remove: Optional[Callable[[], None]] = None,
205
+ ):
206
+ self.on_remove = on_remove
207
+ self.contents = {
208
+ weakref.ref(item, self._remove_item) for item in classes
209
+ }
210
+ _registries.add(self)
211
+
212
+ def remove_item(self, cls: Type[Any]) -> None:
213
+ self._remove_item(weakref.ref(cls))
214
+
215
+ def __iter__(self) -> Generator[Optional[Type[Any]], None, None]:
216
+ return (ref() for ref in self.contents)
217
+
218
+ def attempt_get(self, path: List[str], key: str) -> Type[Any]:
219
+ if len(self.contents) > 1:
220
+ raise exc.InvalidRequestError(
221
+ 'Multiple classes found for path "%s" '
222
+ "in the registry of this declarative "
223
+ "base. Please use a fully module-qualified path."
224
+ % (".".join(path + [key]))
225
+ )
226
+ else:
227
+ ref = list(self.contents)[0]
228
+ cls = ref()
229
+ if cls is None:
230
+ raise NameError(key)
231
+ return cls
232
+
233
+ def _remove_item(self, ref: weakref.ref[Type[Any]]) -> None:
234
+ self.contents.discard(ref)
235
+ if not self.contents:
236
+ _registries.discard(self)
237
+ if self.on_remove:
238
+ self.on_remove()
239
+
240
+ def add_item(self, item: Type[Any]) -> None:
241
+ # protect against class registration race condition against
242
+ # asynchronous garbage collection calling _remove_item,
243
+ # [ticket:3208] and [ticket:10782]
244
+ modules = {
245
+ cls.__module__
246
+ for cls in [ref() for ref in list(self.contents)]
247
+ if cls is not None
248
+ }
249
+ if item.__module__ in modules:
250
+ util.warn(
251
+ "This declarative base already contains a class with the "
252
+ "same class name and module name as %s.%s, and will "
253
+ "be replaced in the string-lookup table."
254
+ % (item.__module__, item.__name__)
255
+ )
256
+ self.contents.add(weakref.ref(item, self._remove_item))
257
+
258
+
259
+ class _ModuleMarker(_ClsRegistryToken):
260
+ """Refers to a module name within
261
+ _decl_class_registry.
262
+
263
+ """
264
+
265
+ __slots__ = "parent", "name", "contents", "mod_ns", "path", "__weakref__"
266
+
267
+ parent: Optional[_ModuleMarker]
268
+ contents: Dict[str, Union[_ModuleMarker, _MultipleClassMarker]]
269
+ mod_ns: _ModNS
270
+ path: List[str]
271
+
272
+ def __init__(self, name: str, parent: Optional[_ModuleMarker]):
273
+ self.parent = parent
274
+ self.name = name
275
+ self.contents = {}
276
+ self.mod_ns = _ModNS(self)
277
+ if self.parent:
278
+ self.path = self.parent.path + [self.name]
279
+ else:
280
+ self.path = []
281
+ _registries.add(self)
282
+
283
+ def __contains__(self, name: str) -> bool:
284
+ return name in self.contents
285
+
286
+ def __getitem__(self, name: str) -> _ClsRegistryToken:
287
+ return self.contents[name]
288
+
289
+ def _remove_item(self, name: str) -> None:
290
+ self.contents.pop(name, None)
291
+ if not self.contents:
292
+ if self.parent is not None:
293
+ self.parent._remove_item(self.name)
294
+ _registries.discard(self)
295
+
296
+ def resolve_attr(self, key: str) -> Union[_ModNS, Type[Any]]:
297
+ return self.mod_ns.__getattr__(key)
298
+
299
+ def get_module(self, name: str) -> _ModuleMarker:
300
+ if name not in self.contents:
301
+ marker = _ModuleMarker(name, self)
302
+ self.contents[name] = marker
303
+ else:
304
+ marker = cast(_ModuleMarker, self.contents[name])
305
+ return marker
306
+
307
+ def add_class(self, name: str, cls: Type[Any]) -> None:
308
+ if name in self.contents:
309
+ existing = cast(_MultipleClassMarker, self.contents[name])
310
+ try:
311
+ existing.add_item(cls)
312
+ except AttributeError as ae:
313
+ if not isinstance(existing, _MultipleClassMarker):
314
+ raise exc.InvalidRequestError(
315
+ f'name "{name}" matches both a '
316
+ "class name and a module name"
317
+ ) from ae
318
+ else:
319
+ raise
320
+ else:
321
+ self.contents[name] = _MultipleClassMarker(
322
+ [cls], on_remove=lambda: self._remove_item(name)
323
+ )
324
+
325
+ def remove_class(self, name: str, cls: Type[Any]) -> None:
326
+ if name in self.contents:
327
+ existing = cast(_MultipleClassMarker, self.contents[name])
328
+ existing.remove_item(cls)
329
+
330
+
331
+ class _ModNS:
332
+ __slots__ = ("__parent",)
333
+
334
+ __parent: _ModuleMarker
335
+
336
+ def __init__(self, parent: _ModuleMarker):
337
+ self.__parent = parent
338
+
339
+ def __getattr__(self, key: str) -> Union[_ModNS, Type[Any]]:
340
+ try:
341
+ value = self.__parent.contents[key]
342
+ except KeyError:
343
+ pass
344
+ else:
345
+ if value is not None:
346
+ if isinstance(value, _ModuleMarker):
347
+ return value.mod_ns
348
+ else:
349
+ assert isinstance(value, _MultipleClassMarker)
350
+ return value.attempt_get(self.__parent.path, key)
351
+ raise NameError(
352
+ "Module %r has no mapped classes "
353
+ "registered under the name %r" % (self.__parent.name, key)
354
+ )
355
+
356
+
357
+ class _GetColumns:
358
+ __slots__ = ("cls",)
359
+
360
+ cls: Type[Any]
361
+
362
+ def __init__(self, cls: Type[Any]):
363
+ self.cls = cls
364
+
365
+ def __getattr__(self, key: str) -> Any:
366
+ mp = class_mapper(self.cls, configure=False)
367
+ if mp:
368
+ if key not in mp.all_orm_descriptors:
369
+ raise AttributeError(
370
+ "Class %r does not have a mapped column named %r"
371
+ % (self.cls, key)
372
+ )
373
+
374
+ desc = mp.all_orm_descriptors[key]
375
+ if desc.extension_type is interfaces.NotExtension.NOT_EXTENSION:
376
+ assert isinstance(desc, attributes.QueryableAttribute)
377
+ prop = desc.property
378
+ if isinstance(prop, SynonymProperty):
379
+ key = prop.name
380
+ elif not isinstance(prop, ColumnProperty):
381
+ raise exc.InvalidRequestError(
382
+ "Property %r is not an instance of"
383
+ " ColumnProperty (i.e. does not correspond"
384
+ " directly to a Column)." % key
385
+ )
386
+ return getattr(self.cls, key)
387
+
388
+
389
+ inspection._inspects(_GetColumns)(
390
+ lambda target: inspection.inspect(target.cls)
391
+ )
392
+
393
+
394
+ class _GetTable:
395
+ __slots__ = "key", "metadata"
396
+
397
+ key: str
398
+ metadata: MetaData
399
+
400
+ def __init__(self, key: str, metadata: MetaData):
401
+ self.key = key
402
+ self.metadata = metadata
403
+
404
+ def __getattr__(self, key: str) -> Table:
405
+ return self.metadata.tables[_get_table_key(key, self.key)]
406
+
407
+
408
+ def _determine_container(key: str, value: Any) -> _GetColumns:
409
+ if isinstance(value, _MultipleClassMarker):
410
+ value = value.attempt_get([], key)
411
+ return _GetColumns(value)
412
+
413
+
414
+ class _class_resolver:
415
+ __slots__ = (
416
+ "cls",
417
+ "prop",
418
+ "arg",
419
+ "fallback",
420
+ "_dict",
421
+ "_resolvers",
422
+ "tables_only",
423
+ )
424
+
425
+ cls: Type[Any]
426
+ prop: RelationshipProperty[Any]
427
+ fallback: Mapping[str, Any]
428
+ arg: str
429
+ tables_only: bool
430
+ _resolvers: Tuple[Callable[[str], Any], ...]
431
+
432
+ def __init__(
433
+ self,
434
+ cls: Type[Any],
435
+ prop: RelationshipProperty[Any],
436
+ fallback: Mapping[str, Any],
437
+ arg: str,
438
+ tables_only: bool = False,
439
+ ):
440
+ self.cls = cls
441
+ self.prop = prop
442
+ self.arg = arg
443
+ self.fallback = fallback
444
+ self._dict = util.PopulateDict(self._access_cls)
445
+ self._resolvers = ()
446
+ self.tables_only = tables_only
447
+
448
+ def _resolve_table_key(
449
+ self, key: str, metadata: MetaData
450
+ ) -> Optional[Table]:
451
+ if metadata.schema is not None and "." not in key:
452
+ schema_key = _get_table_key(key, metadata.schema)
453
+ if schema_key in metadata.tables:
454
+ return metadata.tables[schema_key]
455
+ if key in metadata.tables:
456
+ util.warn_deprecated(
457
+ "The string '%s' was resolved to the "
458
+ "non-schema-qualified table '%s', however "
459
+ "the MetaData object has a default schema "
460
+ "of '%s'. In a future version of SQLAlchemy, "
461
+ "this unqualified name will be resolved as "
462
+ "'%s'. To reference a table without a "
463
+ "schema, use the Table object directly."
464
+ % (key, key, metadata.schema, schema_key),
465
+ "2.1",
466
+ )
467
+ return metadata.tables[key]
468
+ elif key in metadata.tables:
469
+ return metadata.tables[key]
470
+ return None
471
+
472
+ def _access_cls(self, key: str) -> Any:
473
+ cls = self.cls
474
+
475
+ manager = attributes.manager_of_class(cls)
476
+ registry = manager.registry
477
+ assert registry is not None
478
+ decl_class_registry = registry._class_registry
479
+ metadata = _metadata_for_cls(cls, registry)
480
+
481
+ if self.tables_only:
482
+ table = self._resolve_table_key(key, metadata)
483
+ if table is not None:
484
+ return table
485
+ elif key in metadata._schemas:
486
+ return _GetTable(key, metadata)
487
+
488
+ if key in decl_class_registry:
489
+ dt = _determine_container(key, decl_class_registry[key])
490
+ if self.tables_only:
491
+ return dt.cls
492
+ else:
493
+ return dt
494
+
495
+ if not self.tables_only:
496
+ table = self._resolve_table_key(key, metadata)
497
+ if table is not None:
498
+ return table
499
+ elif key in metadata._schemas:
500
+ return _GetTable(key, metadata)
501
+
502
+ if "_sa_module_registry" in decl_class_registry and key in cast(
503
+ _ModuleMarker, decl_class_registry["_sa_module_registry"]
504
+ ):
505
+ _module_registry = cast(
506
+ _ModuleMarker, decl_class_registry["_sa_module_registry"]
507
+ )
508
+ return _module_registry.resolve_attr(key)
509
+
510
+ if self._resolvers:
511
+ for resolv in self._resolvers:
512
+ value = resolv(key)
513
+ if value is not None:
514
+ return value
515
+
516
+ return self.fallback[key]
517
+
518
+ def _raise_for_name(self, name: str, err: Exception) -> NoReturn:
519
+ generic_match = re.match(r"(.+)\[(.+)\]", name)
520
+
521
+ if generic_match:
522
+ clsarg = generic_match.group(2).strip("'")
523
+ raise exc.InvalidRequestError(
524
+ f"When initializing mapper {self.prop.parent}, "
525
+ f'expression "relationship({self.arg!r})" seems to be '
526
+ "using a generic class as the argument to relationship(); "
527
+ "please state the generic argument "
528
+ "using an annotation, e.g. "
529
+ f'"{self.prop.key}: Mapped[{generic_match.group(1)}'
530
+ f"['{clsarg}']] = relationship()\""
531
+ ) from err
532
+ else:
533
+ manager = attributes.manager_of_class(self.cls)
534
+ registry = manager.registry
535
+ metadata = (
536
+ _metadata_for_cls(self.cls, registry)
537
+ if registry is not None
538
+ else None
539
+ )
540
+
541
+ # when deprecated fallback lookup in
542
+ # _resolve_table_key is removed, consider adding
543
+ # additional context to the error message if the
544
+ # unqualified key is located under BLANK_SCHEMA
545
+ if metadata is not None and metadata.schema is not None:
546
+ schema_key = _get_table_key(name, metadata.schema)
547
+ assert schema_key not in metadata.tables
548
+
549
+ raise exc.InvalidRequestError(
550
+ "When initializing mapper %s, expression %r failed to "
551
+ "locate a name (%r). If this is a class name, consider "
552
+ "adding this relationship() to the %r class after "
553
+ "both dependent classes have been defined."
554
+ % (
555
+ self.prop.parent,
556
+ self.arg,
557
+ name,
558
+ self.cls,
559
+ )
560
+ ) from err
561
+
562
+ def _resolve_name(self) -> Union[Table, Type[Any], _ModNS]:
563
+ name = self.arg
564
+ d = self._dict
565
+ rval = None
566
+ try:
567
+ for token in name.split("."):
568
+ if rval is None:
569
+ rval = d[token]
570
+ else:
571
+ rval = getattr(rval, token)
572
+ except KeyError as err:
573
+ self._raise_for_name(name, err)
574
+ except NameError as n:
575
+ self._raise_for_name(n.args[0], n)
576
+ else:
577
+ if isinstance(rval, _GetColumns):
578
+ return rval.cls
579
+ else:
580
+ if TYPE_CHECKING:
581
+ assert isinstance(rval, (type, Table, _ModNS))
582
+ return rval
583
+
584
+ def __call__(self) -> Any:
585
+ if self.tables_only:
586
+ try:
587
+ return self._dict[self.arg]
588
+ except KeyError as k:
589
+ self._raise_for_name(self.arg, k)
590
+ else:
591
+ try:
592
+ x = eval(self.arg, globals(), self._dict)
593
+
594
+ if isinstance(x, _GetColumns):
595
+ return x.cls
596
+ else:
597
+ return x
598
+ except NameError as n:
599
+ self._raise_for_name(n.args[0], n)
600
+
601
+
602
+ _fallback_dict: Mapping[str, Any] = None # type: ignore[assignment]
603
+
604
+
605
+ def _resolver(cls: Type[Any], prop: RelationshipProperty[Any]) -> Tuple[
606
+ Callable[[str], Callable[[], Union[Type[Any], Table, _ModNS]]],
607
+ Callable[[str, bool], _class_resolver],
608
+ ]:
609
+ global _fallback_dict
610
+
611
+ if _fallback_dict is None:
612
+ import sqlalchemy
613
+ from . import foreign
614
+ from . import remote
615
+
616
+ _fallback_dict = util.immutabledict(sqlalchemy.__dict__).union(
617
+ {"foreign": foreign, "remote": remote}
618
+ )
619
+
620
+ def resolve_arg(arg: str, tables_only: bool = False) -> _class_resolver:
621
+ return _class_resolver(
622
+ cls, prop, _fallback_dict, arg, tables_only=tables_only
623
+ )
624
+
625
+ def resolve_name(
626
+ arg: str,
627
+ ) -> Callable[[], Union[Type[Any], Table, _ModNS]]:
628
+ return _class_resolver(cls, prop, _fallback_dict, arg)._resolve_name
629
+
630
+ return resolve_name, resolve_arg