From caa9293e2e0d0b186a24962ad72b954271934913 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 19 Nov 2022 16:42:22 -0500 Subject: add common base class for all SQL col expression objects Added a new type :class:`.SQLColumnExpression` which may be indicated in user code to represent any SQL column oriented expression, including both those based on :class:`.ColumnElement` as well as on ORM :class:`.QueryableAttribute`. This type is a real class, not an alias, so can also be used as the foundation for other objects. Fixes: #8847 Change-Id: I3161bdff1c9f447793fce87864e1774a90cd4146 --- lib/sqlalchemy/sql/__init__.py | 1 + lib/sqlalchemy/sql/elements.py | 22 +++++++++++++++++++++- lib/sqlalchemy/sql/expression.py | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/__init__.py b/lib/sqlalchemy/sql/__init__.py index 5702d6c25..8dae9c3f5 100644 --- a/lib/sqlalchemy/sql/__init__.py +++ b/lib/sqlalchemy/sql/__init__.py @@ -82,6 +82,7 @@ from .expression import Select as Select from .expression import select as select from .expression import Selectable as Selectable from .expression import SelectLabelStyle as SelectLabelStyle +from .expression import SQLColumnExpression as SQLColumnExpression from .expression import StatementLambdaElement as StatementLambdaElement from .expression import Subquery as Subquery from .expression import table as table diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index d9a1a9358..914d2b326 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -1115,6 +1115,26 @@ class SQLCoreOperations(Generic[_T], ColumnOperators, TypingOnly): ... +class SQLColumnExpression( + SQLCoreOperations[_T], roles.ExpressionElementRole[_T], TypingOnly +): + """A type that may be used to indicate any SQL column element or object + that acts in place of one. + + :class:`.SQLColumnExpression` is a base of + :class:`.ColumnElement`, as well as within the bases of ORM elements + such as :class:`.InstrumentedAttribute`, and may be used in :pep:`484` + typing to indicate arguments or return values that should behave + as column expressions. + + .. versionadded:: 2.0.0b4 + + + """ + + __slots__ = () + + _SQO = SQLCoreOperations SelfColumnElement = TypeVar("SelfColumnElement", bound="ColumnElement[Any]") @@ -1131,7 +1151,7 @@ class ColumnElement( roles.DMLColumnRole, roles.DDLConstraintColumnRole, roles.DDLExpressionRole, - SQLCoreOperations[_T], + SQLColumnExpression[_T], DQLDMLClauseElement, ): """Represent a column-oriented SQL expression suitable for usage in the diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index d08bbf4eb..2498bfb37 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -95,6 +95,7 @@ from .elements import quoted_name as quoted_name from .elements import ReleaseSavepointClause as ReleaseSavepointClause from .elements import RollbackToSavepointClause as RollbackToSavepointClause from .elements import SavepointClause as SavepointClause +from .elements import SQLColumnExpression as SQLColumnExpression from .elements import TextClause as TextClause from .elements import True_ as True_ from .elements import Tuple as Tuple -- cgit v1.2.1