"""
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(
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": (
"sqlalchemy.orm.declarative_mixin",
},
),
+ "query_expression": (
+ QUERY_EXPRESSION,
+ {"sqlalchemy.orm.query_expression"},
+ ),
}
--- /dev/null
+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)