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