summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/ext/mypy/apply.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2022-01-24 17:04:27 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2022-02-13 14:23:04 -0500
commite545298e35ea9f126054b337e4b5ba01988b29f7 (patch)
treee64aea159111d5921ff01f08b1c4efb667249dfe /lib/sqlalchemy/ext/mypy/apply.py
parentf1da1623b800cd4de3b71fd1b2ad5ccfde286780 (diff)
downloadsqlalchemy-e545298e35ea9f126054b337e4b5ba01988b29f7.tar.gz
establish mypy / typing approach for v2.0
large patch to get ORM / typing efforts started. this is to support adding new test cases to mypy, support dropping sqlalchemy2-stubs entirely from the test suite, validate major ORM typing reorganization to eliminate the need for the mypy plugin. * New declarative approach which uses annotation introspection, fixes: #7535 * Mapped[] is now at the base of all ORM constructs that find themselves in classes, to support direct typing without plugins * Mypy plugin updated for new typing structures * Mypy test suite broken out into "plugin" tests vs. "plain" tests, and enhanced to better support test structures where we assert that various objects are introspected by the type checker as we expect. as we go forward with typing, we will add new use cases to "plain" where we can assert that types are introspected as we expect. * For typing support, users will be much more exposed to the class names of things. Add these all to "sqlalchemy" import space. * Column(ForeignKey()) no longer needs to be `@declared_attr` if the FK refers to a remote table * composite() attributes mapped to a dataclass no longer need to implement a `__composite_values__()` method * with_variant() accepts multiple dialect names Change-Id: I22797c0be73a8fbbd2d6f5e0c0b7258b17fe145d Fixes: #7535 Fixes: #7551 References: #6810
Diffstat (limited to 'lib/sqlalchemy/ext/mypy/apply.py')
-rw-r--r--lib/sqlalchemy/ext/mypy/apply.py33
1 files changed, 22 insertions, 11 deletions
diff --git a/lib/sqlalchemy/ext/mypy/apply.py b/lib/sqlalchemy/ext/mypy/apply.py
index 99be194cd..4e244b5b9 100644
--- a/lib/sqlalchemy/ext/mypy/apply.py
+++ b/lib/sqlalchemy/ext/mypy/apply.py
@@ -36,6 +36,7 @@ from mypy.types import UnionType
from . import infer
from . import util
+from .names import expr_to_mapped_constructor
from .names import NAMED_TYPE_SQLA_MAPPED
@@ -117,6 +118,7 @@ def re_apply_declarative_assignments(
):
left_node = stmt.lvalues[0].node
+
python_type_for_type = mapped_attr_lookup[
stmt.lvalues[0].name
].type
@@ -142,7 +144,7 @@ def re_apply_declarative_assignments(
)
):
- python_type_for_type = (
+ new_python_type_for_type = (
infer.infer_type_from_right_hand_nameexpr(
api,
stmt,
@@ -152,19 +154,27 @@ def re_apply_declarative_assignments(
)
)
- if python_type_for_type is None or isinstance(
- python_type_for_type, UnboundType
+ if new_python_type_for_type is not None and not isinstance(
+ new_python_type_for_type, UnboundType
):
- continue
+ python_type_for_type = new_python_type_for_type
- # update the SQLAlchemyAttribute with the better information
- mapped_attr_lookup[
- stmt.lvalues[0].name
- ].type = python_type_for_type
+ # update the SQLAlchemyAttribute with the better
+ # information
+ mapped_attr_lookup[
+ stmt.lvalues[0].name
+ ].type = python_type_for_type
- update_cls_metadata = True
+ update_cls_metadata = True
- if python_type_for_type is not None:
+ # 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
+ ):
+ assert python_type_for_type is not None
left_node.type = api.named_type(
NAMED_TYPE_SQLA_MAPPED, [python_type_for_type]
)
@@ -202,6 +212,7 @@ def apply_type_to_mapped_statement(
assert isinstance(left_node, Var)
if left_hand_explicit_type is not None:
+ lvalue.is_inferred_def = False
left_node.type = api.named_type(
NAMED_TYPE_SQLA_MAPPED, [left_hand_explicit_type]
)
@@ -224,7 +235,7 @@ def apply_type_to_mapped_statement(
# _sa_Mapped._empty_constructor(<original CallExpr from rvalue>)
# the original right-hand side is maintained so it gets type checked
# internally
- stmt.rvalue = util.expr_to_mapped_constructor(stmt.rvalue)
+ stmt.rvalue = expr_to_mapped_constructor(stmt.rvalue)
def add_additional_orm_attributes(