From 43f6ae639ca0186f4802255861acdc20f19e702f Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 2 Jan 2022 17:35:43 -0500 Subject: initial reorganize for static typing start applying foundational annotations to key elements. two main elements addressed here: 1. removal of public_factory() and replacement with explicit functions. this just works much better with typing. 2. typing support for column expressions and operators. The biggest part of this involves stubbing out all the ColumnOperators methods under ColumnElement in a TYPE_CHECKING section. Took me a while to see this method vs. much more complicated things I thought I needed. Also for this version implementing #7519, ColumnElement types against the Python type and not TypeEngine. it is hoped this leads to easier transferrence between ORM/Core as well as eventual support for result set typing. Not clear yet how well this approach will work and what new issues it may introduce. given the current approach we now get full, rich typing for scenarios like this: from sqlalchemy import column, Integer, String, Boolean c1 = column('a', String) c2 = column('a', Integer) expr1 = c2.in_([1, 2, 3]) expr2 = c2 / 5 expr3 = -c2 expr4_a = ~(c2 == 5) expr4_b = ~column('q', Boolean) expr5 = c1 + 'x' expr6 = c2 + 10 Fixes: #7519 Fixes: #6810 Change-Id: I078d9f57955549f6f7868314287175f6c61c44cb --- lib/sqlalchemy/sql/expression.py | 394 ++++++++++++++------------------------- 1 file changed, 137 insertions(+), 257 deletions(-) (limited to 'lib/sqlalchemy/sql/expression.py') diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 54f67b930..680eae754 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -10,263 +10,143 @@ """ -__all__ = [ - "Alias", - "AliasedReturnsRows", - "any_", - "all_", - "CacheKey", - "ClauseElement", - "ColumnCollection", - "ColumnElement", - "CompoundSelect", - "Delete", - "FromClause", - "Insert", - "Join", - "Lateral", - "LambdaElement", - "StatementLambdaElement", - "Select", - "Selectable", - "TableClause", - "TableValuedAlias", - "Update", - "Values", - "alias", - "and_", - "asc", - "between", - "bindparam", - "case", - "cast", - "column", - "custom_op", - "cte", - "delete", - "desc", - "distinct", - "except_", - "except_all", - "exists", - "extract", - "func", - "modifier", - "collate", - "insert", - "intersect", - "intersect_all", - "join", - "label", - "lateral", - "lambda_stmt", - "literal", - "literal_column", - "not_", - "null", - "nulls_first", - "nulls_last", - "or_", - "outparam", - "outerjoin", - "over", - "select", - "table", - "text", - "tuple_", - "type_coerce", - "quoted_name", - "union", - "union_all", - "update", - "quoted_name", - "within_group", - "Subquery", - "TableSample", - "tablesample", - "values", -] +from ._dml_constructors import delete as delete +from ._dml_constructors import insert as insert +from ._dml_constructors import update as update +from ._elements_constructors import all_ as all_ +from ._elements_constructors import and_ as and_ +from ._elements_constructors import any_ as any_ +from ._elements_constructors import asc as asc +from ._elements_constructors import between as between +from ._elements_constructors import bindparam as bindparam +from ._elements_constructors import case as case +from ._elements_constructors import cast as cast +from ._elements_constructors import collate as collate +from ._elements_constructors import column as column +from ._elements_constructors import desc as desc +from ._elements_constructors import distinct as distinct +from ._elements_constructors import extract as extract +from ._elements_constructors import false as false +from ._elements_constructors import funcfilter as funcfilter +from ._elements_constructors import label as label +from ._elements_constructors import not_ as not_ +from ._elements_constructors import null as null +from ._elements_constructors import nulls_first as nulls_first +from ._elements_constructors import nulls_last as nulls_last +from ._elements_constructors import or_ as or_ +from ._elements_constructors import outparam as outparam +from ._elements_constructors import over as over +from ._elements_constructors import text as text +from ._elements_constructors import true as true +from ._elements_constructors import tuple_ as tuple_ +from ._elements_constructors import type_coerce as type_coerce +from ._elements_constructors import typing as typing +from ._elements_constructors import within_group as within_group +from ._selectable_constructors import alias as alias +from ._selectable_constructors import cte as cte +from ._selectable_constructors import except_ as except_ +from ._selectable_constructors import except_all as except_all +from ._selectable_constructors import exists as exists +from ._selectable_constructors import intersect as intersect +from ._selectable_constructors import intersect_all as intersect_all +from ._selectable_constructors import join as join +from ._selectable_constructors import lateral as lateral +from ._selectable_constructors import outerjoin as outerjoin +from ._selectable_constructors import select as select +from ._selectable_constructors import table as table +from ._selectable_constructors import tablesample as tablesample +from ._selectable_constructors import union as union +from ._selectable_constructors import union_all as union_all +from ._selectable_constructors import values as values +from .base import _from_objects as _from_objects +from .base import _select_iterables as _select_iterables +from .base import ColumnCollection as ColumnCollection +from .base import Executable as Executable +from .cache_key import CacheKey as CacheKey +from .dml import Delete as Delete +from .dml import Insert as Insert +from .dml import Update as Update +from .dml import UpdateBase as UpdateBase +from .dml import ValuesBase as ValuesBase +from .elements import _truncated_label as _truncated_label +from .elements import BinaryExpression as BinaryExpression +from .elements import BindParameter as BindParameter +from .elements import BooleanClauseList as BooleanClauseList +from .elements import Case as Case +from .elements import Cast as Cast +from .elements import ClauseElement as ClauseElement +from .elements import ClauseList as ClauseList +from .elements import CollectionAggregate as CollectionAggregate +from .elements import ColumnClause as ColumnClause +from .elements import ColumnElement as ColumnElement +from .elements import Extract as Extract +from .elements import False_ as False_ +from .elements import FunctionFilter as FunctionFilter +from .elements import Grouping as Grouping +from .elements import Label as Label +from .elements import literal as literal +from .elements import literal_column as literal_column +from .elements import Null as Null +from .elements import Over as Over +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 TextClause as TextClause +from .elements import True_ as True_ +from .elements import Tuple as Tuple +from .elements import TypeClause as TypeClause +from .elements import TypeCoerce as TypeCoerce +from .elements import UnaryExpression as UnaryExpression +from .elements import WithinGroup as WithinGroup +from .functions import func as func +from .functions import Function as Function +from .functions import FunctionElement as FunctionElement +from .functions import modifier as modifier +from .lambdas import lambda_stmt as lambda_stmt +from .lambdas import LambdaElement as LambdaElement +from .lambdas import StatementLambdaElement as StatementLambdaElement +from .operators import ColumnOperators as ColumnOperators +from .operators import custom_op as custom_op +from .operators import Operators as Operators +from .selectable import Alias as Alias +from .selectable import AliasedReturnsRows as AliasedReturnsRows +from .selectable import CompoundSelect as CompoundSelect +from .selectable import CTE as CTE +from .selectable import Exists as Exists +from .selectable import FromClause as FromClause +from .selectable import FromGrouping as FromGrouping +from .selectable import GenerativeSelect as GenerativeSelect +from .selectable import HasCTE as HasCTE +from .selectable import HasPrefixes as HasPrefixes +from .selectable import HasSuffixes as HasSuffixes +from .selectable import Join as Join +from .selectable import LABEL_STYLE_DEFAULT as LABEL_STYLE_DEFAULT +from .selectable import LABEL_STYLE_NONE as LABEL_STYLE_NONE +from .selectable import Lateral as Lateral +from .selectable import ReturnsRows as ReturnsRows +from .selectable import ScalarSelect as ScalarSelect +from .selectable import Select as Select +from .selectable import Selectable as Selectable +from .selectable import SelectBase as SelectBase +from .selectable import Subquery as Subquery +from .selectable import TableClause as TableClause +from .selectable import TableSample as TableSample +from .selectable import TableValuedAlias as TableValuedAlias +from .selectable import TextAsFrom as TextAsFrom +from .selectable import TextualSelect as TextualSelect +from .selectable import Values as Values +from .visitors import Visitable as Visitable -from typing import Callable +if True: + # work around zimports + from .selectable import ( + LABEL_STYLE_DISAMBIGUATE_ONLY as LABEL_STYLE_DISAMBIGUATE_ONLY, + ) + from .selectable import ( + LABEL_STYLE_TABLENAME_PLUS_COL as LABEL_STYLE_TABLENAME_PLUS_COL, + ) -from .base import _from_objects -from .base import _select_iterables -from .base import ColumnCollection -from .base import Executable -from .cache_key import CacheKey -from .dml import Delete -from .dml import Insert -from .dml import Update -from .dml import UpdateBase -from .dml import ValuesBase -from .elements import _truncated_label -from .elements import between -from .elements import BinaryExpression -from .elements import BindParameter -from .elements import BooleanClauseList -from .elements import Case -from .elements import Cast -from .elements import ClauseElement -from .elements import ClauseList -from .elements import collate -from .elements import CollectionAggregate -from .elements import ColumnClause -from .elements import ColumnElement -from .elements import Extract -from .elements import False_ -from .elements import FunctionFilter -from .elements import Grouping -from .elements import Label -from .elements import literal -from .elements import literal_column -from .elements import not_ -from .elements import Null -from .elements import outparam -from .elements import Over -from .elements import quoted_name -from .elements import ReleaseSavepointClause -from .elements import RollbackToSavepointClause -from .elements import SavepointClause -from .elements import TextClause -from .elements import True_ -from .elements import Tuple -from .elements import TypeClause -from .elements import TypeCoerce -from .elements import UnaryExpression -from .elements import WithinGroup -from .functions import func -from .functions import Function -from .functions import FunctionElement -from .functions import modifier -from .lambdas import lambda_stmt -from .lambdas import LambdaElement -from .lambdas import StatementLambdaElement -from .operators import ColumnOperators -from .operators import custom_op -from .operators import Operators -from .selectable import Alias -from .selectable import AliasedReturnsRows -from .selectable import CompoundSelect -from .selectable import CTE -from .selectable import Exists -from .selectable import FromClause -from .selectable import FromGrouping -from .selectable import GenerativeSelect -from .selectable import HasCTE -from .selectable import HasPrefixes -from .selectable import HasSuffixes -from .selectable import Join -from .selectable import LABEL_STYLE_DEFAULT -from .selectable import LABEL_STYLE_DISAMBIGUATE_ONLY -from .selectable import LABEL_STYLE_NONE -from .selectable import LABEL_STYLE_TABLENAME_PLUS_COL -from .selectable import Lateral -from .selectable import ReturnsRows -from .selectable import ScalarSelect -from .selectable import Select -from .selectable import Selectable -from .selectable import SelectBase -from .selectable import Subquery -from .selectable import TableClause -from .selectable import TableSample -from .selectable import TableValuedAlias -from .selectable import TextAsFrom -from .selectable import TextualSelect -from .selectable import Values -from .visitors import Visitable -from ..util.langhelpers import public_factory - -# TODO: proposal is to remove public_factory and replace with traditional -# functions exported here. - -all_ = public_factory(CollectionAggregate._create_all, ".sql.expression.all_") -any_ = public_factory(CollectionAggregate._create_any, ".sql.expression.any_") -and_ = public_factory(BooleanClauseList.and_, ".sql.expression.and_") -alias = public_factory(Alias._factory, ".sql.expression.alias") -tablesample = public_factory( - TableSample._factory, ".sql.expression.tablesample" -) -lateral = public_factory(Lateral._factory, ".sql.expression.lateral") -or_ = public_factory(BooleanClauseList.or_, ".sql.expression.or_") -bindparam = public_factory(BindParameter, ".sql.expression.bindparam") -select = public_factory(Select._create, ".sql.expression.select") -text = public_factory(TextClause._create_text, ".sql.expression.text") -table = public_factory(TableClause, ".sql.expression.table") -column = public_factory(ColumnClause, ".sql.expression.column") -over = public_factory(Over, ".sql.expression.over") -within_group = public_factory(WithinGroup, ".sql.expression.within_group") -label = public_factory(Label, ".sql.expression.label") -case = public_factory(Case, ".sql.expression.case") -cast = public_factory(Cast, ".sql.expression.cast") -cte = public_factory(CTE._factory, ".sql.expression.cte") -values = public_factory(Values, ".sql.expression.values") -extract = public_factory(Extract, ".sql.expression.extract") -tuple_ = public_factory(Tuple, ".sql.expression.tuple_") -except_ = public_factory( - CompoundSelect._create_except, ".sql.expression.except_" -) -except_all = public_factory( - CompoundSelect._create_except_all, ".sql.expression.except_all" -) -intersect = public_factory( - CompoundSelect._create_intersect, ".sql.expression.intersect" -) -intersect_all = public_factory( - CompoundSelect._create_intersect_all, ".sql.expression.intersect_all" -) -union = public_factory(CompoundSelect._create_union, ".sql.expression.union") -union_all = public_factory( - CompoundSelect._create_union_all, ".sql.expression.union_all" -) -exists = public_factory(Exists, ".sql.expression.exists") -nulls_first = public_factory( - UnaryExpression._create_nulls_first, ".sql.expression.nulls_first" -) -nullsfirst = nulls_first # deprecated 1.4; see #5435 -nulls_last = public_factory( - UnaryExpression._create_nulls_last, ".sql.expression.nulls_last" -) -nullslast = nulls_last # deprecated 1.4; see #5435 -asc = public_factory(UnaryExpression._create_asc, ".sql.expression.asc") -desc = public_factory(UnaryExpression._create_desc, ".sql.expression.desc") -distinct = public_factory( - UnaryExpression._create_distinct, ".sql.expression.distinct" -) -type_coerce = public_factory(TypeCoerce, ".sql.expression.type_coerce") -true = public_factory(True_._instance, ".sql.expression.true") -false = public_factory(False_._instance, ".sql.expression.false") -null = public_factory(Null._instance, ".sql.expression.null") -join = public_factory(Join._create_join, ".sql.expression.join") -outerjoin = public_factory(Join._create_outerjoin, ".sql.expression.outerjoin") -insert = public_factory(Insert, ".sql.expression.insert") -update = public_factory(Update, ".sql.expression.update") -delete = public_factory(Delete, ".sql.expression.delete") -funcfilter = public_factory(FunctionFilter, ".sql.expression.funcfilter") - - -# internal functions still being called from tests and the ORM, -# these might be better off in some other namespace - - -# old names for compatibility -_Executable = Executable -_BindParamClause = BindParameter -_Label = Label -_SelectBase = SelectBase -_BinaryExpression = BinaryExpression -_Cast = Cast -_Null = Null -_False = False_ -_True = True_ -_TextClause = TextClause -_UnaryExpression = UnaryExpression -_Case = Case -_Tuple = Tuple -_Over = Over -_TypeClause = TypeClause -_Extract = Extract -_Exists = Exists -_Grouping = Grouping -_FromGrouping = FromGrouping -_ScalarSelect = ScalarSelect +nullsfirst = nulls_first +nullslast = nulls_last -- cgit v1.2.1