SQLAlchemy 2.0.36__cp313-cp313-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 (273) hide show
  1. SQLAlchemy-2.0.36.dist-info/LICENSE +19 -0
  2. SQLAlchemy-2.0.36.dist-info/METADATA +243 -0
  3. SQLAlchemy-2.0.36.dist-info/RECORD +273 -0
  4. SQLAlchemy-2.0.36.dist-info/WHEEL +5 -0
  5. SQLAlchemy-2.0.36.dist-info/top_level.txt +1 -0
  6. sqlalchemy/__init__.py +294 -0
  7. sqlalchemy/connectors/__init__.py +18 -0
  8. sqlalchemy/connectors/aioodbc.py +174 -0
  9. sqlalchemy/connectors/asyncio.py +213 -0
  10. sqlalchemy/connectors/pyodbc.py +249 -0
  11. sqlalchemy/cyextension/__init__.py +6 -0
  12. sqlalchemy/cyextension/collections.cp313-win_amd64.pyd +0 -0
  13. sqlalchemy/cyextension/collections.pyx +409 -0
  14. sqlalchemy/cyextension/immutabledict.cp313-win_amd64.pyd +0 -0
  15. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  16. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  17. sqlalchemy/cyextension/processors.cp313-win_amd64.pyd +0 -0
  18. sqlalchemy/cyextension/processors.pyx +68 -0
  19. sqlalchemy/cyextension/resultproxy.cp313-win_amd64.pyd +0 -0
  20. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  21. sqlalchemy/cyextension/util.cp313-win_amd64.pyd +0 -0
  22. sqlalchemy/cyextension/util.pyx +91 -0
  23. sqlalchemy/dialects/__init__.py +61 -0
  24. sqlalchemy/dialects/_typing.py +25 -0
  25. sqlalchemy/dialects/mssql/__init__.py +88 -0
  26. sqlalchemy/dialects/mssql/aioodbc.py +64 -0
  27. sqlalchemy/dialects/mssql/base.py +4010 -0
  28. sqlalchemy/dialects/mssql/information_schema.py +254 -0
  29. sqlalchemy/dialects/mssql/json.py +133 -0
  30. sqlalchemy/dialects/mssql/provision.py +162 -0
  31. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  32. sqlalchemy/dialects/mssql/pyodbc.py +745 -0
  33. sqlalchemy/dialects/mysql/__init__.py +101 -0
  34. sqlalchemy/dialects/mysql/aiomysql.py +333 -0
  35. sqlalchemy/dialects/mysql/asyncmy.py +337 -0
  36. sqlalchemy/dialects/mysql/base.py +3494 -0
  37. sqlalchemy/dialects/mysql/cymysql.py +84 -0
  38. sqlalchemy/dialects/mysql/dml.py +219 -0
  39. sqlalchemy/dialects/mysql/enumerated.py +244 -0
  40. sqlalchemy/dialects/mysql/expression.py +141 -0
  41. sqlalchemy/dialects/mysql/json.py +81 -0
  42. sqlalchemy/dialects/mysql/mariadb.py +32 -0
  43. sqlalchemy/dialects/mysql/mariadbconnector.py +277 -0
  44. sqlalchemy/dialects/mysql/mysqlconnector.py +180 -0
  45. sqlalchemy/dialects/mysql/mysqldb.py +303 -0
  46. sqlalchemy/dialects/mysql/provision.py +110 -0
  47. sqlalchemy/dialects/mysql/pymysql.py +137 -0
  48. sqlalchemy/dialects/mysql/pyodbc.py +138 -0
  49. sqlalchemy/dialects/mysql/reflection.py +677 -0
  50. sqlalchemy/dialects/mysql/reserved_words.py +571 -0
  51. sqlalchemy/dialects/mysql/types.py +774 -0
  52. sqlalchemy/dialects/oracle/__init__.py +67 -0
  53. sqlalchemy/dialects/oracle/base.py +3271 -0
  54. sqlalchemy/dialects/oracle/cx_oracle.py +1483 -0
  55. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  56. sqlalchemy/dialects/oracle/oracledb.py +431 -0
  57. sqlalchemy/dialects/oracle/provision.py +220 -0
  58. sqlalchemy/dialects/oracle/types.py +287 -0
  59. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  60. sqlalchemy/dialects/postgresql/_psycopg_common.py +187 -0
  61. sqlalchemy/dialects/postgresql/array.py +425 -0
  62. sqlalchemy/dialects/postgresql/asyncpg.py +1274 -0
  63. sqlalchemy/dialects/postgresql/base.py +5008 -0
  64. sqlalchemy/dialects/postgresql/dml.py +310 -0
  65. sqlalchemy/dialects/postgresql/ext.py +496 -0
  66. sqlalchemy/dialects/postgresql/hstore.py +397 -0
  67. sqlalchemy/dialects/postgresql/json.py +333 -0
  68. sqlalchemy/dialects/postgresql/named_types.py +509 -0
  69. sqlalchemy/dialects/postgresql/operators.py +129 -0
  70. sqlalchemy/dialects/postgresql/pg8000.py +662 -0
  71. sqlalchemy/dialects/postgresql/pg_catalog.py +300 -0
  72. sqlalchemy/dialects/postgresql/provision.py +175 -0
  73. sqlalchemy/dialects/postgresql/psycopg.py +772 -0
  74. sqlalchemy/dialects/postgresql/psycopg2.py +886 -0
  75. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  76. sqlalchemy/dialects/postgresql/ranges.py +1029 -0
  77. sqlalchemy/dialects/postgresql/types.py +303 -0
  78. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  79. sqlalchemy/dialects/sqlite/aiosqlite.py +396 -0
  80. sqlalchemy/dialects/sqlite/base.py +2805 -0
  81. sqlalchemy/dialects/sqlite/dml.py +240 -0
  82. sqlalchemy/dialects/sqlite/json.py +92 -0
  83. sqlalchemy/dialects/sqlite/provision.py +198 -0
  84. sqlalchemy/dialects/sqlite/pysqlcipher.py +155 -0
  85. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  86. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  87. sqlalchemy/engine/__init__.py +62 -0
  88. sqlalchemy/engine/_py_processors.py +136 -0
  89. sqlalchemy/engine/_py_row.py +128 -0
  90. sqlalchemy/engine/_py_util.py +74 -0
  91. sqlalchemy/engine/base.py +3375 -0
  92. sqlalchemy/engine/characteristics.py +155 -0
  93. sqlalchemy/engine/create.py +875 -0
  94. sqlalchemy/engine/cursor.py +2181 -0
  95. sqlalchemy/engine/default.py +2365 -0
  96. sqlalchemy/engine/events.py +951 -0
  97. sqlalchemy/engine/interfaces.py +3403 -0
  98. sqlalchemy/engine/mock.py +131 -0
  99. sqlalchemy/engine/processors.py +61 -0
  100. sqlalchemy/engine/reflection.py +2098 -0
  101. sqlalchemy/engine/result.py +2382 -0
  102. sqlalchemy/engine/row.py +401 -0
  103. sqlalchemy/engine/strategies.py +19 -0
  104. sqlalchemy/engine/url.py +910 -0
  105. sqlalchemy/engine/util.py +167 -0
  106. sqlalchemy/event/__init__.py +25 -0
  107. sqlalchemy/event/api.py +225 -0
  108. sqlalchemy/event/attr.py +655 -0
  109. sqlalchemy/event/base.py +470 -0
  110. sqlalchemy/event/legacy.py +246 -0
  111. sqlalchemy/event/registry.py +386 -0
  112. sqlalchemy/events.py +17 -0
  113. sqlalchemy/exc.py +830 -0
  114. sqlalchemy/ext/__init__.py +11 -0
  115. sqlalchemy/ext/associationproxy.py +2013 -0
  116. sqlalchemy/ext/asyncio/__init__.py +25 -0
  117. sqlalchemy/ext/asyncio/base.py +279 -0
  118. sqlalchemy/ext/asyncio/engine.py +1466 -0
  119. sqlalchemy/ext/asyncio/exc.py +21 -0
  120. sqlalchemy/ext/asyncio/result.py +961 -0
  121. sqlalchemy/ext/asyncio/scoping.py +1614 -0
  122. sqlalchemy/ext/asyncio/session.py +1936 -0
  123. sqlalchemy/ext/automap.py +1691 -0
  124. sqlalchemy/ext/baked.py +574 -0
  125. sqlalchemy/ext/compiler.py +570 -0
  126. sqlalchemy/ext/declarative/__init__.py +65 -0
  127. sqlalchemy/ext/declarative/extensions.py +548 -0
  128. sqlalchemy/ext/horizontal_shard.py +481 -0
  129. sqlalchemy/ext/hybrid.py +1514 -0
  130. sqlalchemy/ext/indexable.py +341 -0
  131. sqlalchemy/ext/instrumentation.py +450 -0
  132. sqlalchemy/ext/mutable.py +1073 -0
  133. sqlalchemy/ext/mypy/__init__.py +6 -0
  134. sqlalchemy/ext/mypy/apply.py +320 -0
  135. sqlalchemy/ext/mypy/decl_class.py +515 -0
  136. sqlalchemy/ext/mypy/infer.py +590 -0
  137. sqlalchemy/ext/mypy/names.py +335 -0
  138. sqlalchemy/ext/mypy/plugin.py +303 -0
  139. sqlalchemy/ext/mypy/util.py +357 -0
  140. sqlalchemy/ext/orderinglist.py +416 -0
  141. sqlalchemy/ext/serializer.py +181 -0
  142. sqlalchemy/future/__init__.py +16 -0
  143. sqlalchemy/future/engine.py +15 -0
  144. sqlalchemy/inspection.py +174 -0
  145. sqlalchemy/log.py +288 -0
  146. sqlalchemy/orm/__init__.py +170 -0
  147. sqlalchemy/orm/_orm_constructors.py +2571 -0
  148. sqlalchemy/orm/_typing.py +179 -0
  149. sqlalchemy/orm/attributes.py +2835 -0
  150. sqlalchemy/orm/base.py +973 -0
  151. sqlalchemy/orm/bulk_persistence.py +2123 -0
  152. sqlalchemy/orm/clsregistry.py +571 -0
  153. sqlalchemy/orm/collections.py +1620 -0
  154. sqlalchemy/orm/context.py +3268 -0
  155. sqlalchemy/orm/decl_api.py +1883 -0
  156. sqlalchemy/orm/decl_base.py +2190 -0
  157. sqlalchemy/orm/dependency.py +1304 -0
  158. sqlalchemy/orm/descriptor_props.py +1076 -0
  159. sqlalchemy/orm/dynamic.py +300 -0
  160. sqlalchemy/orm/evaluator.py +379 -0
  161. sqlalchemy/orm/events.py +3261 -0
  162. sqlalchemy/orm/exc.py +228 -0
  163. sqlalchemy/orm/identity.py +302 -0
  164. sqlalchemy/orm/instrumentation.py +754 -0
  165. sqlalchemy/orm/interfaces.py +1474 -0
  166. sqlalchemy/orm/loading.py +1682 -0
  167. sqlalchemy/orm/mapped_collection.py +557 -0
  168. sqlalchemy/orm/mapper.py +4432 -0
  169. sqlalchemy/orm/path_registry.py +811 -0
  170. sqlalchemy/orm/persistence.py +1782 -0
  171. sqlalchemy/orm/properties.py +886 -0
  172. sqlalchemy/orm/query.py +3396 -0
  173. sqlalchemy/orm/relationships.py +3500 -0
  174. sqlalchemy/orm/scoping.py +2165 -0
  175. sqlalchemy/orm/session.py +5301 -0
  176. sqlalchemy/orm/state.py +1143 -0
  177. sqlalchemy/orm/state_changes.py +198 -0
  178. sqlalchemy/orm/strategies.py +3473 -0
  179. sqlalchemy/orm/strategy_options.py +2569 -0
  180. sqlalchemy/orm/sync.py +164 -0
  181. sqlalchemy/orm/unitofwork.py +796 -0
  182. sqlalchemy/orm/util.py +2424 -0
  183. sqlalchemy/orm/writeonly.py +678 -0
  184. sqlalchemy/pool/__init__.py +44 -0
  185. sqlalchemy/pool/base.py +1515 -0
  186. sqlalchemy/pool/events.py +370 -0
  187. sqlalchemy/pool/impl.py +581 -0
  188. sqlalchemy/py.typed +0 -0
  189. sqlalchemy/schema.py +70 -0
  190. sqlalchemy/sql/__init__.py +145 -0
  191. sqlalchemy/sql/_dml_constructors.py +140 -0
  192. sqlalchemy/sql/_elements_constructors.py +1850 -0
  193. sqlalchemy/sql/_orm_types.py +20 -0
  194. sqlalchemy/sql/_py_util.py +75 -0
  195. sqlalchemy/sql/_selectable_constructors.py +635 -0
  196. sqlalchemy/sql/_typing.py +460 -0
  197. sqlalchemy/sql/annotation.py +585 -0
  198. sqlalchemy/sql/base.py +2185 -0
  199. sqlalchemy/sql/cache_key.py +1057 -0
  200. sqlalchemy/sql/coercions.py +1405 -0
  201. sqlalchemy/sql/compiler.py +7818 -0
  202. sqlalchemy/sql/crud.py +1669 -0
  203. sqlalchemy/sql/ddl.py +1378 -0
  204. sqlalchemy/sql/default_comparator.py +552 -0
  205. sqlalchemy/sql/dml.py +1817 -0
  206. sqlalchemy/sql/elements.py +5499 -0
  207. sqlalchemy/sql/events.py +455 -0
  208. sqlalchemy/sql/expression.py +162 -0
  209. sqlalchemy/sql/functions.py +2055 -0
  210. sqlalchemy/sql/lambdas.py +1449 -0
  211. sqlalchemy/sql/naming.py +212 -0
  212. sqlalchemy/sql/operators.py +2579 -0
  213. sqlalchemy/sql/roles.py +323 -0
  214. sqlalchemy/sql/schema.py +6158 -0
  215. sqlalchemy/sql/selectable.py +7004 -0
  216. sqlalchemy/sql/sqltypes.py +3827 -0
  217. sqlalchemy/sql/traversals.py +1024 -0
  218. sqlalchemy/sql/type_api.py +2339 -0
  219. sqlalchemy/sql/util.py +1486 -0
  220. sqlalchemy/sql/visitors.py +1165 -0
  221. sqlalchemy/testing/__init__.py +96 -0
  222. sqlalchemy/testing/assertions.py +989 -0
  223. sqlalchemy/testing/assertsql.py +516 -0
  224. sqlalchemy/testing/asyncio.py +135 -0
  225. sqlalchemy/testing/config.py +427 -0
  226. sqlalchemy/testing/engines.py +472 -0
  227. sqlalchemy/testing/entities.py +117 -0
  228. sqlalchemy/testing/exclusions.py +435 -0
  229. sqlalchemy/testing/fixtures/__init__.py +28 -0
  230. sqlalchemy/testing/fixtures/base.py +366 -0
  231. sqlalchemy/testing/fixtures/mypy.py +312 -0
  232. sqlalchemy/testing/fixtures/orm.py +227 -0
  233. sqlalchemy/testing/fixtures/sql.py +503 -0
  234. sqlalchemy/testing/pickleable.py +155 -0
  235. sqlalchemy/testing/plugin/__init__.py +6 -0
  236. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  237. sqlalchemy/testing/plugin/plugin_base.py +779 -0
  238. sqlalchemy/testing/plugin/pytestplugin.py +868 -0
  239. sqlalchemy/testing/profiling.py +324 -0
  240. sqlalchemy/testing/provision.py +496 -0
  241. sqlalchemy/testing/requirements.py +1818 -0
  242. sqlalchemy/testing/schema.py +224 -0
  243. sqlalchemy/testing/suite/__init__.py +19 -0
  244. sqlalchemy/testing/suite/test_cte.py +211 -0
  245. sqlalchemy/testing/suite/test_ddl.py +389 -0
  246. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  247. sqlalchemy/testing/suite/test_dialect.py +740 -0
  248. sqlalchemy/testing/suite/test_insert.py +630 -0
  249. sqlalchemy/testing/suite/test_reflection.py +3225 -0
  250. sqlalchemy/testing/suite/test_results.py +502 -0
  251. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  252. sqlalchemy/testing/suite/test_select.py +1999 -0
  253. sqlalchemy/testing/suite/test_sequence.py +317 -0
  254. sqlalchemy/testing/suite/test_types.py +2141 -0
  255. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  256. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  257. sqlalchemy/testing/util.py +537 -0
  258. sqlalchemy/testing/warnings.py +52 -0
  259. sqlalchemy/types.py +76 -0
  260. sqlalchemy/util/__init__.py +160 -0
  261. sqlalchemy/util/_collections.py +715 -0
  262. sqlalchemy/util/_concurrency_py3k.py +288 -0
  263. sqlalchemy/util/_has_cy.py +40 -0
  264. sqlalchemy/util/_py_collections.py +541 -0
  265. sqlalchemy/util/compat.py +301 -0
  266. sqlalchemy/util/concurrency.py +108 -0
  267. sqlalchemy/util/deprecations.py +401 -0
  268. sqlalchemy/util/langhelpers.py +2218 -0
  269. sqlalchemy/util/preloaded.py +150 -0
  270. sqlalchemy/util/queue.py +322 -0
  271. sqlalchemy/util/tool_support.py +201 -0
  272. sqlalchemy/util/topological.py +120 -0
  273. sqlalchemy/util/typing.py +629 -0
@@ -0,0 +1,1165 @@
1
+ # sql/visitors.py
2
+ # Copyright (C) 2005-2024 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
+ """Visitor/traversal interface and library functions.
9
+
10
+
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from collections import deque
16
+ from enum import Enum
17
+ import itertools
18
+ import operator
19
+ import typing
20
+ from typing import Any
21
+ from typing import Callable
22
+ from typing import cast
23
+ from typing import ClassVar
24
+ from typing import Dict
25
+ from typing import Iterable
26
+ from typing import Iterator
27
+ from typing import List
28
+ from typing import Mapping
29
+ from typing import Optional
30
+ from typing import overload
31
+ from typing import Tuple
32
+ from typing import Type
33
+ from typing import TYPE_CHECKING
34
+ from typing import TypeVar
35
+ from typing import Union
36
+
37
+ from .. import exc
38
+ from .. import util
39
+ from ..util import langhelpers
40
+ from ..util._has_cy import HAS_CYEXTENSION
41
+ from ..util.typing import Literal
42
+ from ..util.typing import Protocol
43
+ from ..util.typing import Self
44
+
45
+ if TYPE_CHECKING:
46
+ from .annotation import _AnnotationDict
47
+ from .elements import ColumnElement
48
+
49
+ if typing.TYPE_CHECKING or not HAS_CYEXTENSION:
50
+ from ._py_util import prefix_anon_map as prefix_anon_map
51
+ from ._py_util import cache_anon_map as anon_map
52
+ else:
53
+ from sqlalchemy.cyextension.util import ( # noqa: F401,E501
54
+ prefix_anon_map as prefix_anon_map,
55
+ )
56
+ from sqlalchemy.cyextension.util import ( # noqa: F401,E501
57
+ cache_anon_map as anon_map,
58
+ )
59
+
60
+
61
+ __all__ = [
62
+ "iterate",
63
+ "traverse_using",
64
+ "traverse",
65
+ "cloned_traverse",
66
+ "replacement_traverse",
67
+ "Visitable",
68
+ "ExternalTraversal",
69
+ "InternalTraversal",
70
+ "anon_map",
71
+ ]
72
+
73
+
74
+ class _CompilerDispatchType(Protocol):
75
+ def __call__(_self, self: Visitable, visitor: Any, **kw: Any) -> Any: ...
76
+
77
+
78
+ class Visitable:
79
+ """Base class for visitable objects.
80
+
81
+ :class:`.Visitable` is used to implement the SQL compiler dispatch
82
+ functions. Other forms of traversal such as for cache key generation
83
+ are implemented separately using the :class:`.HasTraverseInternals`
84
+ interface.
85
+
86
+ .. versionchanged:: 2.0 The :class:`.Visitable` class was named
87
+ :class:`.Traversible` in the 1.4 series; the name is changed back
88
+ to :class:`.Visitable` in 2.0 which is what it was prior to 1.4.
89
+
90
+ Both names remain importable in both 1.4 and 2.0 versions.
91
+
92
+ """
93
+
94
+ __slots__ = ()
95
+
96
+ __visit_name__: str
97
+
98
+ _original_compiler_dispatch: _CompilerDispatchType
99
+
100
+ if typing.TYPE_CHECKING:
101
+
102
+ def _compiler_dispatch(self, visitor: Any, **kw: Any) -> str: ...
103
+
104
+ def __init_subclass__(cls) -> None:
105
+ if "__visit_name__" in cls.__dict__:
106
+ cls._generate_compiler_dispatch()
107
+ super().__init_subclass__()
108
+
109
+ @classmethod
110
+ def _generate_compiler_dispatch(cls) -> None:
111
+ visit_name = cls.__visit_name__
112
+
113
+ if "_compiler_dispatch" in cls.__dict__:
114
+ # class has a fixed _compiler_dispatch() method.
115
+ # copy it to "original" so that we can get it back if
116
+ # sqlalchemy.ext.compiles overrides it.
117
+ cls._original_compiler_dispatch = cls._compiler_dispatch
118
+ return
119
+
120
+ if not isinstance(visit_name, str):
121
+ raise exc.InvalidRequestError(
122
+ f"__visit_name__ on class {cls.__name__} must be a string "
123
+ "at the class level"
124
+ )
125
+
126
+ name = "visit_%s" % visit_name
127
+ getter = operator.attrgetter(name)
128
+
129
+ def _compiler_dispatch(
130
+ self: Visitable, visitor: Any, **kw: Any
131
+ ) -> str:
132
+ """Look for an attribute named "visit_<visit_name>" on the
133
+ visitor, and call it with the same kw params.
134
+
135
+ """
136
+ try:
137
+ meth = getter(visitor)
138
+ except AttributeError as err:
139
+ return visitor.visit_unsupported_compilation(self, err, **kw) # type: ignore # noqa: E501
140
+ else:
141
+ return meth(self, **kw) # type: ignore # noqa: E501
142
+
143
+ cls._compiler_dispatch = ( # type: ignore
144
+ cls._original_compiler_dispatch
145
+ ) = _compiler_dispatch
146
+
147
+ def __class_getitem__(cls, key: Any) -> Any:
148
+ # allow generic classes in py3.9+
149
+ return cls
150
+
151
+
152
+ class InternalTraversal(Enum):
153
+ r"""Defines visitor symbols used for internal traversal.
154
+
155
+ The :class:`.InternalTraversal` class is used in two ways. One is that
156
+ it can serve as the superclass for an object that implements the
157
+ various visit methods of the class. The other is that the symbols
158
+ themselves of :class:`.InternalTraversal` are used within
159
+ the ``_traverse_internals`` collection. Such as, the :class:`.Case`
160
+ object defines ``_traverse_internals`` as ::
161
+
162
+ class Case(ColumnElement[_T]):
163
+ _traverse_internals = [
164
+ ("value", InternalTraversal.dp_clauseelement),
165
+ ("whens", InternalTraversal.dp_clauseelement_tuples),
166
+ ("else_", InternalTraversal.dp_clauseelement),
167
+ ]
168
+
169
+ Above, the :class:`.Case` class indicates its internal state as the
170
+ attributes named ``value``, ``whens``, and ``else_``. They each
171
+ link to an :class:`.InternalTraversal` method which indicates the type
172
+ of datastructure to which each attribute refers.
173
+
174
+ Using the ``_traverse_internals`` structure, objects of type
175
+ :class:`.InternalTraversible` will have the following methods automatically
176
+ implemented:
177
+
178
+ * :meth:`.HasTraverseInternals.get_children`
179
+
180
+ * :meth:`.HasTraverseInternals._copy_internals`
181
+
182
+ * :meth:`.HasCacheKey._gen_cache_key`
183
+
184
+ Subclasses can also implement these methods directly, particularly for the
185
+ :meth:`.HasTraverseInternals._copy_internals` method, when special steps
186
+ are needed.
187
+
188
+ .. versionadded:: 1.4
189
+
190
+ """
191
+
192
+ dp_has_cache_key = "HC"
193
+ """Visit a :class:`.HasCacheKey` object."""
194
+
195
+ dp_has_cache_key_list = "HL"
196
+ """Visit a list of :class:`.HasCacheKey` objects."""
197
+
198
+ dp_clauseelement = "CE"
199
+ """Visit a :class:`_expression.ClauseElement` object."""
200
+
201
+ dp_fromclause_canonical_column_collection = "FC"
202
+ """Visit a :class:`_expression.FromClause` object in the context of the
203
+ ``columns`` attribute.
204
+
205
+ The column collection is "canonical", meaning it is the originally
206
+ defined location of the :class:`.ColumnClause` objects. Right now
207
+ this means that the object being visited is a
208
+ :class:`_expression.TableClause`
209
+ or :class:`_schema.Table` object only.
210
+
211
+ """
212
+
213
+ dp_clauseelement_tuples = "CTS"
214
+ """Visit a list of tuples which contain :class:`_expression.ClauseElement`
215
+ objects.
216
+
217
+ """
218
+
219
+ dp_clauseelement_list = "CL"
220
+ """Visit a list of :class:`_expression.ClauseElement` objects.
221
+
222
+ """
223
+
224
+ dp_clauseelement_tuple = "CT"
225
+ """Visit a tuple of :class:`_expression.ClauseElement` objects.
226
+
227
+ """
228
+
229
+ dp_executable_options = "EO"
230
+
231
+ dp_with_context_options = "WC"
232
+
233
+ dp_fromclause_ordered_set = "CO"
234
+ """Visit an ordered set of :class:`_expression.FromClause` objects. """
235
+
236
+ dp_string = "S"
237
+ """Visit a plain string value.
238
+
239
+ Examples include table and column names, bound parameter keys, special
240
+ keywords such as "UNION", "UNION ALL".
241
+
242
+ The string value is considered to be significant for cache key
243
+ generation.
244
+
245
+ """
246
+
247
+ dp_string_list = "SL"
248
+ """Visit a list of strings."""
249
+
250
+ dp_anon_name = "AN"
251
+ """Visit a potentially "anonymized" string value.
252
+
253
+ The string value is considered to be significant for cache key
254
+ generation.
255
+
256
+ """
257
+
258
+ dp_boolean = "B"
259
+ """Visit a boolean value.
260
+
261
+ The boolean value is considered to be significant for cache key
262
+ generation.
263
+
264
+ """
265
+
266
+ dp_operator = "O"
267
+ """Visit an operator.
268
+
269
+ The operator is a function from the :mod:`sqlalchemy.sql.operators`
270
+ module.
271
+
272
+ The operator value is considered to be significant for cache key
273
+ generation.
274
+
275
+ """
276
+
277
+ dp_type = "T"
278
+ """Visit a :class:`.TypeEngine` object
279
+
280
+ The type object is considered to be significant for cache key
281
+ generation.
282
+
283
+ """
284
+
285
+ dp_plain_dict = "PD"
286
+ """Visit a dictionary with string keys.
287
+
288
+ The keys of the dictionary should be strings, the values should
289
+ be immutable and hashable. The dictionary is considered to be
290
+ significant for cache key generation.
291
+
292
+ """
293
+
294
+ dp_dialect_options = "DO"
295
+ """Visit a dialect options structure."""
296
+
297
+ dp_string_clauseelement_dict = "CD"
298
+ """Visit a dictionary of string keys to :class:`_expression.ClauseElement`
299
+ objects.
300
+
301
+ """
302
+
303
+ dp_string_multi_dict = "MD"
304
+ """Visit a dictionary of string keys to values which may either be
305
+ plain immutable/hashable or :class:`.HasCacheKey` objects.
306
+
307
+ """
308
+
309
+ dp_annotations_key = "AK"
310
+ """Visit the _annotations_cache_key element.
311
+
312
+ This is a dictionary of additional information about a ClauseElement
313
+ that modifies its role. It should be included when comparing or caching
314
+ objects, however generating this key is relatively expensive. Visitors
315
+ should check the "_annotations" dict for non-None first before creating
316
+ this key.
317
+
318
+ """
319
+
320
+ dp_plain_obj = "PO"
321
+ """Visit a plain python object.
322
+
323
+ The value should be immutable and hashable, such as an integer.
324
+ The value is considered to be significant for cache key generation.
325
+
326
+ """
327
+
328
+ dp_named_ddl_element = "DD"
329
+ """Visit a simple named DDL element.
330
+
331
+ The current object used by this method is the :class:`.Sequence`.
332
+
333
+ The object is only considered to be important for cache key generation
334
+ as far as its name, but not any other aspects of it.
335
+
336
+ """
337
+
338
+ dp_prefix_sequence = "PS"
339
+ """Visit the sequence represented by :class:`_expression.HasPrefixes`
340
+ or :class:`_expression.HasSuffixes`.
341
+
342
+ """
343
+
344
+ dp_table_hint_list = "TH"
345
+ """Visit the ``_hints`` collection of a :class:`_expression.Select`
346
+ object.
347
+
348
+ """
349
+
350
+ dp_setup_join_tuple = "SJ"
351
+
352
+ dp_memoized_select_entities = "ME"
353
+
354
+ dp_statement_hint_list = "SH"
355
+ """Visit the ``_statement_hints`` collection of a
356
+ :class:`_expression.Select`
357
+ object.
358
+
359
+ """
360
+
361
+ dp_unknown_structure = "UK"
362
+ """Visit an unknown structure.
363
+
364
+ """
365
+
366
+ dp_dml_ordered_values = "DML_OV"
367
+ """Visit the values() ordered tuple list of an
368
+ :class:`_expression.Update` object."""
369
+
370
+ dp_dml_values = "DML_V"
371
+ """Visit the values() dictionary of a :class:`.ValuesBase`
372
+ (e.g. Insert or Update) object.
373
+
374
+ """
375
+
376
+ dp_dml_multi_values = "DML_MV"
377
+ """Visit the values() multi-valued list of dictionaries of an
378
+ :class:`_expression.Insert` object.
379
+
380
+ """
381
+
382
+ dp_propagate_attrs = "PA"
383
+ """Visit the propagate attrs dict. This hardcodes to the particular
384
+ elements we care about right now."""
385
+
386
+ """Symbols that follow are additional symbols that are useful in
387
+ caching applications.
388
+
389
+ Traversals for :class:`_expression.ClauseElement` objects only need to use
390
+ those symbols present in :class:`.InternalTraversal`. However, for
391
+ additional caching use cases within the ORM, symbols dealing with the
392
+ :class:`.HasCacheKey` class are added here.
393
+
394
+ """
395
+
396
+ dp_ignore = "IG"
397
+ """Specify an object that should be ignored entirely.
398
+
399
+ This currently applies function call argument caching where some
400
+ arguments should not be considered to be part of a cache key.
401
+
402
+ """
403
+
404
+ dp_inspectable = "IS"
405
+ """Visit an inspectable object where the return value is a
406
+ :class:`.HasCacheKey` object."""
407
+
408
+ dp_multi = "M"
409
+ """Visit an object that may be a :class:`.HasCacheKey` or may be a
410
+ plain hashable object."""
411
+
412
+ dp_multi_list = "MT"
413
+ """Visit a tuple containing elements that may be :class:`.HasCacheKey` or
414
+ may be a plain hashable object."""
415
+
416
+ dp_has_cache_key_tuples = "HT"
417
+ """Visit a list of tuples which contain :class:`.HasCacheKey`
418
+ objects.
419
+
420
+ """
421
+
422
+ dp_inspectable_list = "IL"
423
+ """Visit a list of inspectable objects which upon inspection are
424
+ HasCacheKey objects."""
425
+
426
+
427
+ _TraverseInternalsType = List[Tuple[str, InternalTraversal]]
428
+ """a structure that defines how a HasTraverseInternals should be
429
+ traversed.
430
+
431
+ This structure consists of a list of (attributename, internaltraversal)
432
+ tuples, where the "attributename" refers to the name of an attribute on an
433
+ instance of the HasTraverseInternals object, and "internaltraversal" refers
434
+ to an :class:`.InternalTraversal` enumeration symbol defining what kind
435
+ of data this attribute stores, which indicates to the traverser how it should
436
+ be handled.
437
+
438
+ """
439
+
440
+
441
+ class HasTraverseInternals:
442
+ """base for classes that have a "traverse internals" element,
443
+ which defines all kinds of ways of traversing the elements of an object.
444
+
445
+ Compared to :class:`.Visitable`, which relies upon an external visitor to
446
+ define how the object is travered (i.e. the :class:`.SQLCompiler`), the
447
+ :class:`.HasTraverseInternals` interface allows classes to define their own
448
+ traversal, that is, what attributes are accessed and in what order.
449
+
450
+ """
451
+
452
+ __slots__ = ()
453
+
454
+ _traverse_internals: _TraverseInternalsType
455
+
456
+ _is_immutable: bool = False
457
+
458
+ @util.preload_module("sqlalchemy.sql.traversals")
459
+ def get_children(
460
+ self, *, omit_attrs: Tuple[str, ...] = (), **kw: Any
461
+ ) -> Iterable[HasTraverseInternals]:
462
+ r"""Return immediate child :class:`.visitors.HasTraverseInternals`
463
+ elements of this :class:`.visitors.HasTraverseInternals`.
464
+
465
+ This is used for visit traversal.
466
+
467
+ \**kw may contain flags that change the collection that is
468
+ returned, for example to return a subset of items in order to
469
+ cut down on larger traversals, or to return child items from a
470
+ different context (such as schema-level collections instead of
471
+ clause-level).
472
+
473
+ """
474
+
475
+ traversals = util.preloaded.sql_traversals
476
+
477
+ try:
478
+ traverse_internals = self._traverse_internals
479
+ except AttributeError:
480
+ # user-defined classes may not have a _traverse_internals
481
+ return []
482
+
483
+ dispatch = traversals._get_children.run_generated_dispatch
484
+ return itertools.chain.from_iterable(
485
+ meth(obj, **kw)
486
+ for attrname, obj, meth in dispatch(
487
+ self, traverse_internals, "_generated_get_children_traversal"
488
+ )
489
+ if attrname not in omit_attrs and obj is not None
490
+ )
491
+
492
+
493
+ class _InternalTraversalDispatchType(Protocol):
494
+ def __call__(s, self: object, visitor: HasTraversalDispatch) -> Any: ...
495
+
496
+
497
+ class HasTraversalDispatch:
498
+ r"""Define infrastructure for classes that perform internal traversals
499
+
500
+ .. versionadded:: 2.0
501
+
502
+ """
503
+
504
+ __slots__ = ()
505
+
506
+ _dispatch_lookup: ClassVar[Dict[Union[InternalTraversal, str], str]] = {}
507
+
508
+ def dispatch(self, visit_symbol: InternalTraversal) -> Callable[..., Any]:
509
+ """Given a method from :class:`.HasTraversalDispatch`, return the
510
+ corresponding method on a subclass.
511
+
512
+ """
513
+ name = _dispatch_lookup[visit_symbol]
514
+ return getattr(self, name, None) # type: ignore
515
+
516
+ def run_generated_dispatch(
517
+ self,
518
+ target: object,
519
+ internal_dispatch: _TraverseInternalsType,
520
+ generate_dispatcher_name: str,
521
+ ) -> Any:
522
+ dispatcher: _InternalTraversalDispatchType
523
+ try:
524
+ dispatcher = target.__class__.__dict__[generate_dispatcher_name]
525
+ except KeyError:
526
+ # traversals.py -> _preconfigure_traversals()
527
+ # may be used to run these ahead of time, but
528
+ # is not enabled right now.
529
+ # this block will generate any remaining dispatchers.
530
+ dispatcher = self.generate_dispatch(
531
+ target.__class__, internal_dispatch, generate_dispatcher_name
532
+ )
533
+ return dispatcher(target, self)
534
+
535
+ def generate_dispatch(
536
+ self,
537
+ target_cls: Type[object],
538
+ internal_dispatch: _TraverseInternalsType,
539
+ generate_dispatcher_name: str,
540
+ ) -> _InternalTraversalDispatchType:
541
+ dispatcher = self._generate_dispatcher(
542
+ internal_dispatch, generate_dispatcher_name
543
+ )
544
+ # assert isinstance(target_cls, type)
545
+ setattr(target_cls, generate_dispatcher_name, dispatcher)
546
+ return dispatcher
547
+
548
+ def _generate_dispatcher(
549
+ self, internal_dispatch: _TraverseInternalsType, method_name: str
550
+ ) -> _InternalTraversalDispatchType:
551
+ names = []
552
+ for attrname, visit_sym in internal_dispatch:
553
+ meth = self.dispatch(visit_sym)
554
+ if meth is not None:
555
+ visit_name = _dispatch_lookup[visit_sym]
556
+ names.append((attrname, visit_name))
557
+
558
+ code = (
559
+ (" return [\n")
560
+ + (
561
+ ", \n".join(
562
+ " (%r, self.%s, visitor.%s)"
563
+ % (attrname, attrname, visit_name)
564
+ for attrname, visit_name in names
565
+ )
566
+ )
567
+ + ("\n ]\n")
568
+ )
569
+ meth_text = ("def %s(self, visitor):\n" % method_name) + code + "\n"
570
+ return cast(
571
+ _InternalTraversalDispatchType,
572
+ langhelpers._exec_code_in_env(meth_text, {}, method_name),
573
+ )
574
+
575
+
576
+ ExtendedInternalTraversal = InternalTraversal
577
+
578
+
579
+ def _generate_traversal_dispatch() -> None:
580
+ lookup = _dispatch_lookup
581
+
582
+ for sym in InternalTraversal:
583
+ key = sym.name
584
+ if key.startswith("dp_"):
585
+ visit_key = key.replace("dp_", "visit_")
586
+ sym_name = sym.value
587
+ assert sym_name not in lookup, sym_name
588
+ lookup[sym] = lookup[sym_name] = visit_key
589
+
590
+
591
+ _dispatch_lookup = HasTraversalDispatch._dispatch_lookup
592
+ _generate_traversal_dispatch()
593
+
594
+
595
+ class ExternallyTraversible(HasTraverseInternals, Visitable):
596
+ __slots__ = ()
597
+
598
+ _annotations: Mapping[Any, Any] = util.EMPTY_DICT
599
+
600
+ if typing.TYPE_CHECKING:
601
+
602
+ def _annotate(self, values: _AnnotationDict) -> Self: ...
603
+
604
+ def get_children(
605
+ self, *, omit_attrs: Tuple[str, ...] = (), **kw: Any
606
+ ) -> Iterable[ExternallyTraversible]: ...
607
+
608
+ def _clone(self, **kw: Any) -> Self:
609
+ """clone this element"""
610
+ raise NotImplementedError()
611
+
612
+ def _copy_internals(
613
+ self, *, omit_attrs: Tuple[str, ...] = (), **kw: Any
614
+ ) -> None:
615
+ """Reassign internal elements to be clones of themselves.
616
+
617
+ Called during a copy-and-traverse operation on newly
618
+ shallow-copied elements to create a deep copy.
619
+
620
+ The given clone function should be used, which may be applying
621
+ additional transformations to the element (i.e. replacement
622
+ traversal, cloned traversal, annotations).
623
+
624
+ """
625
+ raise NotImplementedError()
626
+
627
+
628
+ _ET = TypeVar("_ET", bound=ExternallyTraversible)
629
+
630
+ _CE = TypeVar("_CE", bound="ColumnElement[Any]")
631
+
632
+ _TraverseCallableType = Callable[[_ET], None]
633
+
634
+
635
+ class _CloneCallableType(Protocol):
636
+ def __call__(self, element: _ET, **kw: Any) -> _ET: ...
637
+
638
+
639
+ class _TraverseTransformCallableType(Protocol[_ET]):
640
+ def __call__(self, element: _ET, **kw: Any) -> Optional[_ET]: ...
641
+
642
+
643
+ _ExtT = TypeVar("_ExtT", bound="ExternalTraversal")
644
+
645
+
646
+ class ExternalTraversal(util.MemoizedSlots):
647
+ """Base class for visitor objects which can traverse externally using
648
+ the :func:`.visitors.traverse` function.
649
+
650
+ Direct usage of the :func:`.visitors.traverse` function is usually
651
+ preferred.
652
+
653
+ """
654
+
655
+ __slots__ = ("_visitor_dict", "_next")
656
+
657
+ __traverse_options__: Dict[str, Any] = {}
658
+ _next: Optional[ExternalTraversal]
659
+
660
+ def traverse_single(self, obj: Visitable, **kw: Any) -> Any:
661
+ for v in self.visitor_iterator:
662
+ meth = getattr(v, "visit_%s" % obj.__visit_name__, None)
663
+ if meth:
664
+ return meth(obj, **kw)
665
+
666
+ def iterate(
667
+ self, obj: Optional[ExternallyTraversible]
668
+ ) -> Iterator[ExternallyTraversible]:
669
+ """Traverse the given expression structure, returning an iterator
670
+ of all elements.
671
+
672
+ """
673
+ return iterate(obj, self.__traverse_options__)
674
+
675
+ @overload
676
+ def traverse(self, obj: Literal[None]) -> None: ...
677
+
678
+ @overload
679
+ def traverse(
680
+ self, obj: ExternallyTraversible
681
+ ) -> ExternallyTraversible: ...
682
+
683
+ def traverse(
684
+ self, obj: Optional[ExternallyTraversible]
685
+ ) -> Optional[ExternallyTraversible]:
686
+ """Traverse and visit the given expression structure."""
687
+
688
+ return traverse(obj, self.__traverse_options__, self._visitor_dict)
689
+
690
+ def _memoized_attr__visitor_dict(
691
+ self,
692
+ ) -> Dict[str, _TraverseCallableType[Any]]:
693
+ visitors = {}
694
+
695
+ for name in dir(self):
696
+ if name.startswith("visit_"):
697
+ visitors[name[6:]] = getattr(self, name)
698
+ return visitors
699
+
700
+ @property
701
+ def visitor_iterator(self) -> Iterator[ExternalTraversal]:
702
+ """Iterate through this visitor and each 'chained' visitor."""
703
+
704
+ v: Optional[ExternalTraversal] = self
705
+ while v:
706
+ yield v
707
+ v = getattr(v, "_next", None)
708
+
709
+ def chain(self: _ExtT, visitor: ExternalTraversal) -> _ExtT:
710
+ """'Chain' an additional ExternalTraversal onto this ExternalTraversal
711
+
712
+ The chained visitor will receive all visit events after this one.
713
+
714
+ """
715
+ tail = list(self.visitor_iterator)[-1]
716
+ tail._next = visitor
717
+ return self
718
+
719
+
720
+ class CloningExternalTraversal(ExternalTraversal):
721
+ """Base class for visitor objects which can traverse using
722
+ the :func:`.visitors.cloned_traverse` function.
723
+
724
+ Direct usage of the :func:`.visitors.cloned_traverse` function is usually
725
+ preferred.
726
+
727
+
728
+ """
729
+
730
+ __slots__ = ()
731
+
732
+ def copy_and_process(
733
+ self, list_: List[ExternallyTraversible]
734
+ ) -> List[ExternallyTraversible]:
735
+ """Apply cloned traversal to the given list of elements, and return
736
+ the new list.
737
+
738
+ """
739
+ return [self.traverse(x) for x in list_]
740
+
741
+ @overload
742
+ def traverse(self, obj: Literal[None]) -> None: ...
743
+
744
+ @overload
745
+ def traverse(
746
+ self, obj: ExternallyTraversible
747
+ ) -> ExternallyTraversible: ...
748
+
749
+ def traverse(
750
+ self, obj: Optional[ExternallyTraversible]
751
+ ) -> Optional[ExternallyTraversible]:
752
+ """Traverse and visit the given expression structure."""
753
+
754
+ return cloned_traverse(
755
+ obj, self.__traverse_options__, self._visitor_dict
756
+ )
757
+
758
+
759
+ class ReplacingExternalTraversal(CloningExternalTraversal):
760
+ """Base class for visitor objects which can traverse using
761
+ the :func:`.visitors.replacement_traverse` function.
762
+
763
+ Direct usage of the :func:`.visitors.replacement_traverse` function is
764
+ usually preferred.
765
+
766
+ """
767
+
768
+ __slots__ = ()
769
+
770
+ def replace(
771
+ self, elem: ExternallyTraversible
772
+ ) -> Optional[ExternallyTraversible]:
773
+ """Receive pre-copied elements during a cloning traversal.
774
+
775
+ If the method returns a new element, the element is used
776
+ instead of creating a simple copy of the element. Traversal
777
+ will halt on the newly returned element if it is re-encountered.
778
+ """
779
+ return None
780
+
781
+ @overload
782
+ def traverse(self, obj: Literal[None]) -> None: ...
783
+
784
+ @overload
785
+ def traverse(
786
+ self, obj: ExternallyTraversible
787
+ ) -> ExternallyTraversible: ...
788
+
789
+ def traverse(
790
+ self, obj: Optional[ExternallyTraversible]
791
+ ) -> Optional[ExternallyTraversible]:
792
+ """Traverse and visit the given expression structure."""
793
+
794
+ def replace(
795
+ element: ExternallyTraversible,
796
+ **kw: Any,
797
+ ) -> Optional[ExternallyTraversible]:
798
+ for v in self.visitor_iterator:
799
+ e = cast(ReplacingExternalTraversal, v).replace(element)
800
+ if e is not None:
801
+ return e
802
+
803
+ return None
804
+
805
+ return replacement_traverse(obj, self.__traverse_options__, replace)
806
+
807
+
808
+ # backwards compatibility
809
+ Traversible = Visitable
810
+
811
+ ClauseVisitor = ExternalTraversal
812
+ CloningVisitor = CloningExternalTraversal
813
+ ReplacingCloningVisitor = ReplacingExternalTraversal
814
+
815
+
816
+ def iterate(
817
+ obj: Optional[ExternallyTraversible],
818
+ opts: Mapping[str, Any] = util.EMPTY_DICT,
819
+ ) -> Iterator[ExternallyTraversible]:
820
+ r"""Traverse the given expression structure, returning an iterator.
821
+
822
+ Traversal is configured to be breadth-first.
823
+
824
+ The central API feature used by the :func:`.visitors.iterate`
825
+ function is the
826
+ :meth:`_expression.ClauseElement.get_children` method of
827
+ :class:`_expression.ClauseElement` objects. This method should return all
828
+ the :class:`_expression.ClauseElement` objects which are associated with a
829
+ particular :class:`_expression.ClauseElement` object. For example, a
830
+ :class:`.Case` structure will refer to a series of
831
+ :class:`_expression.ColumnElement` objects within its "whens" and "else\_"
832
+ member variables.
833
+
834
+ :param obj: :class:`_expression.ClauseElement` structure to be traversed
835
+
836
+ :param opts: dictionary of iteration options. This dictionary is usually
837
+ empty in modern usage.
838
+
839
+ """
840
+ if obj is None:
841
+ return
842
+
843
+ yield obj
844
+ children = obj.get_children(**opts)
845
+
846
+ if not children:
847
+ return
848
+
849
+ stack = deque([children])
850
+ while stack:
851
+ t_iterator = stack.popleft()
852
+ for t in t_iterator:
853
+ yield t
854
+ stack.append(t.get_children(**opts))
855
+
856
+
857
+ @overload
858
+ def traverse_using(
859
+ iterator: Iterable[ExternallyTraversible],
860
+ obj: Literal[None],
861
+ visitors: Mapping[str, _TraverseCallableType[Any]],
862
+ ) -> None: ...
863
+
864
+
865
+ @overload
866
+ def traverse_using(
867
+ iterator: Iterable[ExternallyTraversible],
868
+ obj: ExternallyTraversible,
869
+ visitors: Mapping[str, _TraverseCallableType[Any]],
870
+ ) -> ExternallyTraversible: ...
871
+
872
+
873
+ def traverse_using(
874
+ iterator: Iterable[ExternallyTraversible],
875
+ obj: Optional[ExternallyTraversible],
876
+ visitors: Mapping[str, _TraverseCallableType[Any]],
877
+ ) -> Optional[ExternallyTraversible]:
878
+ """Visit the given expression structure using the given iterator of
879
+ objects.
880
+
881
+ :func:`.visitors.traverse_using` is usually called internally as the result
882
+ of the :func:`.visitors.traverse` function.
883
+
884
+ :param iterator: an iterable or sequence which will yield
885
+ :class:`_expression.ClauseElement`
886
+ structures; the iterator is assumed to be the
887
+ product of the :func:`.visitors.iterate` function.
888
+
889
+ :param obj: the :class:`_expression.ClauseElement`
890
+ that was used as the target of the
891
+ :func:`.iterate` function.
892
+
893
+ :param visitors: dictionary of visit functions. See :func:`.traverse`
894
+ for details on this dictionary.
895
+
896
+ .. seealso::
897
+
898
+ :func:`.traverse`
899
+
900
+
901
+ """
902
+ for target in iterator:
903
+ meth = visitors.get(target.__visit_name__, None)
904
+ if meth:
905
+ meth(target)
906
+ return obj
907
+
908
+
909
+ @overload
910
+ def traverse(
911
+ obj: Literal[None],
912
+ opts: Mapping[str, Any],
913
+ visitors: Mapping[str, _TraverseCallableType[Any]],
914
+ ) -> None: ...
915
+
916
+
917
+ @overload
918
+ def traverse(
919
+ obj: ExternallyTraversible,
920
+ opts: Mapping[str, Any],
921
+ visitors: Mapping[str, _TraverseCallableType[Any]],
922
+ ) -> ExternallyTraversible: ...
923
+
924
+
925
+ def traverse(
926
+ obj: Optional[ExternallyTraversible],
927
+ opts: Mapping[str, Any],
928
+ visitors: Mapping[str, _TraverseCallableType[Any]],
929
+ ) -> Optional[ExternallyTraversible]:
930
+ """Traverse and visit the given expression structure using the default
931
+ iterator.
932
+
933
+ e.g.::
934
+
935
+ from sqlalchemy.sql import visitors
936
+
937
+ stmt = select(some_table).where(some_table.c.foo == 'bar')
938
+
939
+ def visit_bindparam(bind_param):
940
+ print("found bound value: %s" % bind_param.value)
941
+
942
+ visitors.traverse(stmt, {}, {"bindparam": visit_bindparam})
943
+
944
+ The iteration of objects uses the :func:`.visitors.iterate` function,
945
+ which does a breadth-first traversal using a stack.
946
+
947
+ :param obj: :class:`_expression.ClauseElement` structure to be traversed
948
+
949
+ :param opts: dictionary of iteration options. This dictionary is usually
950
+ empty in modern usage.
951
+
952
+ :param visitors: dictionary of visit functions. The dictionary should
953
+ have strings as keys, each of which would correspond to the
954
+ ``__visit_name__`` of a particular kind of SQL expression object, and
955
+ callable functions as values, each of which represents a visitor function
956
+ for that kind of object.
957
+
958
+ """
959
+ return traverse_using(iterate(obj, opts), obj, visitors)
960
+
961
+
962
+ @overload
963
+ def cloned_traverse(
964
+ obj: Literal[None],
965
+ opts: Mapping[str, Any],
966
+ visitors: Mapping[str, _TraverseCallableType[Any]],
967
+ ) -> None: ...
968
+
969
+
970
+ # a bit of controversy here, as the clone of the lead element
971
+ # *could* in theory replace with an entirely different kind of element.
972
+ # however this is really not how cloned_traverse is ever used internally
973
+ # at least.
974
+ @overload
975
+ def cloned_traverse(
976
+ obj: _ET,
977
+ opts: Mapping[str, Any],
978
+ visitors: Mapping[str, _TraverseCallableType[Any]],
979
+ ) -> _ET: ...
980
+
981
+
982
+ def cloned_traverse(
983
+ obj: Optional[ExternallyTraversible],
984
+ opts: Mapping[str, Any],
985
+ visitors: Mapping[str, _TraverseCallableType[Any]],
986
+ ) -> Optional[ExternallyTraversible]:
987
+ """Clone the given expression structure, allowing modifications by
988
+ visitors for mutable objects.
989
+
990
+ Traversal usage is the same as that of :func:`.visitors.traverse`.
991
+ The visitor functions present in the ``visitors`` dictionary may also
992
+ modify the internals of the given structure as the traversal proceeds.
993
+
994
+ The :func:`.cloned_traverse` function does **not** provide objects that are
995
+ part of the :class:`.Immutable` interface to the visit methods (this
996
+ primarily includes :class:`.ColumnClause`, :class:`.Column`,
997
+ :class:`.TableClause` and :class:`.Table` objects). As this traversal is
998
+ only intended to allow in-place mutation of objects, :class:`.Immutable`
999
+ objects are skipped. The :meth:`.Immutable._clone` method is still called
1000
+ on each object to allow for objects to replace themselves with a different
1001
+ object based on a clone of their sub-internals (e.g. a
1002
+ :class:`.ColumnClause` that clones its subquery to return a new
1003
+ :class:`.ColumnClause`).
1004
+
1005
+ .. versionchanged:: 2.0 The :func:`.cloned_traverse` function omits
1006
+ objects that are part of the :class:`.Immutable` interface.
1007
+
1008
+ The central API feature used by the :func:`.visitors.cloned_traverse`
1009
+ and :func:`.visitors.replacement_traverse` functions, in addition to the
1010
+ :meth:`_expression.ClauseElement.get_children`
1011
+ function that is used to achieve
1012
+ the iteration, is the :meth:`_expression.ClauseElement._copy_internals`
1013
+ method.
1014
+ For a :class:`_expression.ClauseElement`
1015
+ structure to support cloning and replacement
1016
+ traversals correctly, it needs to be able to pass a cloning function into
1017
+ its internal members in order to make copies of them.
1018
+
1019
+ .. seealso::
1020
+
1021
+ :func:`.visitors.traverse`
1022
+
1023
+ :func:`.visitors.replacement_traverse`
1024
+
1025
+ """
1026
+
1027
+ cloned: Dict[int, ExternallyTraversible] = {}
1028
+ stop_on = set(opts.get("stop_on", []))
1029
+
1030
+ def deferred_copy_internals(
1031
+ obj: ExternallyTraversible,
1032
+ ) -> ExternallyTraversible:
1033
+ return cloned_traverse(obj, opts, visitors)
1034
+
1035
+ def clone(elem: ExternallyTraversible, **kw: Any) -> ExternallyTraversible:
1036
+ if elem in stop_on:
1037
+ return elem
1038
+ else:
1039
+ if id(elem) not in cloned:
1040
+ if "replace" in kw:
1041
+ newelem = cast(
1042
+ Optional[ExternallyTraversible], kw["replace"](elem)
1043
+ )
1044
+ if newelem is not None:
1045
+ cloned[id(elem)] = newelem
1046
+ return newelem
1047
+
1048
+ # the _clone method for immutable normally returns "self".
1049
+ # however, the method is still allowed to return a
1050
+ # different object altogether; ColumnClause._clone() will
1051
+ # based on options clone the subquery to which it is associated
1052
+ # and return the new corresponding column.
1053
+ cloned[id(elem)] = newelem = elem._clone(clone=clone, **kw)
1054
+ newelem._copy_internals(clone=clone, **kw)
1055
+
1056
+ # however, visit methods which are tasked with in-place
1057
+ # mutation of the object should not get access to the immutable
1058
+ # object.
1059
+ if not elem._is_immutable:
1060
+ meth = visitors.get(newelem.__visit_name__, None)
1061
+ if meth:
1062
+ meth(newelem)
1063
+ return cloned[id(elem)]
1064
+
1065
+ if obj is not None:
1066
+ obj = clone(
1067
+ obj, deferred_copy_internals=deferred_copy_internals, **opts
1068
+ )
1069
+ clone = None # type: ignore[assignment] # remove gc cycles
1070
+ return obj
1071
+
1072
+
1073
+ @overload
1074
+ def replacement_traverse(
1075
+ obj: Literal[None],
1076
+ opts: Mapping[str, Any],
1077
+ replace: _TraverseTransformCallableType[Any],
1078
+ ) -> None: ...
1079
+
1080
+
1081
+ @overload
1082
+ def replacement_traverse(
1083
+ obj: _CE,
1084
+ opts: Mapping[str, Any],
1085
+ replace: _TraverseTransformCallableType[Any],
1086
+ ) -> _CE: ...
1087
+
1088
+
1089
+ @overload
1090
+ def replacement_traverse(
1091
+ obj: ExternallyTraversible,
1092
+ opts: Mapping[str, Any],
1093
+ replace: _TraverseTransformCallableType[Any],
1094
+ ) -> ExternallyTraversible: ...
1095
+
1096
+
1097
+ def replacement_traverse(
1098
+ obj: Optional[ExternallyTraversible],
1099
+ opts: Mapping[str, Any],
1100
+ replace: _TraverseTransformCallableType[Any],
1101
+ ) -> Optional[ExternallyTraversible]:
1102
+ """Clone the given expression structure, allowing element
1103
+ replacement by a given replacement function.
1104
+
1105
+ This function is very similar to the :func:`.visitors.cloned_traverse`
1106
+ function, except instead of being passed a dictionary of visitors, all
1107
+ elements are unconditionally passed into the given replace function.
1108
+ The replace function then has the option to return an entirely new object
1109
+ which will replace the one given. If it returns ``None``, then the object
1110
+ is kept in place.
1111
+
1112
+ The difference in usage between :func:`.visitors.cloned_traverse` and
1113
+ :func:`.visitors.replacement_traverse` is that in the former case, an
1114
+ already-cloned object is passed to the visitor function, and the visitor
1115
+ function can then manipulate the internal state of the object.
1116
+ In the case of the latter, the visitor function should only return an
1117
+ entirely different object, or do nothing.
1118
+
1119
+ The use case for :func:`.visitors.replacement_traverse` is that of
1120
+ replacing a FROM clause inside of a SQL structure with a different one,
1121
+ as is a common use case within the ORM.
1122
+
1123
+ """
1124
+
1125
+ cloned = {}
1126
+ stop_on = {id(x) for x in opts.get("stop_on", [])}
1127
+
1128
+ def deferred_copy_internals(
1129
+ obj: ExternallyTraversible,
1130
+ ) -> ExternallyTraversible:
1131
+ return replacement_traverse(obj, opts, replace)
1132
+
1133
+ def clone(elem: ExternallyTraversible, **kw: Any) -> ExternallyTraversible:
1134
+ if (
1135
+ id(elem) in stop_on
1136
+ or "no_replacement_traverse" in elem._annotations
1137
+ ):
1138
+ return elem
1139
+ else:
1140
+ newelem = replace(elem)
1141
+ if newelem is not None:
1142
+ stop_on.add(id(newelem))
1143
+ return newelem # type: ignore
1144
+ else:
1145
+ # base "already seen" on id(), not hash, so that we don't
1146
+ # replace an Annotated element with its non-annotated one, and
1147
+ # vice versa
1148
+ id_elem = id(elem)
1149
+ if id_elem not in cloned:
1150
+ if "replace" in kw:
1151
+ newelem = kw["replace"](elem)
1152
+ if newelem is not None:
1153
+ cloned[id_elem] = newelem
1154
+ return newelem # type: ignore
1155
+
1156
+ cloned[id_elem] = newelem = elem._clone(**kw)
1157
+ newelem._copy_internals(clone=clone, **kw)
1158
+ return cloned[id_elem] # type: ignore
1159
+
1160
+ if obj is not None:
1161
+ obj = clone(
1162
+ obj, deferred_copy_internals=deferred_copy_internals, **opts
1163
+ )
1164
+ clone = None # type: ignore[assignment] # remove gc cycles
1165
+ return obj