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
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
# ext/mypy/decl_class.py
|
|
2
|
+
# Copyright (C) 2021-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
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import List
|
|
11
|
+
from typing import Optional
|
|
12
|
+
from typing import Union
|
|
13
|
+
|
|
14
|
+
from mypy.nodes import AssignmentStmt
|
|
15
|
+
from mypy.nodes import CallExpr
|
|
16
|
+
from mypy.nodes import ClassDef
|
|
17
|
+
from mypy.nodes import Decorator
|
|
18
|
+
from mypy.nodes import LambdaExpr
|
|
19
|
+
from mypy.nodes import ListExpr
|
|
20
|
+
from mypy.nodes import MemberExpr
|
|
21
|
+
from mypy.nodes import NameExpr
|
|
22
|
+
from mypy.nodes import PlaceholderNode
|
|
23
|
+
from mypy.nodes import RefExpr
|
|
24
|
+
from mypy.nodes import StrExpr
|
|
25
|
+
from mypy.nodes import SymbolNode
|
|
26
|
+
from mypy.nodes import SymbolTableNode
|
|
27
|
+
from mypy.nodes import TempNode
|
|
28
|
+
from mypy.nodes import TypeInfo
|
|
29
|
+
from mypy.nodes import Var
|
|
30
|
+
from mypy.plugin import SemanticAnalyzerPluginInterface
|
|
31
|
+
from mypy.types import AnyType
|
|
32
|
+
from mypy.types import CallableType
|
|
33
|
+
from mypy.types import get_proper_type
|
|
34
|
+
from mypy.types import Instance
|
|
35
|
+
from mypy.types import NoneType
|
|
36
|
+
from mypy.types import ProperType
|
|
37
|
+
from mypy.types import Type
|
|
38
|
+
from mypy.types import TypeOfAny
|
|
39
|
+
from mypy.types import UnboundType
|
|
40
|
+
from mypy.types import UnionType
|
|
41
|
+
|
|
42
|
+
from . import apply
|
|
43
|
+
from . import infer
|
|
44
|
+
from . import names
|
|
45
|
+
from . import util
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def scan_declarative_assignments_and_apply_types(
|
|
49
|
+
cls: ClassDef,
|
|
50
|
+
api: SemanticAnalyzerPluginInterface,
|
|
51
|
+
is_mixin_scan: bool = False,
|
|
52
|
+
) -> Optional[List[util.SQLAlchemyAttribute]]:
|
|
53
|
+
info = util.info_for_cls(cls, api)
|
|
54
|
+
|
|
55
|
+
if info is None:
|
|
56
|
+
# this can occur during cached passes
|
|
57
|
+
return None
|
|
58
|
+
elif cls.fullname.startswith("builtins"):
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
mapped_attributes: Optional[List[util.SQLAlchemyAttribute]] = (
|
|
62
|
+
util.get_mapped_attributes(info, api)
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
# used by assign.add_additional_orm_attributes among others
|
|
66
|
+
util.establish_as_sqlalchemy(info)
|
|
67
|
+
|
|
68
|
+
if mapped_attributes is not None:
|
|
69
|
+
# ensure that a class that's mapped is always picked up by
|
|
70
|
+
# its mapped() decorator or declarative metaclass before
|
|
71
|
+
# it would be detected as an unmapped mixin class
|
|
72
|
+
|
|
73
|
+
if not is_mixin_scan:
|
|
74
|
+
# mypy can call us more than once. it then *may* have reset the
|
|
75
|
+
# left hand side of everything, but not the right that we removed,
|
|
76
|
+
# removing our ability to re-scan. but we have the types
|
|
77
|
+
# here, so lets re-apply them, or if we have an UnboundType,
|
|
78
|
+
# we can re-scan
|
|
79
|
+
|
|
80
|
+
apply.re_apply_declarative_assignments(cls, api, mapped_attributes)
|
|
81
|
+
|
|
82
|
+
return mapped_attributes
|
|
83
|
+
|
|
84
|
+
mapped_attributes = []
|
|
85
|
+
|
|
86
|
+
if not cls.defs.body:
|
|
87
|
+
# when we get a mixin class from another file, the body is
|
|
88
|
+
# empty (!) but the names are in the symbol table. so use that.
|
|
89
|
+
|
|
90
|
+
for sym_name, sym in info.names.items():
|
|
91
|
+
_scan_symbol_table_entry(
|
|
92
|
+
cls, api, sym_name, sym, mapped_attributes
|
|
93
|
+
)
|
|
94
|
+
else:
|
|
95
|
+
for stmt in util.flatten_typechecking(cls.defs.body):
|
|
96
|
+
if isinstance(stmt, AssignmentStmt):
|
|
97
|
+
_scan_declarative_assignment_stmt(
|
|
98
|
+
cls, api, stmt, mapped_attributes
|
|
99
|
+
)
|
|
100
|
+
elif isinstance(stmt, Decorator):
|
|
101
|
+
_scan_declarative_decorator_stmt(
|
|
102
|
+
cls, api, stmt, mapped_attributes
|
|
103
|
+
)
|
|
104
|
+
_scan_for_mapped_bases(cls, api)
|
|
105
|
+
|
|
106
|
+
if not is_mixin_scan:
|
|
107
|
+
apply.add_additional_orm_attributes(cls, api, mapped_attributes)
|
|
108
|
+
|
|
109
|
+
util.set_mapped_attributes(info, mapped_attributes)
|
|
110
|
+
|
|
111
|
+
return mapped_attributes
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _scan_symbol_table_entry(
|
|
115
|
+
cls: ClassDef,
|
|
116
|
+
api: SemanticAnalyzerPluginInterface,
|
|
117
|
+
name: str,
|
|
118
|
+
value: SymbolTableNode,
|
|
119
|
+
attributes: List[util.SQLAlchemyAttribute],
|
|
120
|
+
) -> None:
|
|
121
|
+
"""Extract mapping information from a SymbolTableNode that's in the
|
|
122
|
+
type.names dictionary.
|
|
123
|
+
|
|
124
|
+
"""
|
|
125
|
+
value_type = get_proper_type(value.type)
|
|
126
|
+
if not isinstance(value_type, Instance):
|
|
127
|
+
return
|
|
128
|
+
|
|
129
|
+
left_hand_explicit_type = None
|
|
130
|
+
type_id = names.type_id_for_named_node(value_type.type)
|
|
131
|
+
# type_id = names._type_id_for_unbound_type(value.type.type, cls, api)
|
|
132
|
+
|
|
133
|
+
err = False
|
|
134
|
+
|
|
135
|
+
# TODO: this is nearly the same logic as that of
|
|
136
|
+
# _scan_declarative_decorator_stmt, likely can be merged
|
|
137
|
+
if type_id in {
|
|
138
|
+
names.MAPPED,
|
|
139
|
+
names.RELATIONSHIP,
|
|
140
|
+
names.COMPOSITE_PROPERTY,
|
|
141
|
+
names.MAPPER_PROPERTY,
|
|
142
|
+
names.SYNONYM_PROPERTY,
|
|
143
|
+
names.COLUMN_PROPERTY,
|
|
144
|
+
}:
|
|
145
|
+
if value_type.args:
|
|
146
|
+
left_hand_explicit_type = get_proper_type(value_type.args[0])
|
|
147
|
+
else:
|
|
148
|
+
err = True
|
|
149
|
+
elif type_id is names.COLUMN:
|
|
150
|
+
if not value_type.args:
|
|
151
|
+
err = True
|
|
152
|
+
else:
|
|
153
|
+
typeengine_arg: Union[ProperType, TypeInfo] = get_proper_type(
|
|
154
|
+
value_type.args[0]
|
|
155
|
+
)
|
|
156
|
+
if isinstance(typeengine_arg, Instance):
|
|
157
|
+
typeengine_arg = typeengine_arg.type
|
|
158
|
+
|
|
159
|
+
if isinstance(typeengine_arg, (UnboundType, TypeInfo)):
|
|
160
|
+
sym = api.lookup_qualified(typeengine_arg.name, typeengine_arg)
|
|
161
|
+
if sym is not None and isinstance(sym.node, TypeInfo):
|
|
162
|
+
if names.has_base_type_id(sym.node, names.TYPEENGINE):
|
|
163
|
+
left_hand_explicit_type = UnionType(
|
|
164
|
+
[
|
|
165
|
+
infer.extract_python_type_from_typeengine(
|
|
166
|
+
api, sym.node, []
|
|
167
|
+
),
|
|
168
|
+
NoneType(),
|
|
169
|
+
]
|
|
170
|
+
)
|
|
171
|
+
else:
|
|
172
|
+
util.fail(
|
|
173
|
+
api,
|
|
174
|
+
"Column type should be a TypeEngine "
|
|
175
|
+
"subclass not '{}'".format(sym.node.fullname),
|
|
176
|
+
value_type,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if err:
|
|
180
|
+
msg = (
|
|
181
|
+
"Can't infer type from attribute {} on class {}. "
|
|
182
|
+
"please specify a return type from this function that is "
|
|
183
|
+
"one of: Mapped[<python type>], relationship[<target class>], "
|
|
184
|
+
"Column[<TypeEngine>], MapperProperty[<python type>]"
|
|
185
|
+
)
|
|
186
|
+
util.fail(api, msg.format(name, cls.name), cls)
|
|
187
|
+
|
|
188
|
+
left_hand_explicit_type = AnyType(TypeOfAny.special_form)
|
|
189
|
+
|
|
190
|
+
if left_hand_explicit_type is not None:
|
|
191
|
+
assert value.node is not None
|
|
192
|
+
attributes.append(
|
|
193
|
+
util.SQLAlchemyAttribute(
|
|
194
|
+
name=name,
|
|
195
|
+
line=value.node.line,
|
|
196
|
+
column=value.node.column,
|
|
197
|
+
typ=left_hand_explicit_type,
|
|
198
|
+
info=cls.info,
|
|
199
|
+
)
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _scan_declarative_decorator_stmt(
|
|
204
|
+
cls: ClassDef,
|
|
205
|
+
api: SemanticAnalyzerPluginInterface,
|
|
206
|
+
stmt: Decorator,
|
|
207
|
+
attributes: List[util.SQLAlchemyAttribute],
|
|
208
|
+
) -> None:
|
|
209
|
+
"""Extract mapping information from a @declared_attr in a declarative
|
|
210
|
+
class.
|
|
211
|
+
|
|
212
|
+
E.g.::
|
|
213
|
+
|
|
214
|
+
@reg.mapped
|
|
215
|
+
class MyClass:
|
|
216
|
+
# ...
|
|
217
|
+
|
|
218
|
+
@declared_attr
|
|
219
|
+
def updated_at(cls) -> Column[DateTime]:
|
|
220
|
+
return Column(DateTime)
|
|
221
|
+
|
|
222
|
+
Will resolve in mypy as::
|
|
223
|
+
|
|
224
|
+
@reg.mapped
|
|
225
|
+
class MyClass:
|
|
226
|
+
# ...
|
|
227
|
+
|
|
228
|
+
updated_at: Mapped[Optional[datetime.datetime]]
|
|
229
|
+
|
|
230
|
+
"""
|
|
231
|
+
for dec in stmt.decorators:
|
|
232
|
+
if (
|
|
233
|
+
isinstance(dec, (NameExpr, MemberExpr, SymbolNode))
|
|
234
|
+
and names.type_id_for_named_node(dec) is names.DECLARED_ATTR
|
|
235
|
+
):
|
|
236
|
+
break
|
|
237
|
+
else:
|
|
238
|
+
return
|
|
239
|
+
|
|
240
|
+
dec_index = cls.defs.body.index(stmt)
|
|
241
|
+
|
|
242
|
+
left_hand_explicit_type: Optional[ProperType] = None
|
|
243
|
+
|
|
244
|
+
if util.name_is_dunder(stmt.name):
|
|
245
|
+
# for dunder names like __table_args__, __tablename__,
|
|
246
|
+
# __mapper_args__ etc., rewrite these as simple assignment
|
|
247
|
+
# statements; otherwise mypy doesn't like if the decorated
|
|
248
|
+
# function has an annotation like ``cls: Type[Foo]`` because
|
|
249
|
+
# it isn't @classmethod
|
|
250
|
+
any_ = AnyType(TypeOfAny.special_form)
|
|
251
|
+
left_node = NameExpr(stmt.var.name)
|
|
252
|
+
left_node.node = stmt.var
|
|
253
|
+
new_stmt = AssignmentStmt([left_node], TempNode(any_))
|
|
254
|
+
new_stmt.type = left_node.node.type
|
|
255
|
+
cls.defs.body[dec_index] = new_stmt
|
|
256
|
+
return
|
|
257
|
+
elif isinstance(stmt.func.type, CallableType):
|
|
258
|
+
func_type = stmt.func.type.ret_type
|
|
259
|
+
if isinstance(func_type, UnboundType):
|
|
260
|
+
type_id = names.type_id_for_unbound_type(func_type, cls, api)
|
|
261
|
+
else:
|
|
262
|
+
# this does not seem to occur unless the type argument is
|
|
263
|
+
# incorrect
|
|
264
|
+
return
|
|
265
|
+
|
|
266
|
+
if (
|
|
267
|
+
type_id
|
|
268
|
+
in {
|
|
269
|
+
names.MAPPED,
|
|
270
|
+
names.RELATIONSHIP,
|
|
271
|
+
names.COMPOSITE_PROPERTY,
|
|
272
|
+
names.MAPPER_PROPERTY,
|
|
273
|
+
names.SYNONYM_PROPERTY,
|
|
274
|
+
names.COLUMN_PROPERTY,
|
|
275
|
+
}
|
|
276
|
+
and func_type.args
|
|
277
|
+
):
|
|
278
|
+
left_hand_explicit_type = get_proper_type(func_type.args[0])
|
|
279
|
+
elif type_id is names.COLUMN and func_type.args:
|
|
280
|
+
typeengine_arg = func_type.args[0]
|
|
281
|
+
if isinstance(typeengine_arg, UnboundType):
|
|
282
|
+
sym = api.lookup_qualified(typeengine_arg.name, typeengine_arg)
|
|
283
|
+
if sym is not None and isinstance(sym.node, TypeInfo):
|
|
284
|
+
if names.has_base_type_id(sym.node, names.TYPEENGINE):
|
|
285
|
+
left_hand_explicit_type = UnionType(
|
|
286
|
+
[
|
|
287
|
+
infer.extract_python_type_from_typeengine(
|
|
288
|
+
api, sym.node, []
|
|
289
|
+
),
|
|
290
|
+
NoneType(),
|
|
291
|
+
]
|
|
292
|
+
)
|
|
293
|
+
else:
|
|
294
|
+
util.fail(
|
|
295
|
+
api,
|
|
296
|
+
"Column type should be a TypeEngine "
|
|
297
|
+
"subclass not '{}'".format(sym.node.fullname),
|
|
298
|
+
func_type,
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
if left_hand_explicit_type is None:
|
|
302
|
+
# no type on the decorated function. our option here is to
|
|
303
|
+
# dig into the function body and get the return type, but they
|
|
304
|
+
# should just have an annotation.
|
|
305
|
+
msg = (
|
|
306
|
+
"Can't infer type from @declared_attr on function '{}'; "
|
|
307
|
+
"please specify a return type from this function that is "
|
|
308
|
+
"one of: Mapped[<python type>], relationship[<target class>], "
|
|
309
|
+
"Column[<TypeEngine>], MapperProperty[<python type>]"
|
|
310
|
+
)
|
|
311
|
+
util.fail(api, msg.format(stmt.var.name), stmt)
|
|
312
|
+
|
|
313
|
+
left_hand_explicit_type = AnyType(TypeOfAny.special_form)
|
|
314
|
+
|
|
315
|
+
left_node = NameExpr(stmt.var.name)
|
|
316
|
+
left_node.node = stmt.var
|
|
317
|
+
|
|
318
|
+
# totally feeling around in the dark here as I don't totally understand
|
|
319
|
+
# the significance of UnboundType. It seems to be something that is
|
|
320
|
+
# not going to do what's expected when it is applied as the type of
|
|
321
|
+
# an AssignmentStatement. So do a feeling-around-in-the-dark version
|
|
322
|
+
# of converting it to the regular Instance/TypeInfo/UnionType structures
|
|
323
|
+
# we see everywhere else.
|
|
324
|
+
if isinstance(left_hand_explicit_type, UnboundType):
|
|
325
|
+
left_hand_explicit_type = get_proper_type(
|
|
326
|
+
util.unbound_to_instance(api, left_hand_explicit_type)
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
left_node.node.type = api.named_type(
|
|
330
|
+
names.NAMED_TYPE_SQLA_MAPPED, [left_hand_explicit_type]
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
# this will ignore the rvalue entirely
|
|
334
|
+
# rvalue = TempNode(AnyType(TypeOfAny.special_form))
|
|
335
|
+
|
|
336
|
+
# rewrite the node as:
|
|
337
|
+
# <attr> : Mapped[<typ>] =
|
|
338
|
+
# _sa_Mapped._empty_constructor(lambda: <function body>)
|
|
339
|
+
# the function body is maintained so it gets type checked internally
|
|
340
|
+
rvalue = names.expr_to_mapped_constructor(
|
|
341
|
+
LambdaExpr(stmt.func.arguments, stmt.func.body)
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
new_stmt = AssignmentStmt([left_node], rvalue)
|
|
345
|
+
new_stmt.type = left_node.node.type
|
|
346
|
+
|
|
347
|
+
attributes.append(
|
|
348
|
+
util.SQLAlchemyAttribute(
|
|
349
|
+
name=left_node.name,
|
|
350
|
+
line=stmt.line,
|
|
351
|
+
column=stmt.column,
|
|
352
|
+
typ=left_hand_explicit_type,
|
|
353
|
+
info=cls.info,
|
|
354
|
+
)
|
|
355
|
+
)
|
|
356
|
+
cls.defs.body[dec_index] = new_stmt
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
def _scan_declarative_assignment_stmt(
|
|
360
|
+
cls: ClassDef,
|
|
361
|
+
api: SemanticAnalyzerPluginInterface,
|
|
362
|
+
stmt: AssignmentStmt,
|
|
363
|
+
attributes: List[util.SQLAlchemyAttribute],
|
|
364
|
+
) -> None:
|
|
365
|
+
"""Extract mapping information from an assignment statement in a
|
|
366
|
+
declarative class.
|
|
367
|
+
|
|
368
|
+
"""
|
|
369
|
+
lvalue = stmt.lvalues[0]
|
|
370
|
+
if not isinstance(lvalue, NameExpr):
|
|
371
|
+
return
|
|
372
|
+
|
|
373
|
+
sym = cls.info.names.get(lvalue.name)
|
|
374
|
+
|
|
375
|
+
# this establishes that semantic analysis has taken place, which
|
|
376
|
+
# means the nodes are populated and we are called from an appropriate
|
|
377
|
+
# hook.
|
|
378
|
+
assert sym is not None
|
|
379
|
+
node = sym.node
|
|
380
|
+
|
|
381
|
+
if isinstance(node, PlaceholderNode):
|
|
382
|
+
return
|
|
383
|
+
|
|
384
|
+
assert node is lvalue.node
|
|
385
|
+
assert isinstance(node, Var)
|
|
386
|
+
|
|
387
|
+
if node.name == "__abstract__":
|
|
388
|
+
if api.parse_bool(stmt.rvalue) is True:
|
|
389
|
+
util.set_is_base(cls.info)
|
|
390
|
+
return
|
|
391
|
+
elif node.name == "__tablename__":
|
|
392
|
+
util.set_has_table(cls.info)
|
|
393
|
+
elif node.name.startswith("__"):
|
|
394
|
+
return
|
|
395
|
+
elif node.name == "_mypy_mapped_attrs":
|
|
396
|
+
if not isinstance(stmt.rvalue, ListExpr):
|
|
397
|
+
util.fail(api, "_mypy_mapped_attrs is expected to be a list", stmt)
|
|
398
|
+
else:
|
|
399
|
+
for item in stmt.rvalue.items:
|
|
400
|
+
if isinstance(item, (NameExpr, StrExpr)):
|
|
401
|
+
apply.apply_mypy_mapped_attr(cls, api, item, attributes)
|
|
402
|
+
|
|
403
|
+
left_hand_mapped_type: Optional[Type] = None
|
|
404
|
+
left_hand_explicit_type: Optional[ProperType] = None
|
|
405
|
+
|
|
406
|
+
if node.is_inferred or node.type is None:
|
|
407
|
+
if isinstance(stmt.type, UnboundType):
|
|
408
|
+
# look for an explicit Mapped[] type annotation on the left
|
|
409
|
+
# side with nothing on the right
|
|
410
|
+
|
|
411
|
+
# print(stmt.type)
|
|
412
|
+
# Mapped?[Optional?[A?]]
|
|
413
|
+
|
|
414
|
+
left_hand_explicit_type = stmt.type
|
|
415
|
+
|
|
416
|
+
if stmt.type.name == "Mapped":
|
|
417
|
+
mapped_sym = api.lookup_qualified("Mapped", cls)
|
|
418
|
+
if (
|
|
419
|
+
mapped_sym is not None
|
|
420
|
+
and mapped_sym.node is not None
|
|
421
|
+
and names.type_id_for_named_node(mapped_sym.node)
|
|
422
|
+
is names.MAPPED
|
|
423
|
+
):
|
|
424
|
+
left_hand_explicit_type = get_proper_type(
|
|
425
|
+
stmt.type.args[0]
|
|
426
|
+
)
|
|
427
|
+
left_hand_mapped_type = stmt.type
|
|
428
|
+
|
|
429
|
+
# TODO: do we need to convert from unbound for this case?
|
|
430
|
+
# left_hand_explicit_type = util._unbound_to_instance(
|
|
431
|
+
# api, left_hand_explicit_type
|
|
432
|
+
# )
|
|
433
|
+
else:
|
|
434
|
+
node_type = get_proper_type(node.type)
|
|
435
|
+
if (
|
|
436
|
+
isinstance(node_type, Instance)
|
|
437
|
+
and names.type_id_for_named_node(node_type.type) is names.MAPPED
|
|
438
|
+
):
|
|
439
|
+
# print(node.type)
|
|
440
|
+
# sqlalchemy.orm.attributes.Mapped[<python type>]
|
|
441
|
+
left_hand_explicit_type = get_proper_type(node_type.args[0])
|
|
442
|
+
left_hand_mapped_type = node_type
|
|
443
|
+
else:
|
|
444
|
+
# print(node.type)
|
|
445
|
+
# <python type>
|
|
446
|
+
left_hand_explicit_type = node_type
|
|
447
|
+
left_hand_mapped_type = None
|
|
448
|
+
|
|
449
|
+
if isinstance(stmt.rvalue, TempNode) and left_hand_mapped_type is not None:
|
|
450
|
+
# annotation without assignment and Mapped is present
|
|
451
|
+
# as type annotation
|
|
452
|
+
# equivalent to using _infer_type_from_left_hand_type_only.
|
|
453
|
+
|
|
454
|
+
python_type_for_type = left_hand_explicit_type
|
|
455
|
+
elif isinstance(stmt.rvalue, CallExpr) and isinstance(
|
|
456
|
+
stmt.rvalue.callee, RefExpr
|
|
457
|
+
):
|
|
458
|
+
python_type_for_type = infer.infer_type_from_right_hand_nameexpr(
|
|
459
|
+
api, stmt, node, left_hand_explicit_type, stmt.rvalue.callee
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
if python_type_for_type is None:
|
|
463
|
+
return
|
|
464
|
+
|
|
465
|
+
else:
|
|
466
|
+
return
|
|
467
|
+
|
|
468
|
+
assert python_type_for_type is not None
|
|
469
|
+
|
|
470
|
+
attributes.append(
|
|
471
|
+
util.SQLAlchemyAttribute(
|
|
472
|
+
name=node.name,
|
|
473
|
+
line=stmt.line,
|
|
474
|
+
column=stmt.column,
|
|
475
|
+
typ=python_type_for_type,
|
|
476
|
+
info=cls.info,
|
|
477
|
+
)
|
|
478
|
+
)
|
|
479
|
+
|
|
480
|
+
apply.apply_type_to_mapped_statement(
|
|
481
|
+
api,
|
|
482
|
+
stmt,
|
|
483
|
+
lvalue,
|
|
484
|
+
left_hand_explicit_type,
|
|
485
|
+
python_type_for_type,
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
def _scan_for_mapped_bases(
|
|
490
|
+
cls: ClassDef,
|
|
491
|
+
api: SemanticAnalyzerPluginInterface,
|
|
492
|
+
) -> None:
|
|
493
|
+
"""Given a class, iterate through its superclass hierarchy to find
|
|
494
|
+
all other classes that are considered as ORM-significant.
|
|
495
|
+
|
|
496
|
+
Locates non-mapped mixins and scans them for mapped attributes to be
|
|
497
|
+
applied to subclasses.
|
|
498
|
+
|
|
499
|
+
"""
|
|
500
|
+
|
|
501
|
+
info = util.info_for_cls(cls, api)
|
|
502
|
+
|
|
503
|
+
if info is None:
|
|
504
|
+
return
|
|
505
|
+
|
|
506
|
+
for base_info in info.mro[1:-1]:
|
|
507
|
+
if base_info.fullname.startswith("builtins"):
|
|
508
|
+
continue
|
|
509
|
+
|
|
510
|
+
# scan each base for mapped attributes. if they are not already
|
|
511
|
+
# scanned (but have all their type info), that means they are unmapped
|
|
512
|
+
# mixins
|
|
513
|
+
scan_declarative_assignments_and_apply_types(
|
|
514
|
+
base_info.defn, api, is_mixin_scan=True
|
|
515
|
+
)
|