From 1bec1cac731eb42e097948f84ae3d0ef133f8a9a Mon Sep 17 00:00:00 2001 From: wouter bolsterlee Date: Thu, 4 Apr 2024 11:03:06 +0200 Subject: [PATCH] typing: annotate Exists.select() to return Select[bool] MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fixes: #11231 A query of the form: SELECT EXISTS ( SELECT 1 FROM ... WHERE ... ) … returns a boolean. --- lib/sqlalchemy/sql/selectable.py | 2 +- test/typing/plain_files/sql/common_sql_element.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 4ae60b7724..c28d4df0e4 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -6674,7 +6674,7 @@ class Exists(UnaryExpression[bool]): assert isinstance(return_value, SelectStatementGrouping) return return_value - def select(self) -> Select[Unpack[TupleAny]]: + def select(self) -> Select[bool]: r"""Return a SELECT of this :class:`_expression.Exists`. e.g.:: diff --git a/test/typing/plain_files/sql/common_sql_element.py b/test/typing/plain_files/sql/common_sql_element.py index 89c0c4d2ef..7c8001a728 100644 --- a/test/typing/plain_files/sql/common_sql_element.py +++ b/test/typing/plain_files/sql/common_sql_element.py @@ -98,6 +98,11 @@ stmt2 = ( # EXPECTED_TYPE: Select[int] reveal_type(stmt2) +stmt3 = select(User.id).exists().select() + +# EXPECTED_TYPE: Select[bool] +reveal_type(stmt3) + receives_str_col_expr(User.email) receives_str_col_expr(User.email + "some expr") -- 2.47.2