summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/mypy/apply.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2023-01-16 20:17:50 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2023-01-18 08:56:17 -0500
commit0e2b3aa323f62f46a342a8a8bba35323e214d300 (patch)
tree58d8279169e72305d5c18f338bb49e45766f6635 /lib/sqlalchemy/ext/mypy/apply.py
parentf91a25cd8191f026dd43c0a2475cda8a56d65c19 (diff)
downloadsqlalchemy-0e2b3aa323f62f46a342a8a8bba35323e214d300.tar.gz
mypy plugin fixes
Adjustments made to the mypy plugin to accommodate for some potential changes being made for issue #236 sqlalchemy2-stubs when using SQLAlchemy 1.4. These changes are being kept in sync within SQLAlchemy 2.0. The changes are also backwards compatible with older versions of sqlalchemy2-stubs. Fixed crash in mypy plugin which could occur on both 1.4 and 2.0 versions if a decorator for the :func:`_orm.registry.mapped` decorator were used that was referenced in an expression with more than two components (e.g. ``@Backend.mapper_registry.mapped``). This scenario is now ignored; when using the plugin, the decorator expression needs to be two components (i.e. ``@reg.mapped``). References: https://github.com/sqlalchemy/sqlalchemy2-stubs/issues/236 Fixes: #9102 Change-Id: Ieb1bf7bf8184645bcd43253e57f1c267b2640537
Diffstat (limited to 'lib/sqlalchemy/ext/mypy/apply.py')
-rw-r--r--lib/sqlalchemy/ext/mypy/apply.py16
1 files changed, 12 insertions, 4 deletions
diff --git a/lib/sqlalchemy/ext/mypy/apply.py b/lib/sqlalchemy/ext/mypy/apply.py
index f392a85a7..2211561b1 100644
--- a/lib/sqlalchemy/ext/mypy/apply.py
+++ b/lib/sqlalchemy/ext/mypy/apply.py
@@ -169,9 +169,6 @@ def re_apply_declarative_assignments(
update_cls_metadata = True
- # for some reason if you have a Mapped type explicitly annotated,
- # and here you set it again, mypy forgets how to do descriptors.
- # no idea. 100% feeling around in the dark to see what sticks
if (
not isinstance(left_node.type, Instance)
or left_node.type.type.fullname != NAMED_TYPE_SQLA_MAPPED
@@ -213,6 +210,12 @@ def apply_type_to_mapped_statement(
left_node = lvalue.node
assert isinstance(left_node, Var)
+ # to be completely honest I have no idea what the difference between
+ # left_node.type and stmt.type is, what it means if these are different
+ # vs. the same, why in order to get tests to pass I have to assign
+ # to stmt.type for the second case and not the first. this is complete
+ # trying every combination until it works stuff.
+
if left_hand_explicit_type is not None:
lvalue.is_inferred_def = False
left_node.type = api.named_type(
@@ -222,7 +225,9 @@ def apply_type_to_mapped_statement(
lvalue.is_inferred_def = False
left_node.type = api.named_type(
NAMED_TYPE_SQLA_MAPPED,
- [] if python_type_for_type is None else [python_type_for_type],
+ [AnyType(TypeOfAny.special_form)]
+ if python_type_for_type is None
+ else [python_type_for_type],
)
# so to have it skip the right side totally, we can do this:
@@ -239,6 +244,9 @@ def apply_type_to_mapped_statement(
# internally
stmt.rvalue = expr_to_mapped_constructor(stmt.rvalue)
+ if stmt.type is not None and python_type_for_type is not None:
+ stmt.type = python_type_for_type
+
def add_additional_orm_attributes(
cls: ClassDef,