From 30e2fdfbade3843f619c08733a9dc03dfeac49ad Mon Sep 17 00:00:00 2001 From: Sebastian Kreft <911768+sk-@users.noreply.github.com> Date: Thu, 5 Jun 2025 17:40:02 -0400 Subject: [PATCH 1/3] fix(typings): improve typing for server_default Issue #1669 shows that the typings for server default were inconsistent with sqlaclhemy's definition. In this PR we: - fix the definition of `_ServerDefault` to match that of sqlalchemy's `_ServerDefaultArgument` - use that same definition in all places that use a `server_default` or `existing_server_default` Note that the last change also changes the default of the argument `existing_server_default` from `False` to `None`. This could be a breaking change, however that argument is currently typed in manyplaces as `Optional[_ServerDefault] = None` (and `_ServerDefault` does not include `bool`). --- alembic/ddl/base.py | 4 ++-- alembic/op.pyi | 9 +++------ alembic/operations/base.py | 19 +++++++++++-------- alembic/operations/batch.py | 8 ++++++-- alembic/operations/ops.py | 23 ++++++++++++++--------- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py index ad2847eb..48ae50d9 100644 --- a/alembic/ddl/base.py +++ b/alembic/ddl/base.py @@ -29,14 +29,14 @@ from sqlalchemy import Identity from sqlalchemy.sql.compiler import Compiled from sqlalchemy.sql.compiler import DDLCompiler + from sqlalchemy.sql.elements import ColumnElement from sqlalchemy.sql.elements import TextClause - from sqlalchemy.sql.functions import Function from sqlalchemy.sql.schema import FetchedValue from sqlalchemy.sql.type_api import TypeEngine from .impl import DefaultImpl -_ServerDefault = Union["TextClause", "FetchedValue", "Function[Any]", str] +_ServerDefault = Union["FetchedValue", str, "TextClause", "ColumnElement[Any]"] class AlterTable(DDLElement): diff --git a/alembic/op.pyi b/alembic/op.pyi index 8cdf7590..7ee01882 100644 --- a/alembic/op.pyi +++ b/alembic/op.pyi @@ -29,6 +29,7 @@ if TYPE_CHECKING: from sqlalchemy.sql.expression import TableClause from sqlalchemy.sql.schema import Column from sqlalchemy.sql.schema import Computed + from sqlalchemy.sql.schema import FetchedValue from sqlalchemy.sql.schema import Identity from sqlalchemy.sql.schema import SchemaItem from sqlalchemy.sql.schema import Table @@ -154,15 +155,11 @@ def alter_column( *, nullable: Optional[bool] = None, comment: Union[str, Literal[False], None] = False, - server_default: Union[ - str, bool, Identity, Computed, TextClause, None - ] = False, + server_default: Union["FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False]] = False, new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, - existing_server_default: Union[ - str, bool, Identity, Computed, TextClause, None - ] = False, + existing_server_default: Optional[Union["FetchedValue", str, "TextClause", "ColumnElement[Any]"]] = None, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None, diff --git a/alembic/operations/base.py b/alembic/operations/base.py index 26c32724..57f28961 100644 --- a/alembic/operations/base.py +++ b/alembic/operations/base.py @@ -45,6 +45,7 @@ from sqlalchemy.sql.expression import TextClause from sqlalchemy.sql.schema import Column from sqlalchemy.sql.schema import Computed + from sqlalchemy.sql.schema import FetchedValue from sqlalchemy.sql.schema import Identity from sqlalchemy.sql.schema import SchemaItem from sqlalchemy.types import TypeEngine @@ -711,16 +712,16 @@ def alter_column( nullable: Optional[bool] = None, comment: Union[str, Literal[False], None] = False, server_default: Union[ - str, bool, Identity, Computed, TextClause, None + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] ] = False, new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[ TypeEngine[Any], Type[TypeEngine[Any]], None ] = None, - existing_server_default: Union[ - str, bool, Identity, Computed, TextClause, None - ] = False, + existing_server_default: Optional[Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ]] = None, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None, @@ -1678,15 +1679,17 @@ def alter_column( *, nullable: Optional[bool] = None, comment: Union[str, Literal[False], None] = False, - server_default: Any = False, + server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[ TypeEngine[Any], Type[TypeEngine[Any]], None ] = None, - existing_server_default: Union[ - str, bool, Identity, Computed, None - ] = False, + existing_server_default: Optional[Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ]] = None, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, insert_before: Optional[str] = None, diff --git a/alembic/operations/batch.py b/alembic/operations/batch.py index fe183e9c..3463098e 100644 --- a/alembic/operations/batch.py +++ b/alembic/operations/batch.py @@ -43,9 +43,11 @@ from sqlalchemy.engine import Dialect from sqlalchemy.sql.elements import ColumnClause + from sqlalchemy.sql.elements import ColumnElement + from sqlalchemy.sql.elements import TextClause from sqlalchemy.sql.elements import quoted_name - from sqlalchemy.sql.functions import Function from sqlalchemy.sql.schema import Constraint + from sqlalchemy.sql.schema import FetchedValue from sqlalchemy.sql.type_api import TypeEngine from ..ddl.impl import DefaultImpl @@ -485,7 +487,9 @@ def alter_column( table_name: str, column_name: str, nullable: Optional[bool] = None, - server_default: Optional[Union[Function[Any], str, bool]] = False, + server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, name: Optional[str] = None, type_: Optional[TypeEngine] = None, autoincrement: Optional[Union[bool, Literal["auto"]]] = None, diff --git a/alembic/operations/ops.py b/alembic/operations/ops.py index c9b1526b..2c657008 100644 --- a/alembic/operations/ops.py +++ b/alembic/operations/ops.py @@ -43,6 +43,7 @@ from sqlalchemy.sql.schema import Constraint from sqlalchemy.sql.schema import ForeignKeyConstraint from sqlalchemy.sql.schema import Identity + from sqlalchemy.sql.schema import FetchedValue from sqlalchemy.sql.schema import Index from sqlalchemy.sql.schema import MetaData from sqlalchemy.sql.schema import PrimaryKeyConstraint @@ -1696,7 +1697,9 @@ def __init__( *, schema: Optional[str] = None, existing_type: Optional[Any] = None, - existing_server_default: Any = False, + existing_server_default: Optional[Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]" + ]] = None, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, modify_nullable: Optional[bool] = None, @@ -1856,16 +1859,16 @@ def alter_column( nullable: Optional[bool] = None, comment: Optional[Union[str, Literal[False]]] = False, server_default: Union[ - str, bool, Identity, Computed, TextClause, None + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] ] = False, new_column_name: Optional[str] = None, type_: Optional[Union[TypeEngine[Any], Type[TypeEngine[Any]]]] = None, existing_type: Optional[ Union[TypeEngine[Any], Type[TypeEngine[Any]]] ] = None, - existing_server_default: Union[ - str, bool, Identity, Computed, TextClause, None - ] = False, + existing_server_default: Optional[Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]" + ]] = None, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None, @@ -1980,15 +1983,17 @@ def batch_alter_column( *, nullable: Optional[bool] = None, comment: Optional[Union[str, Literal[False]]] = False, - server_default: Any = False, + server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, new_column_name: Optional[str] = None, type_: Optional[Union[TypeEngine[Any], Type[TypeEngine[Any]]]] = None, existing_type: Optional[ Union[TypeEngine[Any], Type[TypeEngine[Any]]] ] = None, - existing_server_default: Optional[ - Union[str, bool, Identity, Computed] - ] = False, + existing_server_default: Optional[Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]" + ]] = None, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, insert_before: Optional[str] = None, From 96d6c0f90a9b0fe937450812e97879634eda4a05 Mon Sep 17 00:00:00 2001 From: Sebastian Kreft <911768+sk-@users.noreply.github.com> Date: Mon, 9 Jun 2025 16:24:30 -0400 Subject: [PATCH 2/3] revert default change --- alembic/op.pyi | 2 +- alembic/operations/base.py | 8 ++++---- alembic/operations/ops.py | 18 +++++++++--------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/alembic/op.pyi b/alembic/op.pyi index 7ee01882..e3bb7370 100644 --- a/alembic/op.pyi +++ b/alembic/op.pyi @@ -159,7 +159,7 @@ def alter_column( new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, - existing_server_default: Optional[Union["FetchedValue", str, "TextClause", "ColumnElement[Any]"]] = None, + existing_server_default: Union["FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False]] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None, diff --git a/alembic/operations/base.py b/alembic/operations/base.py index 57f28961..042415e6 100644 --- a/alembic/operations/base.py +++ b/alembic/operations/base.py @@ -719,9 +719,9 @@ def alter_column( existing_type: Union[ TypeEngine[Any], Type[TypeEngine[Any]], None ] = None, - existing_server_default: Optional[Union[ + existing_server_default: Union[ "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] - ]] = None, + ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None, @@ -1687,9 +1687,9 @@ def alter_column( existing_type: Union[ TypeEngine[Any], Type[TypeEngine[Any]], None ] = None, - existing_server_default: Optional[Union[ + existing_server_default: Union[ "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] - ]] = None, + ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, insert_before: Optional[str] = None, diff --git a/alembic/operations/ops.py b/alembic/operations/ops.py index 2c657008..7b678a2e 100644 --- a/alembic/operations/ops.py +++ b/alembic/operations/ops.py @@ -1697,9 +1697,9 @@ def __init__( *, schema: Optional[str] = None, existing_type: Optional[Any] = None, - existing_server_default: Optional[Union[ - "FetchedValue", str, "TextClause", "ColumnElement[Any]" - ]] = None, + existing_server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, modify_nullable: Optional[bool] = None, @@ -1866,9 +1866,9 @@ def alter_column( existing_type: Optional[ Union[TypeEngine[Any], Type[TypeEngine[Any]]] ] = None, - existing_server_default: Optional[Union[ - "FetchedValue", str, "TextClause", "ColumnElement[Any]" - ]] = None, + existing_server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None, @@ -1991,9 +1991,9 @@ def batch_alter_column( existing_type: Optional[ Union[TypeEngine[Any], Type[TypeEngine[Any]]] ] = None, - existing_server_default: Optional[Union[ - "FetchedValue", str, "TextClause", "ColumnElement[Any]" - ]] = None, + existing_server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, insert_before: Optional[str] = None, From e6464647b6e33e077e7baf4bbc5c7549ab570a06 Mon Sep 17 00:00:00 2001 From: Sebastian Kreft <911768+sk-@users.noreply.github.com> Date: Mon, 9 Jun 2025 16:26:24 -0400 Subject: [PATCH 3/3] simplify diff --- alembic/op.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/alembic/op.pyi b/alembic/op.pyi index e3bb7370..bfaff1a1 100644 --- a/alembic/op.pyi +++ b/alembic/op.pyi @@ -159,7 +159,9 @@ def alter_column( new_column_name: Optional[str] = None, type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, existing_type: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None, - existing_server_default: Union["FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False]] = False, + existing_server_default: Union[ + "FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False] + ] = False, existing_nullable: Optional[bool] = None, existing_comment: Optional[str] = None, schema: Optional[str] = None,