diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-13 09:45:29 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-04-15 10:29:23 -0400 |
commit | c932123bacad9bf047d160b85e3f95d396c513ae (patch) | |
tree | 3f84221c467ff8fba468d7ca78dc4b0c158d8970 /lib/sqlalchemy/sql/coercions.py | |
parent | 0bfb620009f668e97ad3c2c25a564ca36428b9ae (diff) | |
download | sqlalchemy-c932123bacad9bf047d160b85e3f95d396c513ae.tar.gz |
pep484: schema API
implement strict typing for schema.py
this module has lots of public API, lots of old decisions
and very hard to follow construction sequences in many
cases, and is also where we get a lot of new feature requests,
so strict typing should help keep things clean.
among improvements here, fixed the pool .info getters
and also figured out how to get ColumnCollection and
related to be covariant so that we may set them up
as returning Column or ColumnClause without any conflicts.
DDL was affected, noting that superclasses of DDLElement
(_DDLCompiles, added recently) can now be passed into
"ddl_if" callables; reorganized ddl into ExecutableDDLElement
as a new name for DDLElement and _DDLCompiles renamed to
BaseDDLElement.
setting up strict also located an API use case that
is completely broken, which is connection.execute(some_default)
returns a scalar value. This case has been deprecated
and new paths have been set up so that connection.scalar()
may be used. This likely wasn't possible in previous
versions because scalar() would assume a CursorResult.
The scalar() change also impacts Session as we have explicit
support (since someone had reported it as a regression)
for session.execute(Sequence()) to work. They will get the
same deprecation message (which omits the word "Connection",
just uses ".execute()" and ".scalar()") and they can then
use Session.scalar() as well. Getting this to type
correctly while still supporting ORM use cases required
some refactoring, and I also set up a keyword only delimeter
for Session.execute() and related as execution_options /
bind_arguments should always be keyword only, applied these
changes to AsyncSession as well.
Additionally simpify Table __init__ now that we are Python
3 only, we can have positional plus explicit kwargs finally.
Simplify Column.__init__ as well again taking advantage
of kw only arguments.
Fill in most/all __init__ methods in sqltypes.py as
the constructor for types is most of the API. should
likely do this for dialect-specific types as well.
Apply _InfoType for all info attributes as should have been
done originally and update descriptor decorators.
Change-Id: I3f9f8ff3f1c8858471ff4545ac83d68c88107527
Diffstat (limited to 'lib/sqlalchemy/sql/coercions.py')
-rw-r--r-- | lib/sqlalchemy/sql/coercions.py | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/lib/sqlalchemy/sql/coercions.py b/lib/sqlalchemy/sql/coercions.py index 623bb0be2..4bf45da9c 100644 --- a/lib/sqlalchemy/sql/coercions.py +++ b/lib/sqlalchemy/sql/coercions.py @@ -14,10 +14,13 @@ import typing from typing import Any from typing import Callable from typing import Dict +from typing import Iterable +from typing import Iterator from typing import List from typing import NoReturn from typing import Optional from typing import overload +from typing import Tuple from typing import Type from typing import TYPE_CHECKING from typing import TypeVar @@ -50,6 +53,7 @@ if typing.TYPE_CHECKING: from . import traversals from ._typing import _ColumnExpressionArgument from ._typing import _ColumnsClauseArgument + from ._typing import _DDLColumnArgument from ._typing import _DMLTableArgument from ._typing import _FromClauseArgument from .dml import _DMLTableElement @@ -166,19 +170,28 @@ def expect( @overload def expect( - role: Type[roles.StatementOptionRole], + role: Type[roles.DDLReferredColumnRole], element: Any, **kw: Any, -) -> DQLDMLClauseElement: +) -> Column[Any]: ... @overload def expect( - role: Type[roles.DDLReferredColumnRole], + role: Type[roles.DDLConstraintColumnRole], element: Any, **kw: Any, -) -> Column[Any]: +) -> Union[Column[Any], str]: + ... + + +@overload +def expect( + role: Type[roles.StatementOptionRole], + element: Any, + **kw: Any, +) -> DQLDMLClauseElement: ... @@ -398,21 +411,33 @@ def expect_as_key(role, element, **kw): return expect(role, element, **kw) -def expect_col_expression_collection(role, expressions): +def expect_col_expression_collection( + role: Type[roles.DDLConstraintColumnRole], + expressions: Iterable[_DDLColumnArgument], +) -> Iterator[ + Tuple[ + Union[str, Column[Any]], + Optional[ColumnClause[Any]], + Optional[str], + Optional[Union[Column[Any], str]], + ] +]: for expr in expressions: strname = None column = None - resolved = expect(role, expr) + resolved: Union[Column[Any], str] = expect(role, expr) if isinstance(resolved, str): + assert isinstance(expr, str) strname = resolved = expr else: - cols: List[ColumnClause[Any]] = [] - col_append: _TraverseCallableType[ColumnClause[Any]] = cols.append + cols: List[Column[Any]] = [] + col_append: _TraverseCallableType[Column[Any]] = cols.append visitors.traverse(resolved, {}, {"column": col_append}) if cols: column = cols[0] add_element = column if column is not None else strname + yield resolved, column, strname, add_element |