Skip to content

Commit

Permalink
match process_revision_directives typing to API
Browse files Browse the repository at this point in the history
Improved typing in the
:paramref:`.EnvironmentContext.configure.process_revision_directives`
callable to better indicate that the passed-in type is
:class:`.MigrationScript`, not the :class:`.MigrationOperation` base class,
and added typing to the example at :ref:`cookbook_no_empty_migrations` to
illustrate.

Change-Id: Ibfb7a57a081818c290cf0964d12a72b85c2c1983
Fixes: #1325
  • Loading branch information
zzzeek committed Oct 17, 2023
1 parent b37d3d0 commit 3e48ed0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions alembic/autogenerate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Callable
from typing import Dict
from typing import Iterator
from typing import List
from typing import Optional
from typing import Sequence
from typing import Set
Expand Down Expand Up @@ -508,6 +509,8 @@ class RevisionContext:
"""Maintains configuration and state that's specific to a revision
file generation operation."""

generated_revisions: List[MigrationScript]

def __init__(
self,
config: Config,
Expand Down
4 changes: 2 additions & 2 deletions alembic/context.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if TYPE_CHECKING:

from .autogenerate.api import AutogenContext
from .config import Config
from .operations.ops import MigrateOperation
from .operations.ops import MigrationScript
from .runtime.migration import _ProxyTransaction
from .runtime.migration import MigrationContext
from .runtime.migration import MigrationInfo
Expand Down Expand Up @@ -143,7 +143,7 @@ def configure(
include_schemas: bool = False,
process_revision_directives: Optional[
Callable[
[MigrationContext, Tuple[str, str], List[MigrateOperation]], None
[MigrationContext, Tuple[str, str], List[MigrationScript]], None
]
] = None,
compare_type: Union[
Expand Down
2 changes: 2 additions & 0 deletions alembic/operations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from .base import BatchOperations
from .base import Operations
from .ops import MigrateOperation
from .ops import MigrationScript


__all__ = [
"AbstractOperations",
"Operations",
"BatchOperations",
"MigrateOperation",
"MigrationScript",
]
4 changes: 2 additions & 2 deletions alembic/runtime/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
from ..autogenerate.api import AutogenContext
from ..config import Config
from ..ddl import DefaultImpl
from ..operations.ops import MigrateOperation
from ..operations.ops import MigrationScript
from ..script.base import ScriptDirectory

_RevNumber = Optional[Union[str, Tuple[str, ...]]]

ProcessRevisionDirectiveFn = Callable[
[MigrationContext, Tuple[str, str], List["MigrateOperation"]], None
[MigrationContext, Tuple[str, str], List["MigrationScript"]], None
]

RenderItemFn = Callable[
Expand Down
14 changes: 13 additions & 1 deletion docs/build/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -900,14 +900,26 @@ hook in :meth:`.MigrationContext.configure` which removes the
single :class:`.MigrationScript` directive if it is empty of
any operations::

# for typing purposes
from alembic.environment import MigrationContext

# this typing-only import requires alembic 1.12.1 or above

This comment has been minimized.

Copy link
@olafurnielsen

olafurnielsen Oct 24, 2023

Alembic 1.12.1 hasn't been released, right?

This comment has been minimized.

Copy link
@zzzeek

zzzeek Oct 24, 2023

Author Member

you can check at https://alembic.sqlalchemy.org/en/latest/changelog.html.

it looks like indeed we've accumultated a lot of fixes, wasnt aware of that

from alembic.operations import MigrationScript


def run_migrations_online():

# ...

def process_revision_directives(context, revision, directives):
def process_revision_directives(
context: MigrationContext,
revision: tuple[str, str],
directives: list[MigrationScript],
):
assert config.cmd_opts is not None
if config.cmd_opts.autogenerate:
script = directives[0]
assert script.upgrade_ops is not None
if script.upgrade_ops.is_empty():
directives[:] = []

Expand Down
10 changes: 10 additions & 0 deletions docs/build/unreleased/1325.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. change::
:tags: bug, typing
:tickets: 1325

Improved typing in the
:paramref:`.EnvironmentContext.configure.process_revision_directives`
callable to better indicate that the passed-in type is
:class:`.MigrationScript`, not the :class:`.MigrationOperation` base class,
and added typing to the example at :ref:`cookbook_no_empty_migrations` to
illustrate.

0 comments on commit 3e48ed0

Please sign in to comment.