diff options
author | mike bayer <mike_mp@zzzcomputing.com> | 2021-08-28 14:51:24 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2021-08-28 14:51:24 +0000 |
commit | af8b03ba89d6b5c5de59799280e40a8a3b2acf1c (patch) | |
tree | a3cede80b80414132e3f5b28625012835027fc81 | |
parent | f02349336fa4470dbb5ca8e4d16031b8aa86a74a (diff) | |
parent | 94dfe0dfd7f7e69588d06dcf2ca74a3fce6ad4fc (diff) | |
download | sqlalchemy-af8b03ba89d6b5c5de59799280e40a8a3b2acf1c.tar.gz |
Merge "dont assume argument lists for column property"
-rw-r--r-- | doc/build/changelog/unreleased_14/6950.rst | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/mypy/infer.py | 29 | ||||
-rw-r--r-- | lib/sqlalchemy/ext/mypy/names.py | 5 | ||||
-rw-r--r-- | test/ext/mypy/files/t_6950.py | 32 |
4 files changed, 65 insertions, 7 deletions
diff --git a/doc/build/changelog/unreleased_14/6950.rst b/doc/build/changelog/unreleased_14/6950.rst new file mode 100644 index 000000000..cdfe69e88 --- /dev/null +++ b/doc/build/changelog/unreleased_14/6950.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, mypy + :tickets: 6950 + + Fixed issue where mypy plugin would crash when interpreting a + ``query_expression()`` construct. diff --git a/lib/sqlalchemy/ext/mypy/infer.py b/lib/sqlalchemy/ext/mypy/infer.py index 85a94bba6..52570f772 100644 --- a/lib/sqlalchemy/ext/mypy/infer.py +++ b/lib/sqlalchemy/ext/mypy/infer.py @@ -284,20 +284,35 @@ def _infer_type_from_decl_column_property( """ assert isinstance(stmt.rvalue, CallExpr) - first_prop_arg = stmt.rvalue.args[0] - if isinstance(first_prop_arg, CallExpr): - type_id = names.type_id_for_callee(first_prop_arg.callee) + if stmt.rvalue.args: + first_prop_arg = stmt.rvalue.args[0] + + if isinstance(first_prop_arg, CallExpr): + type_id = names.type_id_for_callee(first_prop_arg.callee) + + # look for column_property() / deferred() etc with Column as first + # argument + if type_id is names.COLUMN: + return _infer_type_from_decl_column( + api, + stmt, + node, + left_hand_explicit_type, + right_hand_expression=first_prop_arg, + ) - # look for column_property() / deferred() etc with Column as first - # argument - if type_id is names.COLUMN: + if isinstance(stmt.rvalue, CallExpr): + type_id = names.type_id_for_callee(stmt.rvalue.callee) + # this is probably not strictly necessary as we have to use the left + # hand type for query expression in any case. any other no-arg + # column prop objects would go here also + if type_id is names.QUERY_EXPRESSION: return _infer_type_from_decl_column( api, stmt, node, left_hand_explicit_type, - right_hand_expression=first_prop_arg, ) return infer_type_from_left_hand_type_only( diff --git a/lib/sqlalchemy/ext/mypy/names.py b/lib/sqlalchemy/ext/mypy/names.py index 22a79e29b..3dbfcc770 100644 --- a/lib/sqlalchemy/ext/mypy/names.py +++ b/lib/sqlalchemy/ext/mypy/names.py @@ -45,6 +45,7 @@ MAPPER_PROPERTY: int = util.symbol("MAPPER_PROPERTY") # type: ignore AS_DECLARATIVE: int = util.symbol("AS_DECLARATIVE") # type: ignore AS_DECLARATIVE_BASE: int = util.symbol("AS_DECLARATIVE_BASE") # type: ignore DECLARATIVE_MIXIN: int = util.symbol("DECLARATIVE_MIXIN") # type: ignore +QUERY_EXPRESSION: int = util.symbol("QUERY_EXPRESSION") # type: ignore _lookup: Dict[str, Tuple[int, Set[str]]] = { "Column": ( @@ -150,6 +151,10 @@ _lookup: Dict[str, Tuple[int, Set[str]]] = { "sqlalchemy.orm.declarative_mixin", }, ), + "query_expression": ( + QUERY_EXPRESSION, + {"sqlalchemy.orm.query_expression"}, + ), } diff --git a/test/ext/mypy/files/t_6950.py b/test/ext/mypy/files/t_6950.py new file mode 100644 index 000000000..3ebbf6638 --- /dev/null +++ b/test/ext/mypy/files/t_6950.py @@ -0,0 +1,32 @@ +from typing import cast + +from sqlalchemy import Column +from sqlalchemy import Integer +from sqlalchemy.orm import declarative_base +from sqlalchemy.orm import Mapped +from sqlalchemy.orm import query_expression +from sqlalchemy.orm import Session +from sqlalchemy.orm import with_expression + +Base = declarative_base() + + +class User(Base): + __tablename__ = "users" + + id = Column(Integer, primary_key=True) + + foo = Column(Integer) + + question_count: Mapped[int] = query_expression() + answer_count: int = query_expression() + + +s = Session() + +q = s.query(User).options(with_expression(User.question_count, User.foo + 5)) + +u1: User = cast(User, q.first()) + +qc: int = u1.question_count +print(qc) |