SQLAlchemy 2.0.47__cp313-cp313t-win32.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlalchemy/__init__.py +283 -0
- sqlalchemy/connectors/__init__.py +18 -0
- sqlalchemy/connectors/aioodbc.py +184 -0
- sqlalchemy/connectors/asyncio.py +429 -0
- sqlalchemy/connectors/pyodbc.py +250 -0
- sqlalchemy/cyextension/__init__.py +6 -0
- sqlalchemy/cyextension/collections.cp313t-win32.pyd +0 -0
- sqlalchemy/cyextension/collections.pyx +409 -0
- sqlalchemy/cyextension/immutabledict.cp313t-win32.pyd +0 -0
- sqlalchemy/cyextension/immutabledict.pxd +8 -0
- sqlalchemy/cyextension/immutabledict.pyx +133 -0
- sqlalchemy/cyextension/processors.cp313t-win32.pyd +0 -0
- sqlalchemy/cyextension/processors.pyx +68 -0
- sqlalchemy/cyextension/resultproxy.cp313t-win32.pyd +0 -0
- sqlalchemy/cyextension/resultproxy.pyx +102 -0
- sqlalchemy/cyextension/util.cp313t-win32.pyd +0 -0
- sqlalchemy/cyextension/util.pyx +90 -0
- sqlalchemy/dialects/__init__.py +62 -0
- sqlalchemy/dialects/_typing.py +30 -0
- sqlalchemy/dialects/mssql/__init__.py +88 -0
- sqlalchemy/dialects/mssql/aioodbc.py +63 -0
- sqlalchemy/dialects/mssql/base.py +4093 -0
- sqlalchemy/dialects/mssql/information_schema.py +285 -0
- sqlalchemy/dialects/mssql/json.py +129 -0
- sqlalchemy/dialects/mssql/provision.py +185 -0
- sqlalchemy/dialects/mssql/pymssql.py +126 -0
- sqlalchemy/dialects/mssql/pyodbc.py +760 -0
- sqlalchemy/dialects/mysql/__init__.py +104 -0
- sqlalchemy/dialects/mysql/aiomysql.py +250 -0
- sqlalchemy/dialects/mysql/asyncmy.py +231 -0
- sqlalchemy/dialects/mysql/base.py +3949 -0
- sqlalchemy/dialects/mysql/cymysql.py +106 -0
- sqlalchemy/dialects/mysql/dml.py +225 -0
- sqlalchemy/dialects/mysql/enumerated.py +282 -0
- sqlalchemy/dialects/mysql/expression.py +146 -0
- sqlalchemy/dialects/mysql/json.py +91 -0
- sqlalchemy/dialects/mysql/mariadb.py +72 -0
- sqlalchemy/dialects/mysql/mariadbconnector.py +322 -0
- sqlalchemy/dialects/mysql/mysqlconnector.py +302 -0
- sqlalchemy/dialects/mysql/mysqldb.py +314 -0
- sqlalchemy/dialects/mysql/provision.py +153 -0
- sqlalchemy/dialects/mysql/pymysql.py +158 -0
- sqlalchemy/dialects/mysql/pyodbc.py +157 -0
- sqlalchemy/dialects/mysql/reflection.py +727 -0
- sqlalchemy/dialects/mysql/reserved_words.py +570 -0
- sqlalchemy/dialects/mysql/types.py +835 -0
- sqlalchemy/dialects/oracle/__init__.py +81 -0
- sqlalchemy/dialects/oracle/base.py +3802 -0
- sqlalchemy/dialects/oracle/cx_oracle.py +1555 -0
- sqlalchemy/dialects/oracle/dictionary.py +507 -0
- sqlalchemy/dialects/oracle/oracledb.py +941 -0
- sqlalchemy/dialects/oracle/provision.py +297 -0
- sqlalchemy/dialects/oracle/types.py +316 -0
- sqlalchemy/dialects/oracle/vector.py +365 -0
- sqlalchemy/dialects/postgresql/__init__.py +167 -0
- sqlalchemy/dialects/postgresql/_psycopg_common.py +189 -0
- sqlalchemy/dialects/postgresql/array.py +519 -0
- sqlalchemy/dialects/postgresql/asyncpg.py +1284 -0
- sqlalchemy/dialects/postgresql/base.py +5378 -0
- sqlalchemy/dialects/postgresql/dml.py +339 -0
- sqlalchemy/dialects/postgresql/ext.py +540 -0
- sqlalchemy/dialects/postgresql/hstore.py +406 -0
- sqlalchemy/dialects/postgresql/json.py +404 -0
- sqlalchemy/dialects/postgresql/named_types.py +524 -0
- sqlalchemy/dialects/postgresql/operators.py +129 -0
- sqlalchemy/dialects/postgresql/pg8000.py +669 -0
- sqlalchemy/dialects/postgresql/pg_catalog.py +326 -0
- sqlalchemy/dialects/postgresql/provision.py +183 -0
- sqlalchemy/dialects/postgresql/psycopg.py +862 -0
- sqlalchemy/dialects/postgresql/psycopg2.py +892 -0
- sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
- sqlalchemy/dialects/postgresql/ranges.py +1031 -0
- sqlalchemy/dialects/postgresql/types.py +313 -0
- sqlalchemy/dialects/sqlite/__init__.py +57 -0
- sqlalchemy/dialects/sqlite/aiosqlite.py +482 -0
- sqlalchemy/dialects/sqlite/base.py +3056 -0
- sqlalchemy/dialects/sqlite/dml.py +263 -0
- sqlalchemy/dialects/sqlite/json.py +92 -0
- sqlalchemy/dialects/sqlite/provision.py +229 -0
- sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
- sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
- sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
- sqlalchemy/engine/__init__.py +62 -0
- sqlalchemy/engine/_py_processors.py +136 -0
- sqlalchemy/engine/_py_row.py +128 -0
- sqlalchemy/engine/_py_util.py +74 -0
- sqlalchemy/engine/base.py +3390 -0
- sqlalchemy/engine/characteristics.py +155 -0
- sqlalchemy/engine/create.py +893 -0
- sqlalchemy/engine/cursor.py +2298 -0
- sqlalchemy/engine/default.py +2394 -0
- sqlalchemy/engine/events.py +965 -0
- sqlalchemy/engine/interfaces.py +3471 -0
- sqlalchemy/engine/mock.py +134 -0
- sqlalchemy/engine/processors.py +61 -0
- sqlalchemy/engine/reflection.py +2102 -0
- sqlalchemy/engine/result.py +2399 -0
- sqlalchemy/engine/row.py +400 -0
- sqlalchemy/engine/strategies.py +16 -0
- sqlalchemy/engine/url.py +924 -0
- sqlalchemy/engine/util.py +167 -0
- sqlalchemy/event/__init__.py +26 -0
- sqlalchemy/event/api.py +220 -0
- sqlalchemy/event/attr.py +676 -0
- sqlalchemy/event/base.py +472 -0
- sqlalchemy/event/legacy.py +258 -0
- sqlalchemy/event/registry.py +390 -0
- sqlalchemy/events.py +17 -0
- sqlalchemy/exc.py +832 -0
- sqlalchemy/ext/__init__.py +11 -0
- sqlalchemy/ext/associationproxy.py +2027 -0
- sqlalchemy/ext/asyncio/__init__.py +25 -0
- sqlalchemy/ext/asyncio/base.py +281 -0
- sqlalchemy/ext/asyncio/engine.py +1471 -0
- sqlalchemy/ext/asyncio/exc.py +21 -0
- sqlalchemy/ext/asyncio/result.py +965 -0
- sqlalchemy/ext/asyncio/scoping.py +1599 -0
- sqlalchemy/ext/asyncio/session.py +1947 -0
- sqlalchemy/ext/automap.py +1701 -0
- sqlalchemy/ext/baked.py +570 -0
- sqlalchemy/ext/compiler.py +600 -0
- sqlalchemy/ext/declarative/__init__.py +65 -0
- sqlalchemy/ext/declarative/extensions.py +564 -0
- sqlalchemy/ext/horizontal_shard.py +478 -0
- sqlalchemy/ext/hybrid.py +1535 -0
- sqlalchemy/ext/indexable.py +364 -0
- sqlalchemy/ext/instrumentation.py +450 -0
- sqlalchemy/ext/mutable.py +1085 -0
- sqlalchemy/ext/mypy/__init__.py +6 -0
- sqlalchemy/ext/mypy/apply.py +324 -0
- sqlalchemy/ext/mypy/decl_class.py +515 -0
- sqlalchemy/ext/mypy/infer.py +590 -0
- sqlalchemy/ext/mypy/names.py +335 -0
- sqlalchemy/ext/mypy/plugin.py +303 -0
- sqlalchemy/ext/mypy/util.py +357 -0
- sqlalchemy/ext/orderinglist.py +439 -0
- sqlalchemy/ext/serializer.py +185 -0
- sqlalchemy/future/__init__.py +16 -0
- sqlalchemy/future/engine.py +15 -0
- sqlalchemy/inspection.py +174 -0
- sqlalchemy/log.py +288 -0
- sqlalchemy/orm/__init__.py +171 -0
- sqlalchemy/orm/_orm_constructors.py +2661 -0
- sqlalchemy/orm/_typing.py +179 -0
- sqlalchemy/orm/attributes.py +2845 -0
- sqlalchemy/orm/base.py +971 -0
- sqlalchemy/orm/bulk_persistence.py +2135 -0
- sqlalchemy/orm/clsregistry.py +571 -0
- sqlalchemy/orm/collections.py +1627 -0
- sqlalchemy/orm/context.py +3334 -0
- sqlalchemy/orm/decl_api.py +2004 -0
- sqlalchemy/orm/decl_base.py +2192 -0
- sqlalchemy/orm/dependency.py +1302 -0
- sqlalchemy/orm/descriptor_props.py +1092 -0
- sqlalchemy/orm/dynamic.py +300 -0
- sqlalchemy/orm/evaluator.py +379 -0
- sqlalchemy/orm/events.py +3252 -0
- sqlalchemy/orm/exc.py +237 -0
- sqlalchemy/orm/identity.py +302 -0
- sqlalchemy/orm/instrumentation.py +754 -0
- sqlalchemy/orm/interfaces.py +1496 -0
- sqlalchemy/orm/loading.py +1686 -0
- sqlalchemy/orm/mapped_collection.py +557 -0
- sqlalchemy/orm/mapper.py +4444 -0
- sqlalchemy/orm/path_registry.py +809 -0
- sqlalchemy/orm/persistence.py +1788 -0
- sqlalchemy/orm/properties.py +935 -0
- sqlalchemy/orm/query.py +3459 -0
- sqlalchemy/orm/relationships.py +3508 -0
- sqlalchemy/orm/scoping.py +2148 -0
- sqlalchemy/orm/session.py +5280 -0
- sqlalchemy/orm/state.py +1168 -0
- sqlalchemy/orm/state_changes.py +196 -0
- sqlalchemy/orm/strategies.py +3470 -0
- sqlalchemy/orm/strategy_options.py +2568 -0
- sqlalchemy/orm/sync.py +164 -0
- sqlalchemy/orm/unitofwork.py +796 -0
- sqlalchemy/orm/util.py +2403 -0
- sqlalchemy/orm/writeonly.py +674 -0
- sqlalchemy/pool/__init__.py +44 -0
- sqlalchemy/pool/base.py +1524 -0
- sqlalchemy/pool/events.py +375 -0
- sqlalchemy/pool/impl.py +588 -0
- sqlalchemy/py.typed +0 -0
- sqlalchemy/schema.py +69 -0
- sqlalchemy/sql/__init__.py +145 -0
- sqlalchemy/sql/_dml_constructors.py +132 -0
- sqlalchemy/sql/_elements_constructors.py +1872 -0
- sqlalchemy/sql/_orm_types.py +20 -0
- sqlalchemy/sql/_py_util.py +75 -0
- sqlalchemy/sql/_selectable_constructors.py +763 -0
- sqlalchemy/sql/_typing.py +482 -0
- sqlalchemy/sql/annotation.py +587 -0
- sqlalchemy/sql/base.py +2293 -0
- sqlalchemy/sql/cache_key.py +1057 -0
- sqlalchemy/sql/coercions.py +1404 -0
- sqlalchemy/sql/compiler.py +8081 -0
- sqlalchemy/sql/crud.py +1752 -0
- sqlalchemy/sql/ddl.py +1444 -0
- sqlalchemy/sql/default_comparator.py +551 -0
- sqlalchemy/sql/dml.py +1850 -0
- sqlalchemy/sql/elements.py +5589 -0
- sqlalchemy/sql/events.py +458 -0
- sqlalchemy/sql/expression.py +159 -0
- sqlalchemy/sql/functions.py +2158 -0
- sqlalchemy/sql/lambdas.py +1442 -0
- sqlalchemy/sql/naming.py +209 -0
- sqlalchemy/sql/operators.py +2623 -0
- sqlalchemy/sql/roles.py +323 -0
- sqlalchemy/sql/schema.py +6222 -0
- sqlalchemy/sql/selectable.py +7265 -0
- sqlalchemy/sql/sqltypes.py +3930 -0
- sqlalchemy/sql/traversals.py +1024 -0
- sqlalchemy/sql/type_api.py +2368 -0
- sqlalchemy/sql/util.py +1485 -0
- sqlalchemy/sql/visitors.py +1164 -0
- sqlalchemy/testing/__init__.py +96 -0
- sqlalchemy/testing/assertions.py +994 -0
- sqlalchemy/testing/assertsql.py +520 -0
- sqlalchemy/testing/asyncio.py +135 -0
- sqlalchemy/testing/config.py +434 -0
- sqlalchemy/testing/engines.py +483 -0
- sqlalchemy/testing/entities.py +117 -0
- sqlalchemy/testing/exclusions.py +476 -0
- sqlalchemy/testing/fixtures/__init__.py +28 -0
- sqlalchemy/testing/fixtures/base.py +384 -0
- sqlalchemy/testing/fixtures/mypy.py +332 -0
- sqlalchemy/testing/fixtures/orm.py +227 -0
- sqlalchemy/testing/fixtures/sql.py +482 -0
- sqlalchemy/testing/pickleable.py +155 -0
- sqlalchemy/testing/plugin/__init__.py +6 -0
- sqlalchemy/testing/plugin/bootstrap.py +51 -0
- sqlalchemy/testing/plugin/plugin_base.py +828 -0
- sqlalchemy/testing/plugin/pytestplugin.py +892 -0
- sqlalchemy/testing/profiling.py +329 -0
- sqlalchemy/testing/provision.py +603 -0
- sqlalchemy/testing/requirements.py +1945 -0
- sqlalchemy/testing/schema.py +198 -0
- sqlalchemy/testing/suite/__init__.py +19 -0
- sqlalchemy/testing/suite/test_cte.py +237 -0
- sqlalchemy/testing/suite/test_ddl.py +389 -0
- sqlalchemy/testing/suite/test_deprecations.py +153 -0
- sqlalchemy/testing/suite/test_dialect.py +776 -0
- sqlalchemy/testing/suite/test_insert.py +630 -0
- sqlalchemy/testing/suite/test_reflection.py +3557 -0
- sqlalchemy/testing/suite/test_results.py +504 -0
- sqlalchemy/testing/suite/test_rowcount.py +258 -0
- sqlalchemy/testing/suite/test_select.py +2010 -0
- sqlalchemy/testing/suite/test_sequence.py +317 -0
- sqlalchemy/testing/suite/test_types.py +2147 -0
- sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
- sqlalchemy/testing/suite/test_update_delete.py +139 -0
- sqlalchemy/testing/util.py +535 -0
- sqlalchemy/testing/warnings.py +52 -0
- sqlalchemy/types.py +74 -0
- sqlalchemy/util/__init__.py +162 -0
- sqlalchemy/util/_collections.py +712 -0
- sqlalchemy/util/_concurrency_py3k.py +288 -0
- sqlalchemy/util/_has_cy.py +40 -0
- sqlalchemy/util/_py_collections.py +541 -0
- sqlalchemy/util/compat.py +421 -0
- sqlalchemy/util/concurrency.py +110 -0
- sqlalchemy/util/deprecations.py +401 -0
- sqlalchemy/util/langhelpers.py +2203 -0
- sqlalchemy/util/preloaded.py +150 -0
- sqlalchemy/util/queue.py +322 -0
- sqlalchemy/util/tool_support.py +201 -0
- sqlalchemy/util/topological.py +120 -0
- sqlalchemy/util/typing.py +734 -0
- sqlalchemy-2.0.47.dist-info/METADATA +243 -0
- sqlalchemy-2.0.47.dist-info/RECORD +274 -0
- sqlalchemy-2.0.47.dist-info/WHEEL +5 -0
- sqlalchemy-2.0.47.dist-info/licenses/LICENSE +19 -0
- sqlalchemy-2.0.47.dist-info/top_level.txt +1 -0
sqlalchemy/orm/base.py
ADDED
|
@@ -0,0 +1,971 @@
|
|
|
1
|
+
# orm/base.py
|
|
2
|
+
# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
|
|
3
|
+
# <see AUTHORS file>
|
|
4
|
+
#
|
|
5
|
+
# This module is part of SQLAlchemy and is released under
|
|
6
|
+
# the MIT License: https://www.opensource.org/licenses/mit-license.php
|
|
7
|
+
|
|
8
|
+
"""Constants and rudimental functions used throughout the ORM."""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from enum import Enum
|
|
13
|
+
import operator
|
|
14
|
+
import typing
|
|
15
|
+
from typing import Any
|
|
16
|
+
from typing import Callable
|
|
17
|
+
from typing import Dict
|
|
18
|
+
from typing import Generic
|
|
19
|
+
from typing import no_type_check
|
|
20
|
+
from typing import Optional
|
|
21
|
+
from typing import overload
|
|
22
|
+
from typing import Tuple
|
|
23
|
+
from typing import Type
|
|
24
|
+
from typing import TYPE_CHECKING
|
|
25
|
+
from typing import TypeVar
|
|
26
|
+
from typing import Union
|
|
27
|
+
|
|
28
|
+
from . import exc
|
|
29
|
+
from ._typing import insp_is_mapper
|
|
30
|
+
from .. import exc as sa_exc
|
|
31
|
+
from .. import inspection
|
|
32
|
+
from .. import util
|
|
33
|
+
from ..sql import roles
|
|
34
|
+
from ..sql.elements import SQLColumnExpression
|
|
35
|
+
from ..sql.elements import SQLCoreOperations
|
|
36
|
+
from ..util import FastIntFlag
|
|
37
|
+
from ..util.langhelpers import TypingOnly
|
|
38
|
+
from ..util.typing import Literal
|
|
39
|
+
|
|
40
|
+
if typing.TYPE_CHECKING:
|
|
41
|
+
from ._typing import _EntityType
|
|
42
|
+
from ._typing import _ExternalEntityType
|
|
43
|
+
from ._typing import _InternalEntityType
|
|
44
|
+
from .attributes import InstrumentedAttribute
|
|
45
|
+
from .dynamic import AppenderQuery
|
|
46
|
+
from .instrumentation import ClassManager
|
|
47
|
+
from .interfaces import PropComparator
|
|
48
|
+
from .mapper import Mapper
|
|
49
|
+
from .state import InstanceState
|
|
50
|
+
from .util import AliasedClass
|
|
51
|
+
from .writeonly import WriteOnlyCollection
|
|
52
|
+
from ..sql._typing import _ColumnExpressionArgument
|
|
53
|
+
from ..sql._typing import _InfoType
|
|
54
|
+
from ..sql.elements import ColumnElement
|
|
55
|
+
from ..sql.operators import OperatorType
|
|
56
|
+
|
|
57
|
+
_T = TypeVar("_T", bound=Any)
|
|
58
|
+
_T_co = TypeVar("_T_co", bound=Any, covariant=True)
|
|
59
|
+
|
|
60
|
+
_O = TypeVar("_O", bound=object)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class LoaderCallableStatus(Enum):
|
|
64
|
+
PASSIVE_NO_RESULT = 0
|
|
65
|
+
"""Symbol returned by a loader callable or other attribute/history
|
|
66
|
+
retrieval operation when a value could not be determined, based
|
|
67
|
+
on loader callable flags.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
PASSIVE_CLASS_MISMATCH = 1
|
|
71
|
+
"""Symbol indicating that an object is locally present for a given
|
|
72
|
+
primary key identity but it is not of the requested class. The
|
|
73
|
+
return value is therefore None and no SQL should be emitted."""
|
|
74
|
+
|
|
75
|
+
ATTR_WAS_SET = 2
|
|
76
|
+
"""Symbol returned by a loader callable to indicate the
|
|
77
|
+
retrieved value, or values, were assigned to their attributes
|
|
78
|
+
on the target object.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
ATTR_EMPTY = 3
|
|
82
|
+
"""Symbol used internally to indicate an attribute had no callable."""
|
|
83
|
+
|
|
84
|
+
NO_VALUE = 4
|
|
85
|
+
"""Symbol which may be placed as the 'previous' value of an attribute,
|
|
86
|
+
indicating no value was loaded for an attribute when it was modified,
|
|
87
|
+
and flags indicated we were not to load it.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
NEVER_SET = NO_VALUE
|
|
91
|
+
"""
|
|
92
|
+
Synonymous with NO_VALUE
|
|
93
|
+
|
|
94
|
+
.. versionchanged:: 1.4 NEVER_SET was merged with NO_VALUE
|
|
95
|
+
|
|
96
|
+
"""
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
(
|
|
100
|
+
PASSIVE_NO_RESULT,
|
|
101
|
+
PASSIVE_CLASS_MISMATCH,
|
|
102
|
+
ATTR_WAS_SET,
|
|
103
|
+
ATTR_EMPTY,
|
|
104
|
+
NO_VALUE,
|
|
105
|
+
) = tuple(LoaderCallableStatus)
|
|
106
|
+
|
|
107
|
+
NEVER_SET = NO_VALUE
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class PassiveFlag(FastIntFlag):
|
|
111
|
+
"""Bitflag interface that passes options onto loader callables"""
|
|
112
|
+
|
|
113
|
+
NO_CHANGE = 0
|
|
114
|
+
"""No callables or SQL should be emitted on attribute access
|
|
115
|
+
and no state should change
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
CALLABLES_OK = 1
|
|
119
|
+
"""Loader callables can be fired off if a value
|
|
120
|
+
is not present.
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
SQL_OK = 2
|
|
124
|
+
"""Loader callables can emit SQL at least on scalar value attributes."""
|
|
125
|
+
|
|
126
|
+
RELATED_OBJECT_OK = 4
|
|
127
|
+
"""Callables can use SQL to load related objects as well
|
|
128
|
+
as scalar value attributes.
|
|
129
|
+
"""
|
|
130
|
+
|
|
131
|
+
INIT_OK = 8
|
|
132
|
+
"""Attributes should be initialized with a blank
|
|
133
|
+
value (None or an empty collection) upon get, if no other
|
|
134
|
+
value can be obtained.
|
|
135
|
+
"""
|
|
136
|
+
|
|
137
|
+
NON_PERSISTENT_OK = 16
|
|
138
|
+
"""Callables can be emitted if the parent is not persistent."""
|
|
139
|
+
|
|
140
|
+
LOAD_AGAINST_COMMITTED = 32
|
|
141
|
+
"""Callables should use committed values as primary/foreign keys during a
|
|
142
|
+
load.
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
NO_AUTOFLUSH = 64
|
|
146
|
+
"""Loader callables should disable autoflush."""
|
|
147
|
+
|
|
148
|
+
NO_RAISE = 128
|
|
149
|
+
"""Loader callables should not raise any assertions"""
|
|
150
|
+
|
|
151
|
+
DEFERRED_HISTORY_LOAD = 256
|
|
152
|
+
"""indicates special load of the previous value of an attribute"""
|
|
153
|
+
|
|
154
|
+
INCLUDE_PENDING_MUTATIONS = 512
|
|
155
|
+
|
|
156
|
+
# pre-packaged sets of flags used as inputs
|
|
157
|
+
PASSIVE_OFF = (
|
|
158
|
+
RELATED_OBJECT_OK | NON_PERSISTENT_OK | INIT_OK | CALLABLES_OK | SQL_OK
|
|
159
|
+
)
|
|
160
|
+
"Callables can be emitted in all cases."
|
|
161
|
+
|
|
162
|
+
PASSIVE_RETURN_NO_VALUE = PASSIVE_OFF ^ INIT_OK
|
|
163
|
+
"""PASSIVE_OFF ^ INIT_OK"""
|
|
164
|
+
|
|
165
|
+
PASSIVE_NO_INITIALIZE = PASSIVE_RETURN_NO_VALUE ^ CALLABLES_OK
|
|
166
|
+
"PASSIVE_RETURN_NO_VALUE ^ CALLABLES_OK"
|
|
167
|
+
|
|
168
|
+
PASSIVE_NO_FETCH = PASSIVE_OFF ^ SQL_OK
|
|
169
|
+
"PASSIVE_OFF ^ SQL_OK"
|
|
170
|
+
|
|
171
|
+
PASSIVE_NO_FETCH_RELATED = PASSIVE_OFF ^ RELATED_OBJECT_OK
|
|
172
|
+
"PASSIVE_OFF ^ RELATED_OBJECT_OK"
|
|
173
|
+
|
|
174
|
+
PASSIVE_ONLY_PERSISTENT = PASSIVE_OFF ^ NON_PERSISTENT_OK
|
|
175
|
+
"PASSIVE_OFF ^ NON_PERSISTENT_OK"
|
|
176
|
+
|
|
177
|
+
PASSIVE_MERGE = PASSIVE_OFF | NO_RAISE
|
|
178
|
+
"""PASSIVE_OFF | NO_RAISE
|
|
179
|
+
|
|
180
|
+
Symbol used specifically for session.merge() and similar cases
|
|
181
|
+
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
(
|
|
186
|
+
NO_CHANGE,
|
|
187
|
+
CALLABLES_OK,
|
|
188
|
+
SQL_OK,
|
|
189
|
+
RELATED_OBJECT_OK,
|
|
190
|
+
INIT_OK,
|
|
191
|
+
NON_PERSISTENT_OK,
|
|
192
|
+
LOAD_AGAINST_COMMITTED,
|
|
193
|
+
NO_AUTOFLUSH,
|
|
194
|
+
NO_RAISE,
|
|
195
|
+
DEFERRED_HISTORY_LOAD,
|
|
196
|
+
INCLUDE_PENDING_MUTATIONS,
|
|
197
|
+
PASSIVE_OFF,
|
|
198
|
+
PASSIVE_RETURN_NO_VALUE,
|
|
199
|
+
PASSIVE_NO_INITIALIZE,
|
|
200
|
+
PASSIVE_NO_FETCH,
|
|
201
|
+
PASSIVE_NO_FETCH_RELATED,
|
|
202
|
+
PASSIVE_ONLY_PERSISTENT,
|
|
203
|
+
PASSIVE_MERGE,
|
|
204
|
+
) = PassiveFlag.__members__.values()
|
|
205
|
+
|
|
206
|
+
DEFAULT_MANAGER_ATTR = "_sa_class_manager"
|
|
207
|
+
DEFAULT_STATE_ATTR = "_sa_instance_state"
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
class EventConstants(Enum):
|
|
211
|
+
EXT_CONTINUE = 1
|
|
212
|
+
EXT_STOP = 2
|
|
213
|
+
EXT_SKIP = 3
|
|
214
|
+
NO_KEY = 4
|
|
215
|
+
"""indicates an :class:`.AttributeEvent` event that did not have any
|
|
216
|
+
key argument.
|
|
217
|
+
|
|
218
|
+
.. versionadded:: 2.0
|
|
219
|
+
|
|
220
|
+
"""
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
EXT_CONTINUE, EXT_STOP, EXT_SKIP, NO_KEY = tuple(EventConstants)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
class RelationshipDirection(Enum):
|
|
227
|
+
"""enumeration which indicates the 'direction' of a
|
|
228
|
+
:class:`_orm.RelationshipProperty`.
|
|
229
|
+
|
|
230
|
+
:class:`.RelationshipDirection` is accessible from the
|
|
231
|
+
:attr:`_orm.Relationship.direction` attribute of
|
|
232
|
+
:class:`_orm.RelationshipProperty`.
|
|
233
|
+
|
|
234
|
+
"""
|
|
235
|
+
|
|
236
|
+
ONETOMANY = 1
|
|
237
|
+
"""Indicates the one-to-many direction for a :func:`_orm.relationship`.
|
|
238
|
+
|
|
239
|
+
This symbol is typically used by the internals but may be exposed within
|
|
240
|
+
certain API features.
|
|
241
|
+
|
|
242
|
+
"""
|
|
243
|
+
|
|
244
|
+
MANYTOONE = 2
|
|
245
|
+
"""Indicates the many-to-one direction for a :func:`_orm.relationship`.
|
|
246
|
+
|
|
247
|
+
This symbol is typically used by the internals but may be exposed within
|
|
248
|
+
certain API features.
|
|
249
|
+
|
|
250
|
+
"""
|
|
251
|
+
|
|
252
|
+
MANYTOMANY = 3
|
|
253
|
+
"""Indicates the many-to-many direction for a :func:`_orm.relationship`.
|
|
254
|
+
|
|
255
|
+
This symbol is typically used by the internals but may be exposed within
|
|
256
|
+
certain API features.
|
|
257
|
+
|
|
258
|
+
"""
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
ONETOMANY, MANYTOONE, MANYTOMANY = tuple(RelationshipDirection)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
class InspectionAttrExtensionType(Enum):
|
|
265
|
+
"""Symbols indicating the type of extension that a
|
|
266
|
+
:class:`.InspectionAttr` is part of."""
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
class NotExtension(InspectionAttrExtensionType):
|
|
270
|
+
NOT_EXTENSION = "not_extension"
|
|
271
|
+
"""Symbol indicating an :class:`InspectionAttr` that's
|
|
272
|
+
not part of sqlalchemy.ext.
|
|
273
|
+
|
|
274
|
+
Is assigned to the :attr:`.InspectionAttr.extension_type`
|
|
275
|
+
attribute.
|
|
276
|
+
|
|
277
|
+
"""
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
_never_set = frozenset([NEVER_SET])
|
|
281
|
+
|
|
282
|
+
_none_set = frozenset([None, NEVER_SET, PASSIVE_NO_RESULT])
|
|
283
|
+
|
|
284
|
+
_none_only_set = frozenset([None])
|
|
285
|
+
|
|
286
|
+
_SET_DEFERRED_EXPIRED = util.symbol("SET_DEFERRED_EXPIRED")
|
|
287
|
+
|
|
288
|
+
_DEFER_FOR_STATE = util.symbol("DEFER_FOR_STATE")
|
|
289
|
+
|
|
290
|
+
_RAISE_FOR_STATE = util.symbol("RAISE_FOR_STATE")
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
_F = TypeVar("_F", bound=Callable[..., Any])
|
|
294
|
+
_Self = TypeVar("_Self")
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _assertions(
|
|
298
|
+
*assertions: Any,
|
|
299
|
+
) -> Callable[[_F], _F]:
|
|
300
|
+
@util.decorator
|
|
301
|
+
def generate(fn: _F, self: _Self, *args: Any, **kw: Any) -> _Self:
|
|
302
|
+
for assertion in assertions:
|
|
303
|
+
assertion(self, fn.__name__)
|
|
304
|
+
fn(self, *args, **kw)
|
|
305
|
+
return self
|
|
306
|
+
|
|
307
|
+
return generate
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
if TYPE_CHECKING:
|
|
311
|
+
|
|
312
|
+
def manager_of_class(cls: Type[_O]) -> ClassManager[_O]: ...
|
|
313
|
+
|
|
314
|
+
@overload
|
|
315
|
+
def opt_manager_of_class(cls: AliasedClass[Any]) -> None: ...
|
|
316
|
+
|
|
317
|
+
@overload
|
|
318
|
+
def opt_manager_of_class(
|
|
319
|
+
cls: _ExternalEntityType[_O],
|
|
320
|
+
) -> Optional[ClassManager[_O]]: ...
|
|
321
|
+
|
|
322
|
+
def opt_manager_of_class(
|
|
323
|
+
cls: _ExternalEntityType[_O],
|
|
324
|
+
) -> Optional[ClassManager[_O]]: ...
|
|
325
|
+
|
|
326
|
+
def instance_state(instance: _O) -> InstanceState[_O]: ...
|
|
327
|
+
|
|
328
|
+
def instance_dict(instance: object) -> Dict[str, Any]: ...
|
|
329
|
+
|
|
330
|
+
else:
|
|
331
|
+
# these can be replaced by sqlalchemy.ext.instrumentation
|
|
332
|
+
# if augmented class instrumentation is enabled.
|
|
333
|
+
|
|
334
|
+
def manager_of_class(cls):
|
|
335
|
+
try:
|
|
336
|
+
return cls.__dict__[DEFAULT_MANAGER_ATTR]
|
|
337
|
+
except KeyError as ke:
|
|
338
|
+
raise exc.UnmappedClassError(
|
|
339
|
+
cls, f"Can't locate an instrumentation manager for class {cls}"
|
|
340
|
+
) from ke
|
|
341
|
+
|
|
342
|
+
def opt_manager_of_class(cls):
|
|
343
|
+
return cls.__dict__.get(DEFAULT_MANAGER_ATTR)
|
|
344
|
+
|
|
345
|
+
instance_state = operator.attrgetter(DEFAULT_STATE_ATTR)
|
|
346
|
+
|
|
347
|
+
instance_dict = operator.attrgetter("__dict__")
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def instance_str(instance: object) -> str:
|
|
351
|
+
"""Return a string describing an instance."""
|
|
352
|
+
|
|
353
|
+
return state_str(instance_state(instance))
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def state_str(state: InstanceState[Any]) -> str:
|
|
357
|
+
"""Return a string describing an instance via its InstanceState."""
|
|
358
|
+
|
|
359
|
+
if state is None:
|
|
360
|
+
return "None"
|
|
361
|
+
else:
|
|
362
|
+
return "<%s at 0x%x>" % (state.class_.__name__, id(state.obj()))
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def state_class_str(state: InstanceState[Any]) -> str:
|
|
366
|
+
"""Return a string describing an instance's class via its
|
|
367
|
+
InstanceState.
|
|
368
|
+
"""
|
|
369
|
+
|
|
370
|
+
if state is None:
|
|
371
|
+
return "None"
|
|
372
|
+
else:
|
|
373
|
+
return "<%s>" % (state.class_.__name__,)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
def attribute_str(instance: object, attribute: str) -> str:
|
|
377
|
+
return instance_str(instance) + "." + attribute
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def state_attribute_str(state: InstanceState[Any], attribute: str) -> str:
|
|
381
|
+
return state_str(state) + "." + attribute
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def object_mapper(instance: _T) -> Mapper[_T]:
|
|
385
|
+
"""Given an object, return the primary Mapper associated with the object
|
|
386
|
+
instance.
|
|
387
|
+
|
|
388
|
+
Raises :class:`sqlalchemy.orm.exc.UnmappedInstanceError`
|
|
389
|
+
if no mapping is configured.
|
|
390
|
+
|
|
391
|
+
This function is available via the inspection system as::
|
|
392
|
+
|
|
393
|
+
inspect(instance).mapper
|
|
394
|
+
|
|
395
|
+
Using the inspection system will raise
|
|
396
|
+
:class:`sqlalchemy.exc.NoInspectionAvailable` if the instance is
|
|
397
|
+
not part of a mapping.
|
|
398
|
+
|
|
399
|
+
"""
|
|
400
|
+
return object_state(instance).mapper
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def object_state(instance: _T) -> InstanceState[_T]:
|
|
404
|
+
"""Given an object, return the :class:`.InstanceState`
|
|
405
|
+
associated with the object.
|
|
406
|
+
|
|
407
|
+
Raises :class:`sqlalchemy.orm.exc.UnmappedInstanceError`
|
|
408
|
+
if no mapping is configured.
|
|
409
|
+
|
|
410
|
+
Equivalent functionality is available via the :func:`_sa.inspect`
|
|
411
|
+
function as::
|
|
412
|
+
|
|
413
|
+
inspect(instance)
|
|
414
|
+
|
|
415
|
+
Using the inspection system will raise
|
|
416
|
+
:class:`sqlalchemy.exc.NoInspectionAvailable` if the instance is
|
|
417
|
+
not part of a mapping.
|
|
418
|
+
|
|
419
|
+
"""
|
|
420
|
+
state = _inspect_mapped_object(instance)
|
|
421
|
+
if state is None:
|
|
422
|
+
raise exc.UnmappedInstanceError(instance)
|
|
423
|
+
else:
|
|
424
|
+
return state
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
@inspection._inspects(object)
|
|
428
|
+
def _inspect_mapped_object(instance: _T) -> Optional[InstanceState[_T]]:
|
|
429
|
+
try:
|
|
430
|
+
return instance_state(instance)
|
|
431
|
+
except (exc.UnmappedClassError,) + exc.NO_STATE:
|
|
432
|
+
return None
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def _class_to_mapper(
|
|
436
|
+
class_or_mapper: Union[Mapper[_T], Type[_T]],
|
|
437
|
+
) -> Mapper[_T]:
|
|
438
|
+
# can't get mypy to see an overload for this
|
|
439
|
+
insp = inspection.inspect(class_or_mapper, False)
|
|
440
|
+
if insp is not None:
|
|
441
|
+
return insp.mapper # type: ignore
|
|
442
|
+
else:
|
|
443
|
+
assert isinstance(class_or_mapper, type)
|
|
444
|
+
raise exc.UnmappedClassError(class_or_mapper)
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def _mapper_or_none(
|
|
448
|
+
entity: Union[Type[_T], _InternalEntityType[_T]],
|
|
449
|
+
) -> Optional[Mapper[_T]]:
|
|
450
|
+
"""Return the :class:`_orm.Mapper` for the given class or None if the
|
|
451
|
+
class is not mapped.
|
|
452
|
+
"""
|
|
453
|
+
|
|
454
|
+
# can't get mypy to see an overload for this
|
|
455
|
+
insp = inspection.inspect(entity, False)
|
|
456
|
+
if insp is not None:
|
|
457
|
+
return insp.mapper # type: ignore
|
|
458
|
+
else:
|
|
459
|
+
return None
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def _is_mapped_class(entity: Any) -> bool:
|
|
463
|
+
"""Return True if the given object is a mapped class,
|
|
464
|
+
:class:`_orm.Mapper`, or :class:`.AliasedClass`.
|
|
465
|
+
"""
|
|
466
|
+
|
|
467
|
+
insp = inspection.inspect(entity, False)
|
|
468
|
+
return (
|
|
469
|
+
insp is not None
|
|
470
|
+
and not insp.is_clause_element
|
|
471
|
+
and (insp.is_mapper or insp.is_aliased_class)
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
def _is_aliased_class(entity: Any) -> bool:
|
|
476
|
+
insp = inspection.inspect(entity, False)
|
|
477
|
+
return insp is not None and getattr(insp, "is_aliased_class", False)
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
@no_type_check
|
|
481
|
+
def _entity_descriptor(entity: _EntityType[Any], key: str) -> Any:
|
|
482
|
+
"""Return a class attribute given an entity and string name.
|
|
483
|
+
|
|
484
|
+
May return :class:`.InstrumentedAttribute` or user-defined
|
|
485
|
+
attribute.
|
|
486
|
+
|
|
487
|
+
"""
|
|
488
|
+
insp = inspection.inspect(entity)
|
|
489
|
+
if insp.is_selectable:
|
|
490
|
+
description = entity
|
|
491
|
+
entity = insp.c
|
|
492
|
+
elif insp.is_aliased_class:
|
|
493
|
+
entity = insp.entity
|
|
494
|
+
description = entity
|
|
495
|
+
elif hasattr(insp, "mapper"):
|
|
496
|
+
description = entity = insp.mapper.class_
|
|
497
|
+
else:
|
|
498
|
+
description = entity
|
|
499
|
+
|
|
500
|
+
try:
|
|
501
|
+
return getattr(entity, key)
|
|
502
|
+
except AttributeError as err:
|
|
503
|
+
raise sa_exc.InvalidRequestError(
|
|
504
|
+
"Entity '%s' has no property '%s'" % (description, key)
|
|
505
|
+
) from err
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
if TYPE_CHECKING:
|
|
509
|
+
|
|
510
|
+
def _state_mapper(state: InstanceState[_O]) -> Mapper[_O]: ...
|
|
511
|
+
|
|
512
|
+
else:
|
|
513
|
+
_state_mapper = util.dottedgetter("manager.mapper")
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def _inspect_mapped_class(
|
|
517
|
+
class_: Type[_O], configure: bool = False
|
|
518
|
+
) -> Optional[Mapper[_O]]:
|
|
519
|
+
try:
|
|
520
|
+
class_manager = opt_manager_of_class(class_)
|
|
521
|
+
if class_manager is None or not class_manager.is_mapped:
|
|
522
|
+
return None
|
|
523
|
+
mapper = class_manager.mapper
|
|
524
|
+
except exc.NO_STATE:
|
|
525
|
+
return None
|
|
526
|
+
else:
|
|
527
|
+
if configure:
|
|
528
|
+
mapper._check_configure()
|
|
529
|
+
return mapper
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
def _parse_mapper_argument(arg: Union[Mapper[_O], Type[_O]]) -> Mapper[_O]:
|
|
533
|
+
insp = inspection.inspect(arg, raiseerr=False)
|
|
534
|
+
if insp_is_mapper(insp):
|
|
535
|
+
return insp
|
|
536
|
+
|
|
537
|
+
raise sa_exc.ArgumentError(f"Mapper or mapped class expected, got {arg!r}")
|
|
538
|
+
|
|
539
|
+
|
|
540
|
+
def class_mapper(class_: Type[_O], configure: bool = True) -> Mapper[_O]:
|
|
541
|
+
"""Given a class, return the primary :class:`_orm.Mapper` associated
|
|
542
|
+
with the key.
|
|
543
|
+
|
|
544
|
+
Raises :exc:`.UnmappedClassError` if no mapping is configured
|
|
545
|
+
on the given class, or :exc:`.ArgumentError` if a non-class
|
|
546
|
+
object is passed.
|
|
547
|
+
|
|
548
|
+
Equivalent functionality is available via the :func:`_sa.inspect`
|
|
549
|
+
function as::
|
|
550
|
+
|
|
551
|
+
inspect(some_mapped_class)
|
|
552
|
+
|
|
553
|
+
Using the inspection system will raise
|
|
554
|
+
:class:`sqlalchemy.exc.NoInspectionAvailable` if the class is not mapped.
|
|
555
|
+
|
|
556
|
+
"""
|
|
557
|
+
mapper = _inspect_mapped_class(class_, configure=configure)
|
|
558
|
+
if mapper is None:
|
|
559
|
+
if not isinstance(class_, type):
|
|
560
|
+
raise sa_exc.ArgumentError(
|
|
561
|
+
"Class object expected, got '%r'." % (class_,)
|
|
562
|
+
)
|
|
563
|
+
raise exc.UnmappedClassError(class_)
|
|
564
|
+
else:
|
|
565
|
+
return mapper
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
class InspectionAttr:
|
|
569
|
+
"""A base class applied to all ORM objects and attributes that are
|
|
570
|
+
related to things that can be returned by the :func:`_sa.inspect` function.
|
|
571
|
+
|
|
572
|
+
The attributes defined here allow the usage of simple boolean
|
|
573
|
+
checks to test basic facts about the object returned.
|
|
574
|
+
|
|
575
|
+
While the boolean checks here are basically the same as using
|
|
576
|
+
the Python isinstance() function, the flags here can be used without
|
|
577
|
+
the need to import all of these classes, and also such that
|
|
578
|
+
the SQLAlchemy class system can change while leaving the flags
|
|
579
|
+
here intact for forwards-compatibility.
|
|
580
|
+
|
|
581
|
+
"""
|
|
582
|
+
|
|
583
|
+
__slots__: Tuple[str, ...] = ()
|
|
584
|
+
|
|
585
|
+
is_selectable = False
|
|
586
|
+
"""Return True if this object is an instance of
|
|
587
|
+
:class:`_expression.Selectable`."""
|
|
588
|
+
|
|
589
|
+
is_aliased_class = False
|
|
590
|
+
"""True if this object is an instance of :class:`.AliasedClass`."""
|
|
591
|
+
|
|
592
|
+
is_instance = False
|
|
593
|
+
"""True if this object is an instance of :class:`.InstanceState`."""
|
|
594
|
+
|
|
595
|
+
is_mapper = False
|
|
596
|
+
"""True if this object is an instance of :class:`_orm.Mapper`."""
|
|
597
|
+
|
|
598
|
+
is_bundle = False
|
|
599
|
+
"""True if this object is an instance of :class:`.Bundle`."""
|
|
600
|
+
|
|
601
|
+
is_property = False
|
|
602
|
+
"""True if this object is an instance of :class:`.MapperProperty`."""
|
|
603
|
+
|
|
604
|
+
is_attribute = False
|
|
605
|
+
"""True if this object is a Python :term:`descriptor`.
|
|
606
|
+
|
|
607
|
+
This can refer to one of many types. Usually a
|
|
608
|
+
:class:`.QueryableAttribute` which handles attributes events on behalf
|
|
609
|
+
of a :class:`.MapperProperty`. But can also be an extension type
|
|
610
|
+
such as :class:`.AssociationProxy` or :class:`.hybrid_property`.
|
|
611
|
+
The :attr:`.InspectionAttr.extension_type` will refer to a constant
|
|
612
|
+
identifying the specific subtype.
|
|
613
|
+
|
|
614
|
+
.. seealso::
|
|
615
|
+
|
|
616
|
+
:attr:`_orm.Mapper.all_orm_descriptors`
|
|
617
|
+
|
|
618
|
+
"""
|
|
619
|
+
|
|
620
|
+
_is_internal_proxy = False
|
|
621
|
+
"""True if this object is an internal proxy object.
|
|
622
|
+
|
|
623
|
+
.. versionadded:: 1.2.12
|
|
624
|
+
|
|
625
|
+
"""
|
|
626
|
+
|
|
627
|
+
is_clause_element = False
|
|
628
|
+
"""True if this object is an instance of
|
|
629
|
+
:class:`_expression.ClauseElement`."""
|
|
630
|
+
|
|
631
|
+
extension_type: InspectionAttrExtensionType = NotExtension.NOT_EXTENSION
|
|
632
|
+
"""The extension type, if any.
|
|
633
|
+
Defaults to :attr:`.interfaces.NotExtension.NOT_EXTENSION`
|
|
634
|
+
|
|
635
|
+
.. seealso::
|
|
636
|
+
|
|
637
|
+
:class:`.HybridExtensionType`
|
|
638
|
+
|
|
639
|
+
:class:`.AssociationProxyExtensionType`
|
|
640
|
+
|
|
641
|
+
"""
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
class InspectionAttrInfo(InspectionAttr):
|
|
645
|
+
"""Adds the ``.info`` attribute to :class:`.InspectionAttr`.
|
|
646
|
+
|
|
647
|
+
The rationale for :class:`.InspectionAttr` vs. :class:`.InspectionAttrInfo`
|
|
648
|
+
is that the former is compatible as a mixin for classes that specify
|
|
649
|
+
``__slots__``; this is essentially an implementation artifact.
|
|
650
|
+
|
|
651
|
+
"""
|
|
652
|
+
|
|
653
|
+
__slots__ = ()
|
|
654
|
+
|
|
655
|
+
@util.ro_memoized_property
|
|
656
|
+
def info(self) -> _InfoType:
|
|
657
|
+
"""Info dictionary associated with the object, allowing user-defined
|
|
658
|
+
data to be associated with this :class:`.InspectionAttr`.
|
|
659
|
+
|
|
660
|
+
The dictionary is generated when first accessed. Alternatively,
|
|
661
|
+
it can be specified as a constructor argument to the
|
|
662
|
+
:func:`.column_property`, :func:`_orm.relationship`, or
|
|
663
|
+
:func:`.composite`
|
|
664
|
+
functions.
|
|
665
|
+
|
|
666
|
+
.. seealso::
|
|
667
|
+
|
|
668
|
+
:attr:`.QueryableAttribute.info`
|
|
669
|
+
|
|
670
|
+
:attr:`.SchemaItem.info`
|
|
671
|
+
|
|
672
|
+
"""
|
|
673
|
+
return {}
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
class SQLORMOperations(SQLCoreOperations[_T_co], TypingOnly):
|
|
677
|
+
__slots__ = ()
|
|
678
|
+
|
|
679
|
+
if typing.TYPE_CHECKING:
|
|
680
|
+
|
|
681
|
+
def of_type(
|
|
682
|
+
self, class_: _EntityType[Any]
|
|
683
|
+
) -> PropComparator[_T_co]: ...
|
|
684
|
+
|
|
685
|
+
def and_(
|
|
686
|
+
self, *criteria: _ColumnExpressionArgument[bool]
|
|
687
|
+
) -> PropComparator[bool]: ...
|
|
688
|
+
|
|
689
|
+
def any( # noqa: A001
|
|
690
|
+
self,
|
|
691
|
+
criterion: Optional[_ColumnExpressionArgument[bool]] = None,
|
|
692
|
+
**kwargs: Any,
|
|
693
|
+
) -> ColumnElement[bool]: ...
|
|
694
|
+
|
|
695
|
+
def has(
|
|
696
|
+
self,
|
|
697
|
+
criterion: Optional[_ColumnExpressionArgument[bool]] = None,
|
|
698
|
+
**kwargs: Any,
|
|
699
|
+
) -> ColumnElement[bool]: ...
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
class ORMDescriptor(Generic[_T_co], TypingOnly):
|
|
703
|
+
"""Represent any Python descriptor that provides a SQL expression
|
|
704
|
+
construct at the class level."""
|
|
705
|
+
|
|
706
|
+
__slots__ = ()
|
|
707
|
+
|
|
708
|
+
if typing.TYPE_CHECKING:
|
|
709
|
+
|
|
710
|
+
@overload
|
|
711
|
+
def __get__(
|
|
712
|
+
self, instance: Any, owner: Literal[None]
|
|
713
|
+
) -> ORMDescriptor[_T_co]: ...
|
|
714
|
+
|
|
715
|
+
@overload
|
|
716
|
+
def __get__(
|
|
717
|
+
self, instance: Literal[None], owner: Any
|
|
718
|
+
) -> SQLCoreOperations[_T_co]: ...
|
|
719
|
+
|
|
720
|
+
@overload
|
|
721
|
+
def __get__(self, instance: object, owner: Any) -> _T_co: ...
|
|
722
|
+
|
|
723
|
+
def __get__(
|
|
724
|
+
self, instance: object, owner: Any
|
|
725
|
+
) -> Union[ORMDescriptor[_T_co], SQLCoreOperations[_T_co], _T_co]: ...
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
class _MappedAnnotationBase(Generic[_T_co], TypingOnly):
|
|
729
|
+
"""common class for Mapped and similar ORM container classes.
|
|
730
|
+
|
|
731
|
+
these are classes that can appear on the left side of an ORM declarative
|
|
732
|
+
mapping, containing a mapped class or in some cases a collection
|
|
733
|
+
surrounding a mapped class.
|
|
734
|
+
|
|
735
|
+
"""
|
|
736
|
+
|
|
737
|
+
__slots__ = ()
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
class SQLORMExpression(
|
|
741
|
+
SQLORMOperations[_T_co], SQLColumnExpression[_T_co], TypingOnly
|
|
742
|
+
):
|
|
743
|
+
"""A type that may be used to indicate any ORM-level attribute or
|
|
744
|
+
object that acts in place of one, in the context of SQL expression
|
|
745
|
+
construction.
|
|
746
|
+
|
|
747
|
+
:class:`.SQLORMExpression` extends from the Core
|
|
748
|
+
:class:`.SQLColumnExpression` to add additional SQL methods that are ORM
|
|
749
|
+
specific, such as :meth:`.PropComparator.of_type`, and is part of the bases
|
|
750
|
+
for :class:`.InstrumentedAttribute`. It may be used in :pep:`484` typing to
|
|
751
|
+
indicate arguments or return values that should behave as ORM-level
|
|
752
|
+
attribute expressions.
|
|
753
|
+
|
|
754
|
+
.. versionadded:: 2.0.0b4
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
"""
|
|
758
|
+
|
|
759
|
+
__slots__ = ()
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
class Mapped(
|
|
763
|
+
SQLORMExpression[_T_co],
|
|
764
|
+
ORMDescriptor[_T_co],
|
|
765
|
+
_MappedAnnotationBase[_T_co],
|
|
766
|
+
roles.DDLConstraintColumnRole,
|
|
767
|
+
):
|
|
768
|
+
"""Represent an ORM mapped attribute on a mapped class.
|
|
769
|
+
|
|
770
|
+
This class represents the complete descriptor interface for any class
|
|
771
|
+
attribute that will have been :term:`instrumented` by the ORM
|
|
772
|
+
:class:`_orm.Mapper` class. Provides appropriate information to type
|
|
773
|
+
checkers such as pylance and mypy so that ORM-mapped attributes
|
|
774
|
+
are correctly typed.
|
|
775
|
+
|
|
776
|
+
The most prominent use of :class:`_orm.Mapped` is in
|
|
777
|
+
the :ref:`Declarative Mapping <orm_explicit_declarative_base>` form
|
|
778
|
+
of :class:`_orm.Mapper` configuration, where used explicitly it drives
|
|
779
|
+
the configuration of ORM attributes such as :func:`_orm.mapped_class`
|
|
780
|
+
and :func:`_orm.relationship`.
|
|
781
|
+
|
|
782
|
+
.. seealso::
|
|
783
|
+
|
|
784
|
+
:ref:`orm_explicit_declarative_base`
|
|
785
|
+
|
|
786
|
+
:ref:`orm_declarative_table`
|
|
787
|
+
|
|
788
|
+
.. tip::
|
|
789
|
+
|
|
790
|
+
The :class:`_orm.Mapped` class represents attributes that are handled
|
|
791
|
+
directly by the :class:`_orm.Mapper` class. It does not include other
|
|
792
|
+
Python descriptor classes that are provided as extensions, including
|
|
793
|
+
:ref:`hybrids_toplevel` and the :ref:`associationproxy_toplevel`.
|
|
794
|
+
While these systems still make use of ORM-specific superclasses
|
|
795
|
+
and structures, they are not :term:`instrumented` by the
|
|
796
|
+
:class:`_orm.Mapper` and instead provide their own functionality
|
|
797
|
+
when they are accessed on a class.
|
|
798
|
+
|
|
799
|
+
.. versionadded:: 1.4
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
"""
|
|
803
|
+
|
|
804
|
+
__slots__ = ()
|
|
805
|
+
|
|
806
|
+
if typing.TYPE_CHECKING:
|
|
807
|
+
|
|
808
|
+
@overload
|
|
809
|
+
def __get__(
|
|
810
|
+
self, instance: None, owner: Any
|
|
811
|
+
) -> InstrumentedAttribute[_T_co]: ...
|
|
812
|
+
|
|
813
|
+
@overload
|
|
814
|
+
def __get__(self, instance: object, owner: Any) -> _T_co: ...
|
|
815
|
+
|
|
816
|
+
def __get__(
|
|
817
|
+
self, instance: Optional[object], owner: Any
|
|
818
|
+
) -> Union[InstrumentedAttribute[_T_co], _T_co]: ...
|
|
819
|
+
|
|
820
|
+
@classmethod
|
|
821
|
+
def _empty_constructor(cls, arg1: Any) -> Mapped[_T_co]: ...
|
|
822
|
+
|
|
823
|
+
def __set__(
|
|
824
|
+
self, instance: Any, value: Union[SQLCoreOperations[_T_co], _T_co]
|
|
825
|
+
) -> None: ...
|
|
826
|
+
|
|
827
|
+
def __delete__(self, instance: Any) -> None: ...
|
|
828
|
+
|
|
829
|
+
|
|
830
|
+
class _MappedAttribute(Generic[_T_co], TypingOnly):
|
|
831
|
+
"""Mixin for attributes which should be replaced by mapper-assigned
|
|
832
|
+
attributes.
|
|
833
|
+
|
|
834
|
+
"""
|
|
835
|
+
|
|
836
|
+
__slots__ = ()
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
class _DeclarativeMapped(Mapped[_T_co], _MappedAttribute[_T_co]):
|
|
840
|
+
"""Mixin for :class:`.MapperProperty` subclasses that allows them to
|
|
841
|
+
be compatible with ORM-annotated declarative mappings.
|
|
842
|
+
|
|
843
|
+
"""
|
|
844
|
+
|
|
845
|
+
__slots__ = ()
|
|
846
|
+
|
|
847
|
+
# MappedSQLExpression, Relationship, Composite etc. dont actually do
|
|
848
|
+
# SQL expression behavior. yet there is code that compares them with
|
|
849
|
+
# __eq__(), __ne__(), etc. Since #8847 made Mapped even more full
|
|
850
|
+
# featured including ColumnOperators, we need to have those methods
|
|
851
|
+
# be no-ops for these objects, so return NotImplemented to fall back
|
|
852
|
+
# to normal comparison behavior.
|
|
853
|
+
def operate(self, op: OperatorType, *other: Any, **kwargs: Any) -> Any:
|
|
854
|
+
return NotImplemented
|
|
855
|
+
|
|
856
|
+
__sa_operate__ = operate
|
|
857
|
+
|
|
858
|
+
def reverse_operate(
|
|
859
|
+
self, op: OperatorType, other: Any, **kwargs: Any
|
|
860
|
+
) -> Any:
|
|
861
|
+
return NotImplemented
|
|
862
|
+
|
|
863
|
+
|
|
864
|
+
class DynamicMapped(_MappedAnnotationBase[_T_co]):
|
|
865
|
+
"""Represent the ORM mapped attribute type for a "dynamic" relationship.
|
|
866
|
+
|
|
867
|
+
The :class:`_orm.DynamicMapped` type annotation may be used in an
|
|
868
|
+
:ref:`Annotated Declarative Table <orm_declarative_mapped_column>` mapping
|
|
869
|
+
to indicate that the ``lazy="dynamic"`` loader strategy should be used
|
|
870
|
+
for a particular :func:`_orm.relationship`.
|
|
871
|
+
|
|
872
|
+
.. legacy:: The "dynamic" lazy loader strategy is the legacy form of what
|
|
873
|
+
is now the "write_only" strategy described in the section
|
|
874
|
+
:ref:`write_only_relationship`.
|
|
875
|
+
|
|
876
|
+
E.g.::
|
|
877
|
+
|
|
878
|
+
class User(Base):
|
|
879
|
+
__tablename__ = "user"
|
|
880
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
881
|
+
addresses: DynamicMapped[Address] = relationship(
|
|
882
|
+
cascade="all,delete-orphan"
|
|
883
|
+
)
|
|
884
|
+
|
|
885
|
+
See the section :ref:`dynamic_relationship` for background.
|
|
886
|
+
|
|
887
|
+
.. versionadded:: 2.0
|
|
888
|
+
|
|
889
|
+
.. seealso::
|
|
890
|
+
|
|
891
|
+
:ref:`dynamic_relationship` - complete background
|
|
892
|
+
|
|
893
|
+
:class:`.WriteOnlyMapped` - fully 2.0 style version
|
|
894
|
+
|
|
895
|
+
"""
|
|
896
|
+
|
|
897
|
+
__slots__ = ()
|
|
898
|
+
|
|
899
|
+
if TYPE_CHECKING:
|
|
900
|
+
|
|
901
|
+
@overload
|
|
902
|
+
def __get__(
|
|
903
|
+
self, instance: None, owner: Any
|
|
904
|
+
) -> InstrumentedAttribute[_T_co]: ...
|
|
905
|
+
|
|
906
|
+
@overload
|
|
907
|
+
def __get__(
|
|
908
|
+
self, instance: object, owner: Any
|
|
909
|
+
) -> AppenderQuery[_T_co]: ...
|
|
910
|
+
|
|
911
|
+
def __get__(
|
|
912
|
+
self, instance: Optional[object], owner: Any
|
|
913
|
+
) -> Union[InstrumentedAttribute[_T_co], AppenderQuery[_T_co]]: ...
|
|
914
|
+
|
|
915
|
+
def __set__(
|
|
916
|
+
self, instance: Any, value: typing.Collection[_T_co]
|
|
917
|
+
) -> None: ...
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
class WriteOnlyMapped(_MappedAnnotationBase[_T_co]):
|
|
921
|
+
"""Represent the ORM mapped attribute type for a "write only" relationship.
|
|
922
|
+
|
|
923
|
+
The :class:`_orm.WriteOnlyMapped` type annotation may be used in an
|
|
924
|
+
:ref:`Annotated Declarative Table <orm_declarative_mapped_column>` mapping
|
|
925
|
+
to indicate that the ``lazy="write_only"`` loader strategy should be used
|
|
926
|
+
for a particular :func:`_orm.relationship`.
|
|
927
|
+
|
|
928
|
+
E.g.::
|
|
929
|
+
|
|
930
|
+
class User(Base):
|
|
931
|
+
__tablename__ = "user"
|
|
932
|
+
id: Mapped[int] = mapped_column(primary_key=True)
|
|
933
|
+
addresses: WriteOnlyMapped[Address] = relationship(
|
|
934
|
+
cascade="all,delete-orphan"
|
|
935
|
+
)
|
|
936
|
+
|
|
937
|
+
See the section :ref:`write_only_relationship` for background.
|
|
938
|
+
|
|
939
|
+
.. versionadded:: 2.0
|
|
940
|
+
|
|
941
|
+
.. seealso::
|
|
942
|
+
|
|
943
|
+
:ref:`write_only_relationship` - complete background
|
|
944
|
+
|
|
945
|
+
:class:`.DynamicMapped` - includes legacy :class:`_orm.Query` support
|
|
946
|
+
|
|
947
|
+
"""
|
|
948
|
+
|
|
949
|
+
__slots__ = ()
|
|
950
|
+
|
|
951
|
+
if TYPE_CHECKING:
|
|
952
|
+
|
|
953
|
+
@overload
|
|
954
|
+
def __get__(
|
|
955
|
+
self, instance: None, owner: Any
|
|
956
|
+
) -> InstrumentedAttribute[_T_co]: ...
|
|
957
|
+
|
|
958
|
+
@overload
|
|
959
|
+
def __get__(
|
|
960
|
+
self, instance: object, owner: Any
|
|
961
|
+
) -> WriteOnlyCollection[_T_co]: ...
|
|
962
|
+
|
|
963
|
+
def __get__(
|
|
964
|
+
self, instance: Optional[object], owner: Any
|
|
965
|
+
) -> Union[
|
|
966
|
+
InstrumentedAttribute[_T_co], WriteOnlyCollection[_T_co]
|
|
967
|
+
]: ...
|
|
968
|
+
|
|
969
|
+
def __set__(
|
|
970
|
+
self, instance: Any, value: typing.Collection[_T_co]
|
|
971
|
+
) -> None: ...
|