SQLAlchemy 2.1.0b2__cp313-cp313t-win_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. sqlalchemy/__init__.py +298 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +171 -0
  4. sqlalchemy/connectors/asyncio.py +476 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/dialects/__init__.py +62 -0
  7. sqlalchemy/dialects/_typing.py +30 -0
  8. sqlalchemy/dialects/mssql/__init__.py +89 -0
  9. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  10. sqlalchemy/dialects/mssql/base.py +4166 -0
  11. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  12. sqlalchemy/dialects/mssql/json.py +140 -0
  13. sqlalchemy/dialects/mssql/mssqlpython.py +220 -0
  14. sqlalchemy/dialects/mssql/provision.py +196 -0
  15. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  16. sqlalchemy/dialects/mssql/pyodbc.py +698 -0
  17. sqlalchemy/dialects/mysql/__init__.py +106 -0
  18. sqlalchemy/dialects/mysql/_mariadb_shim.py +312 -0
  19. sqlalchemy/dialects/mysql/aiomysql.py +226 -0
  20. sqlalchemy/dialects/mysql/asyncmy.py +214 -0
  21. sqlalchemy/dialects/mysql/base.py +3877 -0
  22. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  23. sqlalchemy/dialects/mysql/dml.py +279 -0
  24. sqlalchemy/dialects/mysql/enumerated.py +277 -0
  25. sqlalchemy/dialects/mysql/expression.py +146 -0
  26. sqlalchemy/dialects/mysql/json.py +92 -0
  27. sqlalchemy/dialects/mysql/mariadb.py +67 -0
  28. sqlalchemy/dialects/mysql/mariadbconnector.py +330 -0
  29. sqlalchemy/dialects/mysql/mysqlconnector.py +296 -0
  30. sqlalchemy/dialects/mysql/mysqldb.py +312 -0
  31. sqlalchemy/dialects/mysql/provision.py +153 -0
  32. sqlalchemy/dialects/mysql/pymysql.py +157 -0
  33. sqlalchemy/dialects/mysql/pyodbc.py +156 -0
  34. sqlalchemy/dialects/mysql/reflection.py +724 -0
  35. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  36. sqlalchemy/dialects/mysql/types.py +845 -0
  37. sqlalchemy/dialects/oracle/__init__.py +85 -0
  38. sqlalchemy/dialects/oracle/base.py +3977 -0
  39. sqlalchemy/dialects/oracle/cx_oracle.py +1601 -0
  40. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  41. sqlalchemy/dialects/oracle/json.py +158 -0
  42. sqlalchemy/dialects/oracle/oracledb.py +909 -0
  43. sqlalchemy/dialects/oracle/provision.py +288 -0
  44. sqlalchemy/dialects/oracle/types.py +367 -0
  45. sqlalchemy/dialects/oracle/vector.py +368 -0
  46. sqlalchemy/dialects/postgresql/__init__.py +171 -0
  47. sqlalchemy/dialects/postgresql/_psycopg_common.py +229 -0
  48. sqlalchemy/dialects/postgresql/array.py +534 -0
  49. sqlalchemy/dialects/postgresql/asyncpg.py +1323 -0
  50. sqlalchemy/dialects/postgresql/base.py +5789 -0
  51. sqlalchemy/dialects/postgresql/bitstring.py +327 -0
  52. sqlalchemy/dialects/postgresql/dml.py +360 -0
  53. sqlalchemy/dialects/postgresql/ext.py +593 -0
  54. sqlalchemy/dialects/postgresql/hstore.py +423 -0
  55. sqlalchemy/dialects/postgresql/json.py +408 -0
  56. sqlalchemy/dialects/postgresql/named_types.py +521 -0
  57. sqlalchemy/dialects/postgresql/operators.py +130 -0
  58. sqlalchemy/dialects/postgresql/pg8000.py +670 -0
  59. sqlalchemy/dialects/postgresql/pg_catalog.py +344 -0
  60. sqlalchemy/dialects/postgresql/provision.py +184 -0
  61. sqlalchemy/dialects/postgresql/psycopg.py +799 -0
  62. sqlalchemy/dialects/postgresql/psycopg2.py +860 -0
  63. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  64. sqlalchemy/dialects/postgresql/ranges.py +1002 -0
  65. sqlalchemy/dialects/postgresql/types.py +388 -0
  66. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  67. sqlalchemy/dialects/sqlite/aiosqlite.py +321 -0
  68. sqlalchemy/dialects/sqlite/base.py +3063 -0
  69. sqlalchemy/dialects/sqlite/dml.py +279 -0
  70. sqlalchemy/dialects/sqlite/json.py +100 -0
  71. sqlalchemy/dialects/sqlite/provision.py +229 -0
  72. sqlalchemy/dialects/sqlite/pysqlcipher.py +161 -0
  73. sqlalchemy/dialects/sqlite/pysqlite.py +754 -0
  74. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  75. sqlalchemy/engine/__init__.py +62 -0
  76. sqlalchemy/engine/_processors_cy.cp313t-win_arm64.pyd +0 -0
  77. sqlalchemy/engine/_processors_cy.py +92 -0
  78. sqlalchemy/engine/_result_cy.cp313t-win_arm64.pyd +0 -0
  79. sqlalchemy/engine/_result_cy.py +633 -0
  80. sqlalchemy/engine/_row_cy.cp313t-win_arm64.pyd +0 -0
  81. sqlalchemy/engine/_row_cy.py +232 -0
  82. sqlalchemy/engine/_util_cy.cp313t-win_arm64.pyd +0 -0
  83. sqlalchemy/engine/_util_cy.py +136 -0
  84. sqlalchemy/engine/base.py +3354 -0
  85. sqlalchemy/engine/characteristics.py +155 -0
  86. sqlalchemy/engine/create.py +877 -0
  87. sqlalchemy/engine/cursor.py +2421 -0
  88. sqlalchemy/engine/default.py +2402 -0
  89. sqlalchemy/engine/events.py +965 -0
  90. sqlalchemy/engine/interfaces.py +3495 -0
  91. sqlalchemy/engine/mock.py +134 -0
  92. sqlalchemy/engine/processors.py +82 -0
  93. sqlalchemy/engine/reflection.py +2100 -0
  94. sqlalchemy/engine/result.py +1966 -0
  95. sqlalchemy/engine/row.py +397 -0
  96. sqlalchemy/engine/strategies.py +16 -0
  97. sqlalchemy/engine/url.py +922 -0
  98. sqlalchemy/engine/util.py +156 -0
  99. sqlalchemy/event/__init__.py +26 -0
  100. sqlalchemy/event/api.py +220 -0
  101. sqlalchemy/event/attr.py +674 -0
  102. sqlalchemy/event/base.py +472 -0
  103. sqlalchemy/event/legacy.py +258 -0
  104. sqlalchemy/event/registry.py +390 -0
  105. sqlalchemy/events.py +17 -0
  106. sqlalchemy/exc.py +922 -0
  107. sqlalchemy/ext/__init__.py +11 -0
  108. sqlalchemy/ext/associationproxy.py +2072 -0
  109. sqlalchemy/ext/asyncio/__init__.py +29 -0
  110. sqlalchemy/ext/asyncio/base.py +281 -0
  111. sqlalchemy/ext/asyncio/engine.py +1487 -0
  112. sqlalchemy/ext/asyncio/exc.py +21 -0
  113. sqlalchemy/ext/asyncio/result.py +994 -0
  114. sqlalchemy/ext/asyncio/scoping.py +1679 -0
  115. sqlalchemy/ext/asyncio/session.py +2007 -0
  116. sqlalchemy/ext/automap.py +1701 -0
  117. sqlalchemy/ext/baked.py +559 -0
  118. sqlalchemy/ext/compiler.py +600 -0
  119. sqlalchemy/ext/declarative/__init__.py +65 -0
  120. sqlalchemy/ext/declarative/extensions.py +560 -0
  121. sqlalchemy/ext/horizontal_shard.py +481 -0
  122. sqlalchemy/ext/hybrid.py +1877 -0
  123. sqlalchemy/ext/indexable.py +364 -0
  124. sqlalchemy/ext/instrumentation.py +450 -0
  125. sqlalchemy/ext/mutable.py +1081 -0
  126. sqlalchemy/ext/orderinglist.py +439 -0
  127. sqlalchemy/ext/serializer.py +185 -0
  128. sqlalchemy/future/__init__.py +16 -0
  129. sqlalchemy/future/engine.py +15 -0
  130. sqlalchemy/inspection.py +174 -0
  131. sqlalchemy/log.py +283 -0
  132. sqlalchemy/orm/__init__.py +176 -0
  133. sqlalchemy/orm/_orm_constructors.py +2694 -0
  134. sqlalchemy/orm/_typing.py +179 -0
  135. sqlalchemy/orm/attributes.py +2868 -0
  136. sqlalchemy/orm/base.py +976 -0
  137. sqlalchemy/orm/bulk_persistence.py +2152 -0
  138. sqlalchemy/orm/clsregistry.py +582 -0
  139. sqlalchemy/orm/collections.py +1568 -0
  140. sqlalchemy/orm/context.py +3471 -0
  141. sqlalchemy/orm/decl_api.py +2280 -0
  142. sqlalchemy/orm/decl_base.py +2309 -0
  143. sqlalchemy/orm/dependency.py +1306 -0
  144. sqlalchemy/orm/descriptor_props.py +1183 -0
  145. sqlalchemy/orm/dynamic.py +307 -0
  146. sqlalchemy/orm/evaluator.py +379 -0
  147. sqlalchemy/orm/events.py +3386 -0
  148. sqlalchemy/orm/exc.py +237 -0
  149. sqlalchemy/orm/identity.py +302 -0
  150. sqlalchemy/orm/instrumentation.py +746 -0
  151. sqlalchemy/orm/interfaces.py +1589 -0
  152. sqlalchemy/orm/loading.py +1684 -0
  153. sqlalchemy/orm/mapped_collection.py +557 -0
  154. sqlalchemy/orm/mapper.py +4411 -0
  155. sqlalchemy/orm/path_registry.py +829 -0
  156. sqlalchemy/orm/persistence.py +1789 -0
  157. sqlalchemy/orm/properties.py +973 -0
  158. sqlalchemy/orm/query.py +3528 -0
  159. sqlalchemy/orm/relationships.py +3570 -0
  160. sqlalchemy/orm/scoping.py +2232 -0
  161. sqlalchemy/orm/session.py +5403 -0
  162. sqlalchemy/orm/state.py +1175 -0
  163. sqlalchemy/orm/state_changes.py +196 -0
  164. sqlalchemy/orm/strategies.py +3492 -0
  165. sqlalchemy/orm/strategy_options.py +2562 -0
  166. sqlalchemy/orm/sync.py +164 -0
  167. sqlalchemy/orm/unitofwork.py +798 -0
  168. sqlalchemy/orm/util.py +2438 -0
  169. sqlalchemy/orm/writeonly.py +694 -0
  170. sqlalchemy/pool/__init__.py +41 -0
  171. sqlalchemy/pool/base.py +1522 -0
  172. sqlalchemy/pool/events.py +375 -0
  173. sqlalchemy/pool/impl.py +582 -0
  174. sqlalchemy/py.typed +0 -0
  175. sqlalchemy/schema.py +74 -0
  176. sqlalchemy/sql/__init__.py +156 -0
  177. sqlalchemy/sql/_annotated_cols.py +397 -0
  178. sqlalchemy/sql/_dml_constructors.py +132 -0
  179. sqlalchemy/sql/_elements_constructors.py +2164 -0
  180. sqlalchemy/sql/_orm_types.py +20 -0
  181. sqlalchemy/sql/_selectable_constructors.py +840 -0
  182. sqlalchemy/sql/_typing.py +487 -0
  183. sqlalchemy/sql/_util_cy.cp313t-win_arm64.pyd +0 -0
  184. sqlalchemy/sql/_util_cy.py +127 -0
  185. sqlalchemy/sql/annotation.py +590 -0
  186. sqlalchemy/sql/base.py +2699 -0
  187. sqlalchemy/sql/cache_key.py +1066 -0
  188. sqlalchemy/sql/coercions.py +1373 -0
  189. sqlalchemy/sql/compiler.py +8327 -0
  190. sqlalchemy/sql/crud.py +1815 -0
  191. sqlalchemy/sql/ddl.py +1928 -0
  192. sqlalchemy/sql/default_comparator.py +654 -0
  193. sqlalchemy/sql/dml.py +1977 -0
  194. sqlalchemy/sql/elements.py +6033 -0
  195. sqlalchemy/sql/events.py +458 -0
  196. sqlalchemy/sql/expression.py +172 -0
  197. sqlalchemy/sql/functions.py +2305 -0
  198. sqlalchemy/sql/lambdas.py +1443 -0
  199. sqlalchemy/sql/naming.py +209 -0
  200. sqlalchemy/sql/operators.py +2897 -0
  201. sqlalchemy/sql/roles.py +332 -0
  202. sqlalchemy/sql/schema.py +6703 -0
  203. sqlalchemy/sql/selectable.py +7553 -0
  204. sqlalchemy/sql/sqltypes.py +4093 -0
  205. sqlalchemy/sql/traversals.py +1042 -0
  206. sqlalchemy/sql/type_api.py +2446 -0
  207. sqlalchemy/sql/util.py +1495 -0
  208. sqlalchemy/sql/visitors.py +1157 -0
  209. sqlalchemy/testing/__init__.py +96 -0
  210. sqlalchemy/testing/assertions.py +1007 -0
  211. sqlalchemy/testing/assertsql.py +519 -0
  212. sqlalchemy/testing/asyncio.py +128 -0
  213. sqlalchemy/testing/config.py +440 -0
  214. sqlalchemy/testing/engines.py +483 -0
  215. sqlalchemy/testing/entities.py +117 -0
  216. sqlalchemy/testing/exclusions.py +476 -0
  217. sqlalchemy/testing/fixtures/__init__.py +30 -0
  218. sqlalchemy/testing/fixtures/base.py +384 -0
  219. sqlalchemy/testing/fixtures/mypy.py +247 -0
  220. sqlalchemy/testing/fixtures/orm.py +227 -0
  221. sqlalchemy/testing/fixtures/sql.py +538 -0
  222. sqlalchemy/testing/pickleable.py +155 -0
  223. sqlalchemy/testing/plugin/__init__.py +6 -0
  224. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  225. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  226. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  227. sqlalchemy/testing/profiling.py +329 -0
  228. sqlalchemy/testing/provision.py +613 -0
  229. sqlalchemy/testing/requirements.py +1978 -0
  230. sqlalchemy/testing/schema.py +198 -0
  231. sqlalchemy/testing/suite/__init__.py +19 -0
  232. sqlalchemy/testing/suite/test_cte.py +237 -0
  233. sqlalchemy/testing/suite/test_ddl.py +420 -0
  234. sqlalchemy/testing/suite/test_dialect.py +776 -0
  235. sqlalchemy/testing/suite/test_insert.py +630 -0
  236. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  237. sqlalchemy/testing/suite/test_results.py +660 -0
  238. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  239. sqlalchemy/testing/suite/test_select.py +2112 -0
  240. sqlalchemy/testing/suite/test_sequence.py +317 -0
  241. sqlalchemy/testing/suite/test_table_via_select.py +686 -0
  242. sqlalchemy/testing/suite/test_types.py +2271 -0
  243. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  244. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  245. sqlalchemy/testing/util.py +535 -0
  246. sqlalchemy/testing/warnings.py +52 -0
  247. sqlalchemy/types.py +76 -0
  248. sqlalchemy/util/__init__.py +158 -0
  249. sqlalchemy/util/_collections.py +688 -0
  250. sqlalchemy/util/_collections_cy.cp313t-win_arm64.pyd +0 -0
  251. sqlalchemy/util/_collections_cy.pxd +8 -0
  252. sqlalchemy/util/_collections_cy.py +516 -0
  253. sqlalchemy/util/_has_cython.py +46 -0
  254. sqlalchemy/util/_immutabledict_cy.cp313t-win_arm64.pyd +0 -0
  255. sqlalchemy/util/_immutabledict_cy.py +240 -0
  256. sqlalchemy/util/compat.py +299 -0
  257. sqlalchemy/util/concurrency.py +322 -0
  258. sqlalchemy/util/cython.py +79 -0
  259. sqlalchemy/util/deprecations.py +401 -0
  260. sqlalchemy/util/langhelpers.py +2320 -0
  261. sqlalchemy/util/preloaded.py +152 -0
  262. sqlalchemy/util/queue.py +304 -0
  263. sqlalchemy/util/tool_support.py +201 -0
  264. sqlalchemy/util/topological.py +120 -0
  265. sqlalchemy/util/typing.py +711 -0
  266. sqlalchemy-2.1.0b2.dist-info/METADATA +269 -0
  267. sqlalchemy-2.1.0b2.dist-info/RECORD +270 -0
  268. sqlalchemy-2.1.0b2.dist-info/WHEEL +5 -0
  269. sqlalchemy-2.1.0b2.dist-info/licenses/LICENSE +19 -0
  270. sqlalchemy-2.1.0b2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,688 @@
1
+ # util/_collections.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
+ """Collection classes and helpers."""
10
+ from __future__ import annotations
11
+
12
+ import operator
13
+ import threading
14
+ import types
15
+ import typing
16
+ from typing import Any
17
+ from typing import Callable
18
+ from typing import cast
19
+ from typing import Container
20
+ from typing import Dict
21
+ from typing import FrozenSet
22
+ from typing import Generic
23
+ from typing import Iterable
24
+ from typing import Iterator
25
+ from typing import List
26
+ from typing import Literal
27
+ from typing import Mapping
28
+ from typing import NoReturn
29
+ from typing import Optional
30
+ from typing import overload
31
+ from typing import Protocol
32
+ from typing import Sequence
33
+ from typing import Set
34
+ from typing import Tuple
35
+ from typing import TypeVar
36
+ from typing import Union
37
+ from typing import ValuesView
38
+ import weakref
39
+
40
+ from ._collections_cy import IdentitySet as IdentitySet
41
+ from ._collections_cy import OrderedSet as OrderedSet
42
+ from ._collections_cy import unique_list as unique_list # noqa: F401
43
+ from ._immutabledict_cy import immutabledict as immutabledict
44
+ from ._immutabledict_cy import ImmutableDictBase as ImmutableDictBase
45
+ from ._immutabledict_cy import ReadOnlyContainer as ReadOnlyContainer
46
+ from .typing import is_non_string_iterable
47
+
48
+
49
+ _T = TypeVar("_T", bound=Any)
50
+ _KT = TypeVar("_KT", bound=Any)
51
+ _VT = TypeVar("_VT", bound=Any)
52
+ _T_co = TypeVar("_T_co", covariant=True)
53
+
54
+ EMPTY_SET: FrozenSet[Any] = frozenset()
55
+ NONE_SET: FrozenSet[Any] = frozenset([None])
56
+
57
+
58
+ def merge_lists_w_ordering(a: List[Any], b: List[Any]) -> List[Any]:
59
+ """merge two lists, maintaining ordering as much as possible.
60
+
61
+ this is to reconcile vars(cls) with cls.__annotations__.
62
+
63
+ Example::
64
+
65
+ >>> a = ["__tablename__", "id", "x", "created_at"]
66
+ >>> b = ["id", "name", "data", "y", "created_at"]
67
+ >>> merge_lists_w_ordering(a, b)
68
+ ['__tablename__', 'id', 'name', 'data', 'y', 'x', 'created_at']
69
+
70
+ This is not necessarily the ordering that things had on the class,
71
+ in this case the class is::
72
+
73
+ class User(Base):
74
+ __tablename__ = "users"
75
+
76
+ id: Mapped[int] = mapped_column(primary_key=True)
77
+ name: Mapped[str]
78
+ data: Mapped[Optional[str]]
79
+ x = Column(Integer)
80
+ y: Mapped[int]
81
+ created_at: Mapped[datetime.datetime] = mapped_column()
82
+
83
+ But things are *mostly* ordered.
84
+
85
+ The algorithm could also be done by creating a partial ordering for
86
+ all items in both lists and then using topological_sort(), but that
87
+ is too much overhead.
88
+
89
+ Background on how I came up with this is at:
90
+ https://gist.github.com/zzzeek/89de958cf0803d148e74861bd682ebae
91
+
92
+ """
93
+ overlap = set(a).intersection(b)
94
+
95
+ result = []
96
+
97
+ current, other = iter(a), iter(b)
98
+
99
+ while True:
100
+ for element in current:
101
+ if element in overlap:
102
+ overlap.discard(element)
103
+ other, current = current, other
104
+ break
105
+
106
+ result.append(element)
107
+ else:
108
+ result.extend(other)
109
+ break
110
+
111
+ return result
112
+
113
+
114
+ def coerce_to_immutabledict(d: Mapping[_KT, _VT]) -> immutabledict[_KT, _VT]:
115
+ if not d:
116
+ return EMPTY_DICT
117
+ elif isinstance(d, immutabledict):
118
+ return d
119
+ else:
120
+ return immutabledict(d)
121
+
122
+
123
+ EMPTY_DICT: immutabledict[Any, Any] = immutabledict()
124
+
125
+
126
+ class FacadeDict(ImmutableDictBase[_KT, _VT]):
127
+ """A dictionary that is not publicly mutable."""
128
+
129
+ def __new__(cls, *args: Any) -> FacadeDict[Any, Any]:
130
+ new: FacadeDict[Any, Any] = ImmutableDictBase.__new__(cls)
131
+ return new
132
+
133
+ def copy(self) -> NoReturn:
134
+ raise NotImplementedError(
135
+ "an immutabledict shouldn't need to be copied. use dict(d) "
136
+ "if you need a mutable dictionary."
137
+ )
138
+
139
+ def __reduce__(self) -> Any:
140
+ return FacadeDict, (dict(self),)
141
+
142
+ def _insert_item(self, key: _KT, value: _VT) -> None:
143
+ """insert an item into the dictionary directly."""
144
+ dict.__setitem__(self, key, value)
145
+
146
+ def __repr__(self) -> str:
147
+ return "FacadeDict(%s)" % dict.__repr__(self)
148
+
149
+
150
+ _DT = TypeVar("_DT", bound=Any)
151
+
152
+ _F = TypeVar("_F", bound=Any)
153
+
154
+
155
+ class Properties(Generic[_T]):
156
+ """Provide a __getattr__/__setattr__ interface over a dict."""
157
+
158
+ __slots__ = ("_data",)
159
+
160
+ _data: Dict[str, _T]
161
+
162
+ def __init__(self, data: Dict[str, _T]):
163
+ object.__setattr__(self, "_data", data)
164
+
165
+ def __len__(self) -> int:
166
+ return len(self._data)
167
+
168
+ def __iter__(self) -> Iterator[_T]:
169
+ return iter(list(self._data.values()))
170
+
171
+ def __dir__(self) -> List[str]:
172
+ return dir(super()) + [str(k) for k in self._data.keys()]
173
+
174
+ def __add__(self, other: Properties[_F]) -> List[Union[_T, _F]]:
175
+ return list(self) + list(other)
176
+
177
+ def __setitem__(self, key: str, obj: _T) -> None:
178
+ self._data[key] = obj
179
+
180
+ def __getitem__(self, key: str) -> _T:
181
+ return self._data[key]
182
+
183
+ def __delitem__(self, key: str) -> None:
184
+ del self._data[key]
185
+
186
+ def __setattr__(self, key: str, obj: _T) -> None:
187
+ self._data[key] = obj
188
+
189
+ def __getstate__(self) -> Dict[str, Any]:
190
+ return {"_data": self._data}
191
+
192
+ def __setstate__(self, state: Dict[str, Any]) -> None:
193
+ object.__setattr__(self, "_data", state["_data"])
194
+
195
+ def __getattr__(self, key: str) -> _T:
196
+ try:
197
+ return self._data[key]
198
+ except KeyError:
199
+ raise AttributeError(key)
200
+
201
+ def __contains__(self, key: str) -> bool:
202
+ return key in self._data
203
+
204
+ def as_readonly(self) -> ReadOnlyProperties[_T]:
205
+ """Return an immutable proxy for this :class:`.Properties`."""
206
+
207
+ return ReadOnlyProperties(self._data)
208
+
209
+ def update(self, value: Dict[str, _T]) -> None:
210
+ self._data.update(value)
211
+
212
+ @overload
213
+ def get(self, key: str) -> Optional[_T]: ...
214
+
215
+ @overload
216
+ def get(self, key: str, default: Union[_DT, _T]) -> Union[_DT, _T]: ...
217
+
218
+ def get(
219
+ self, key: str, default: Optional[Union[_DT, _T]] = None
220
+ ) -> Optional[Union[_T, _DT]]:
221
+ if key in self:
222
+ return self[key]
223
+ else:
224
+ return default
225
+
226
+ def keys(self) -> List[str]:
227
+ return list(self._data)
228
+
229
+ def values(self) -> List[_T]:
230
+ return list(self._data.values())
231
+
232
+ def items(self) -> List[Tuple[str, _T]]:
233
+ return list(self._data.items())
234
+
235
+ def has_key(self, key: str) -> bool:
236
+ return key in self._data
237
+
238
+ def clear(self) -> None:
239
+ self._data.clear()
240
+
241
+
242
+ class OrderedProperties(Properties[_T]):
243
+ """Provide a __getattr__/__setattr__ interface with an OrderedDict
244
+ as backing store."""
245
+
246
+ __slots__ = ()
247
+
248
+ def __init__(self):
249
+ Properties.__init__(self, OrderedDict())
250
+
251
+
252
+ class ReadOnlyProperties(ReadOnlyContainer, Properties[_T]):
253
+ """Provide immutable dict/object attribute to an underlying dictionary."""
254
+
255
+ __slots__ = ()
256
+
257
+
258
+ def _ordered_dictionary_sort(d, key=None):
259
+ """Sort an OrderedDict in-place."""
260
+
261
+ items = [(k, d[k]) for k in sorted(d, key=key)]
262
+
263
+ d.clear()
264
+
265
+ d.update(items)
266
+
267
+
268
+ OrderedDict = dict
269
+ sort_dictionary = _ordered_dictionary_sort
270
+
271
+
272
+ class WeakSequence(Sequence[_T]):
273
+ def __init__(self, __elements: Sequence[_T] = (), /):
274
+ # adapted from weakref.WeakKeyDictionary, prevent reference
275
+ # cycles in the collection itself
276
+ def _remove(item, selfref=weakref.ref(self)):
277
+ self = selfref()
278
+ if self is not None:
279
+ self._storage.remove(item)
280
+
281
+ self._remove = _remove
282
+ self._storage = [
283
+ weakref.ref(element, _remove) for element in __elements
284
+ ]
285
+
286
+ def append(self, item):
287
+ self._storage.append(weakref.ref(item, self._remove))
288
+
289
+ def __len__(self):
290
+ return len(self._storage)
291
+
292
+ def __iter__(self):
293
+ return (
294
+ obj for obj in (ref() for ref in self._storage) if obj is not None
295
+ )
296
+
297
+ def __getitem__(self, index):
298
+ return self._storage[index]()
299
+
300
+
301
+ OrderedIdentitySet = IdentitySet
302
+
303
+
304
+ class PopulateDict(Dict[_KT, _VT]):
305
+ """A dict which populates missing values via a creation function.
306
+
307
+ Note the creation function takes a key, unlike
308
+ collections.defaultdict.
309
+
310
+ """
311
+
312
+ def __init__(self, creator: Callable[[_KT], _VT]):
313
+ self.creator = creator
314
+
315
+ def __missing__(self, key: Any) -> Any:
316
+ self[key] = val = self.creator(key)
317
+ return val
318
+
319
+
320
+ class WeakPopulateDict(Dict[_KT, _VT]):
321
+ """Like PopulateDict, but assumes a self + a method and does not create
322
+ a reference cycle.
323
+
324
+ """
325
+
326
+ def __init__(self, creator_method: types.MethodType):
327
+ self.creator = creator_method.__func__
328
+ weakself = creator_method.__self__
329
+ self.weakself = weakref.ref(weakself)
330
+
331
+ def __missing__(self, key: Any) -> Any:
332
+ self[key] = val = self.creator(self.weakself(), key)
333
+ return val
334
+
335
+
336
+ # Define collections that are capable of storing
337
+ # ColumnElement objects as hashable keys/elements.
338
+ # At this point, these are mostly historical, things
339
+ # used to be more complicated.
340
+ column_set = set
341
+ column_dict = dict
342
+ ordered_column_set = OrderedSet
343
+
344
+
345
+ class UniqueAppender(Generic[_T]):
346
+ """Appends items to a collection ensuring uniqueness.
347
+
348
+ Additional appends() of the same object are ignored. Membership is
349
+ determined by identity (``is a``) not equality (``==``).
350
+ """
351
+
352
+ __slots__ = "data", "_data_appender", "_unique"
353
+
354
+ data: Union[Iterable[_T], Set[_T], List[_T]]
355
+ _data_appender: Callable[[_T], None]
356
+ _unique: Dict[int, Literal[True]]
357
+
358
+ def __init__(
359
+ self,
360
+ data: Union[Iterable[_T], Set[_T], List[_T]],
361
+ via: Optional[str] = None,
362
+ ):
363
+ self.data = data
364
+ self._unique = {}
365
+ if via:
366
+ self._data_appender = getattr(data, via)
367
+ elif hasattr(data, "append"):
368
+ self._data_appender = cast("List[_T]", data).append
369
+ elif hasattr(data, "add"):
370
+ self._data_appender = cast("Set[_T]", data).add
371
+
372
+ def append(self, item: _T) -> None:
373
+ id_ = id(item)
374
+ if id_ not in self._unique:
375
+ self._data_appender(item)
376
+ self._unique[id_] = True
377
+
378
+ def __iter__(self) -> Iterator[_T]:
379
+ return iter(self.data)
380
+
381
+
382
+ def coerce_generator_arg(arg: Any) -> List[Any]:
383
+ if len(arg) == 1 and isinstance(arg[0], types.GeneratorType):
384
+ return list(arg[0])
385
+ else:
386
+ return cast("List[Any]", arg)
387
+
388
+
389
+ def to_list(x: Any, default: Optional[List[Any]] = None) -> List[Any]:
390
+ if x is None:
391
+ return default # type: ignore
392
+ if not is_non_string_iterable(x):
393
+ return [x]
394
+ elif isinstance(x, list):
395
+ return x
396
+ else:
397
+ return list(x)
398
+
399
+
400
+ def has_intersection(set_: Container[Any], iterable: Iterable[Any]) -> bool:
401
+ r"""return True if any items of set\_ are present in iterable.
402
+
403
+ Goes through special effort to ensure __hash__ is not called
404
+ on items in iterable that don't support it.
405
+
406
+ """
407
+ return any(i in set_ for i in iterable if i.__hash__)
408
+
409
+
410
+ def to_set(x):
411
+ if x is None:
412
+ return set()
413
+ if not isinstance(x, set):
414
+ return set(to_list(x))
415
+ else:
416
+ return x
417
+
418
+
419
+ def to_column_set(x: Any) -> Set[Any]:
420
+ if x is None:
421
+ return column_set()
422
+ if not isinstance(x, column_set):
423
+ return column_set(to_list(x))
424
+ else:
425
+ return x
426
+
427
+
428
+ def update_copy(
429
+ d: Dict[Any, Any], _new: Optional[Dict[Any, Any]] = None, **kw: Any
430
+ ) -> Dict[Any, Any]:
431
+ """Copy the given dict and update with the given values."""
432
+
433
+ d = d.copy()
434
+ if _new:
435
+ d.update(_new)
436
+ d.update(**kw)
437
+ return d
438
+
439
+
440
+ def flatten_iterator(x: Iterable[_T]) -> Iterator[_T]:
441
+ """Given an iterator of which further sub-elements may also be
442
+ iterators, flatten the sub-elements into a single iterator.
443
+
444
+ """
445
+ elem: _T
446
+ for elem in x:
447
+ if not isinstance(elem, str) and hasattr(elem, "__iter__"):
448
+ yield from flatten_iterator(elem)
449
+ else:
450
+ yield elem
451
+
452
+
453
+ class LRUCache(typing.MutableMapping[_KT, _VT]):
454
+ """Dictionary with 'squishy' removal of least
455
+ recently used items.
456
+
457
+ Note that either get() or [] should be used here, but
458
+ generally its not safe to do an "in" check first as the dictionary
459
+ can change subsequent to that call.
460
+
461
+ """
462
+
463
+ __slots__ = (
464
+ "capacity",
465
+ "threshold",
466
+ "size_alert",
467
+ "_data",
468
+ "_counter",
469
+ "_mutex",
470
+ )
471
+
472
+ capacity: int
473
+ threshold: float
474
+ size_alert: Optional[Callable[[LRUCache[_KT, _VT]], None]]
475
+
476
+ def __init__(
477
+ self,
478
+ capacity: int = 100,
479
+ threshold: float = 0.5,
480
+ size_alert: Optional[Callable[..., None]] = None,
481
+ ):
482
+ self.capacity = capacity
483
+ self.threshold = threshold
484
+ self.size_alert = size_alert
485
+ self._counter = 0
486
+ self._mutex = threading.Lock()
487
+ self._data: Dict[_KT, Tuple[_KT, _VT, List[int]]] = {}
488
+
489
+ def _inc_counter(self):
490
+ self._counter += 1
491
+ return self._counter
492
+
493
+ @overload
494
+ def get(self, key: _KT) -> Optional[_VT]: ...
495
+
496
+ @overload
497
+ def get(self, key: _KT, default: Union[_VT, _T]) -> Union[_VT, _T]: ...
498
+
499
+ def get(
500
+ self, key: _KT, default: Optional[Union[_VT, _T]] = None
501
+ ) -> Optional[Union[_VT, _T]]:
502
+ item = self._data.get(key)
503
+ if item is not None:
504
+ item[2][0] = self._inc_counter()
505
+ return item[1]
506
+ else:
507
+ return default
508
+
509
+ def __getitem__(self, key: _KT) -> _VT:
510
+ item = self._data[key]
511
+ item[2][0] = self._inc_counter()
512
+ return item[1]
513
+
514
+ def __iter__(self) -> Iterator[_KT]:
515
+ return iter(self._data)
516
+
517
+ def __len__(self) -> int:
518
+ return len(self._data)
519
+
520
+ def values(self) -> ValuesView[_VT]:
521
+ return typing.ValuesView({k: i[1] for k, i in self._data.items()})
522
+
523
+ def __setitem__(self, key: _KT, value: _VT) -> None:
524
+ self._data[key] = (key, value, [self._inc_counter()])
525
+ self._manage_size()
526
+
527
+ def __delitem__(self, __v: _KT) -> None:
528
+ del self._data[__v]
529
+
530
+ @property
531
+ def size_threshold(self) -> float:
532
+ return self.capacity + self.capacity * self.threshold
533
+
534
+ def _manage_size(self) -> None:
535
+ if not self._mutex.acquire(False):
536
+ return
537
+ try:
538
+ size_alert = bool(self.size_alert)
539
+ while len(self) > self.capacity + self.capacity * self.threshold:
540
+ if size_alert:
541
+ size_alert = False
542
+ self.size_alert(self) # type: ignore
543
+ by_counter = sorted(
544
+ self._data.values(),
545
+ key=operator.itemgetter(2),
546
+ reverse=True,
547
+ )
548
+ for item in by_counter[self.capacity :]:
549
+ try:
550
+ del self._data[item[0]]
551
+ except KeyError:
552
+ # deleted elsewhere; skip
553
+ continue
554
+ finally:
555
+ self._mutex.release()
556
+
557
+
558
+ class _CreateFuncType(Protocol[_T_co]):
559
+ def __call__(self) -> _T_co: ...
560
+
561
+
562
+ class _ScopeFuncType(Protocol):
563
+ def __call__(self) -> Any: ...
564
+
565
+
566
+ class ScopedRegistry(Generic[_T]):
567
+ """A Registry that can store one or multiple instances of a single
568
+ class on the basis of a "scope" function.
569
+
570
+ The object implements ``__call__`` as the "getter", so by
571
+ calling ``myregistry()`` the contained object is returned
572
+ for the current scope.
573
+
574
+ :param createfunc:
575
+ a callable that returns a new object to be placed in the registry
576
+
577
+ :param scopefunc:
578
+ a callable that will return a key to store/retrieve an object.
579
+ """
580
+
581
+ __slots__ = "createfunc", "scopefunc", "registry"
582
+
583
+ createfunc: _CreateFuncType[_T]
584
+ scopefunc: _ScopeFuncType
585
+ registry: Any
586
+
587
+ def __init__(
588
+ self, createfunc: Callable[[], _T], scopefunc: Callable[[], Any]
589
+ ):
590
+ """Construct a new :class:`.ScopedRegistry`.
591
+
592
+ :param createfunc: A creation function that will generate
593
+ a new value for the current scope, if none is present.
594
+
595
+ :param scopefunc: A function that returns a hashable
596
+ token representing the current scope (such as, current
597
+ thread identifier).
598
+
599
+ """
600
+ self.createfunc = createfunc
601
+ self.scopefunc = scopefunc
602
+ self.registry = {}
603
+
604
+ def __call__(self) -> _T:
605
+ key = self.scopefunc()
606
+ try:
607
+ return self.registry[key] # type: ignore[no-any-return]
608
+ except KeyError:
609
+ return self.registry.setdefault(key, self.createfunc()) # type: ignore[no-any-return] # noqa: E501
610
+
611
+ def has(self) -> bool:
612
+ """Return True if an object is present in the current scope."""
613
+
614
+ return self.scopefunc() in self.registry
615
+
616
+ def set(self, obj: _T) -> None:
617
+ """Set the value for the current scope."""
618
+
619
+ self.registry[self.scopefunc()] = obj
620
+
621
+ def clear(self) -> None:
622
+ """Clear the current scope, if any."""
623
+
624
+ try:
625
+ del self.registry[self.scopefunc()]
626
+ except KeyError:
627
+ pass
628
+
629
+
630
+ class ThreadLocalRegistry(ScopedRegistry[_T]):
631
+ """A :class:`.ScopedRegistry` that uses a ``threading.local()``
632
+ variable for storage.
633
+
634
+ """
635
+
636
+ def __init__(self, createfunc: Callable[[], _T]):
637
+ self.createfunc = createfunc
638
+ self.registry = threading.local()
639
+
640
+ def __call__(self) -> _T:
641
+ try:
642
+ return self.registry.value # type: ignore[no-any-return]
643
+ except AttributeError:
644
+ val = self.registry.value = self.createfunc()
645
+ return val
646
+
647
+ def has(self) -> bool:
648
+ return hasattr(self.registry, "value")
649
+
650
+ def set(self, obj: _T) -> None:
651
+ self.registry.value = obj
652
+
653
+ def clear(self) -> None:
654
+ try:
655
+ del self.registry.value
656
+ except AttributeError:
657
+ pass
658
+
659
+
660
+ def has_dupes(sequence, target):
661
+ """Given a sequence and search object, return True if there's more
662
+ than one, False if zero or one of them.
663
+
664
+
665
+ """
666
+ # compare to .index version below, this version introduces less function
667
+ # overhead and is usually the same speed. At 15000 items (way bigger than
668
+ # a relationship-bound collection in memory usually is) it begins to
669
+ # fall behind the other version only by microseconds.
670
+ c = 0
671
+ for item in sequence:
672
+ if item is target:
673
+ c += 1
674
+ if c > 1:
675
+ return True
676
+ return False
677
+
678
+
679
+ # .index version. the two __contains__ calls as well
680
+ # as .index() and isinstance() slow this down.
681
+ # def has_dupes(sequence, target):
682
+ # if target not in sequence:
683
+ # return False
684
+ # elif not isinstance(sequence, collections_abc.Sequence):
685
+ # return False
686
+ #
687
+ # idx = sequence.index(target)
688
+ # return target in sequence[idx + 1:]
@@ -0,0 +1,8 @@
1
+ # util/_collections_cy.pxd
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
+ cdef unsigned long long _get_id(item: object)