Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions alembic/ddl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 3 additions & 6 deletions alembic/op.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 11 additions & 8 deletions alembic/operations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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[
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CaselIT I made this change to be consistent with other typings of the same argument (existing_server_default: Optional[_ServerDefault] = None). However I understand that it could potentially break something. Let me know what you prefer and if I could revert the default to False.

I'm also not sure if you prefer Optional[Union[...]] or Union[..., None], as both styles are used in the codebase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

false is sometimes used in alembic to mean "no value" when none may be a valid value, so it's likely better if we kept the previous behaviour. I'll run the ci to see if anything breaks though

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted those changes. However, that breaks typings in another part:

$ mypy ./alembic/ --exclude alembic/templates
alembic/operations/toimpl.py:62: error: Argument "existing_server_default" to "alter_column" of "DefaultImpl" has incompatible type "FetchedValue | str | TextClause | ColumnElement[Any] | Literal[False] | None"; expected "FetchedValue | str | TextClause | ColumnElement[Any] | None"  [arg-type]
Found 1 error in 1 file (checked 43 source files)

existing_server_default in alter_column is currently typed as existing_server_default: Optional[_ServerDefault] = None, but in the other parts of the code it can be False.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look

"FetchedValue", str, "TextClause", "ColumnElement[Any]", None, Literal[False]
]] = None,
existing_nullable: Optional[bool] = None,
existing_comment: Optional[str] = None,
schema: Optional[str] = None,
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions alembic/operations/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
23 changes: 14 additions & 9 deletions alembic/operations/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading