Skip to content

Commit

Permalink
Added typing to the default script mako templates.
Browse files Browse the repository at this point in the history
Fixes: #1253
Change-Id: I00cae5daa8ebe9ba36bfac3af485a0e45a39f923
  • Loading branch information
CaselIT committed May 26, 2023
1 parent 78c23b6 commit 9705736
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
5 changes: 3 additions & 2 deletions alembic/operations/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

from ..autogenerate.rewriter import Rewriter
from ..runtime.migration import MigrationContext
from ..script.revision import _RevIdType


class MigrateOperation:
Expand Down Expand Up @@ -2667,9 +2668,9 @@ def __init__(
imports: Set[str] = set(),
head: Optional[str] = None,
splice: Optional[bool] = None,
branch_label: Optional[str] = None,
branch_label: Optional[_RevIdType] = None,
version_path: Optional[str] = None,
depends_on: Optional[Union[str, Sequence[str]]] = None,
depends_on: Optional[_RevIdType] = None,
) -> None:
self.rev_id = rev_id
self.message = message
Expand Down
8 changes: 3 additions & 5 deletions alembic/script/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,21 @@
from ..util import not_none

if TYPE_CHECKING:
from .revision import _RevIdType
from .revision import Revision
from ..config import Config
from ..config import MessagingOptions
from ..runtime.migration import RevisionStep
from ..runtime.migration import StampStep
from ..script.revision import Revision

try:
from dateutil import tz
except ImportError:
tz = None # type: ignore[assignment]

_RevIdType = Union[str, Sequence[str]]

_sourceless_rev_file = re.compile(r"(?!\.\#|__init__)(.*\.py)(c|o)?$")
_only_source_rev_file = re.compile(r"(?!\.\#|__init__)(.*\.py)$")
_legacy_rev = re.compile(r"([a-f0-9]+)\.py$")
_mod_def_re = re.compile(r"(upgrade|downgrade)_([a-z0-9]+)")
_slug_re = re.compile(r"\w+")
_default_file_template = "%(rev)s_%(slug)s"
_split_on_space_comma = re.compile(r", *|(?: +)")
Expand Down Expand Up @@ -637,7 +635,7 @@ def generate_revision(
head: Optional[str] = None,
refresh: bool = False,
splice: Optional[bool] = False,
branch_labels: Optional[str] = None,
branch_labels: Optional[_RevIdType] = None,
version_path: Optional[str] = None,
depends_on: Optional[_RevIdType] = None,
**kw: Any,
Expand Down
10 changes: 6 additions & 4 deletions alembic/templates/async/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}


def upgrade() -> None:
Expand Down
10 changes: 6 additions & 4 deletions alembic/templates/generic/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}


def upgrade() -> None:
Expand Down
10 changes: 6 additions & 4 deletions alembic/templates/multidb/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}


def upgrade(engine_name: str) -> None:
Expand Down
5 changes: 5 additions & 0 deletions docs/build/unreleased/1253.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. change::
:tags: usecase, typing
:tickets: 1253

Added typing to the default script mako templates.

0 comments on commit 9705736

Please sign in to comment.