From 6ecd0ec02c5bfce322d7b358864a46b05e8348cf Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Tue, 4 Mar 2025 10:57:13 +0100 Subject: [PATCH] Overload TypeEngine.operate() when return_type is specified --- lib/sqlalchemy/sql/type_api.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/sqlalchemy/sql/type_api.py b/lib/sqlalchemy/sql/type_api.py index 19b315928a..bdc56b46ac 100644 --- a/lib/sqlalchemy/sql/type_api.py +++ b/lib/sqlalchemy/sql/type_api.py @@ -67,6 +67,7 @@ _T_con = TypeVar("_T_con", bound=Any, contravariant=True) _O = TypeVar("_O", bound=object) _TE = TypeVar("_TE", bound="TypeEngine[Any]") _CT = TypeVar("_CT", bound=Any) +_RT = TypeVar("_RT", bound=Any) _MatchedOnType = Union[ "GenericProtocol[Any]", TypeAliasType, NewType, Type[Any] @@ -186,10 +187,24 @@ class TypeEngine(Visitable, Generic[_T]): def __reduce__(self) -> Any: return self.__class__, (self.expr,) + @overload + def operate( + self, + op: OperatorType, + *other: Any, + result_type: Type[TypeEngine[_RT]], + **kwargs: Any, + ) -> ColumnElement[_RT]: ... + + @overload + def operate( + self, op: OperatorType, *other: Any, **kwargs: Any + ) -> ColumnElement[_CT]: ... + @util.preload_module("sqlalchemy.sql.default_comparator") def operate( self, op: OperatorType, *other: Any, **kwargs: Any - ) -> ColumnElement[_CT]: + ) -> ColumnElement[Any]: default_comparator = util.preloaded.sql_default_comparator op_fn, addtl_kw = default_comparator.operator_lookup[op.__name__] if kwargs: -- 2.47.3