Skip to content

Commit

Permalink
run pyupgrade
Browse files Browse the repository at this point in the history
command is:

find alembic -name "*.py" | xargs pyupgrade --py37-plus --keep-runtime-typing --keep-percent-format

I'm having some weird fighting with the tools/write_pyi, where
in different runtime contexts it keeps losing "MigrationContext"
and also Callable drops the args, but it's not consisistent.
For whatever reason, under py311 things *do* work every time.
im working w/ clean tox environments so not really sure what the
change is.  anyway, let's at least fix the quoting up
around the types.

This is towards getting the "*" in the op signatures for #1130.

Change-Id: I9175905d3b4325e03a97d6752356b70be20e9fad
  • Loading branch information
zzzeek committed Nov 26, 2022
1 parent fc451e6 commit 3b09a89
Show file tree
Hide file tree
Showing 41 changed files with 962 additions and 1,076 deletions.
58 changes: 28 additions & 30 deletions alembic/autogenerate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from alembic.script.base import ScriptDirectory


def compare_metadata(context: "MigrationContext", metadata: "MetaData") -> Any:
def compare_metadata(context: MigrationContext, metadata: MetaData) -> Any:
"""Compare a database schema to that given in a
:class:`~sqlalchemy.schema.MetaData` instance.
Expand Down Expand Up @@ -136,8 +136,8 @@ def compare_metadata(context: "MigrationContext", metadata: "MetaData") -> Any:


def produce_migrations(
context: "MigrationContext", metadata: "MetaData"
) -> "MigrationScript":
context: MigrationContext, metadata: MetaData
) -> MigrationScript:
"""Produce a :class:`.MigrationScript` structure based on schema
comparison.
Expand Down Expand Up @@ -167,13 +167,13 @@ def produce_migrations(


def render_python_code(
up_or_down_op: "UpgradeOps",
up_or_down_op: UpgradeOps,
sqlalchemy_module_prefix: str = "sa.",
alembic_module_prefix: str = "op.",
render_as_batch: bool = False,
imports: Tuple[str, ...] = (),
render_item: None = None,
migration_context: Optional["MigrationContext"] = None,
migration_context: Optional[MigrationContext] = None,
) -> str:
"""Render Python code given an :class:`.UpgradeOps` or
:class:`.DowngradeOps` object.
Expand Down Expand Up @@ -205,7 +205,7 @@ def render_python_code(


def _render_migration_diffs(
context: "MigrationContext", template_args: Dict[Any, Any]
context: MigrationContext, template_args: Dict[Any, Any]
) -> None:
"""legacy, used by test_autogen_composition at the moment"""

Expand All @@ -229,7 +229,7 @@ class AutogenContext:
"""Maintains configuration and state that's specific to an
autogenerate operation."""

metadata: Optional["MetaData"] = None
metadata: Optional[MetaData] = None
"""The :class:`~sqlalchemy.schema.MetaData` object
representing the destination.
Expand All @@ -247,7 +247,7 @@ class AutogenContext:
"""

connection: Optional["Connection"] = None
connection: Optional[Connection] = None
"""The :class:`~sqlalchemy.engine.base.Connection` object currently
connected to the database backend being compared.
Expand All @@ -256,7 +256,7 @@ class AutogenContext:
"""

dialect: Optional["Dialect"] = None
dialect: Optional[Dialect] = None
"""The :class:`~sqlalchemy.engine.Dialect` object currently in use.
This is normally obtained from the
Expand All @@ -278,13 +278,13 @@ class AutogenContext:
"""

migration_context: "MigrationContext" = None # type: ignore[assignment]
migration_context: MigrationContext = None # type: ignore[assignment]
"""The :class:`.MigrationContext` established by the ``env.py`` script."""

def __init__(
self,
migration_context: "MigrationContext",
metadata: Optional["MetaData"] = None,
migration_context: MigrationContext,
metadata: Optional[MetaData] = None,
opts: Optional[dict] = None,
autogenerate: bool = True,
) -> None:
Expand Down Expand Up @@ -342,7 +342,7 @@ def __init__(
self._has_batch: bool = False

@util.memoized_property
def inspector(self) -> "Inspector":
def inspector(self) -> Inspector:
if self.connection is None:
raise TypeError(
"can't return inspector as this "
Expand Down Expand Up @@ -397,18 +397,16 @@ def run_name_filters(
def run_object_filters(
self,
object_: Union[
"Table",
"Index",
"Column",
"UniqueConstraint",
"ForeignKeyConstraint",
Table,
Index,
Column,
UniqueConstraint,
ForeignKeyConstraint,
],
name: Optional[str],
type_: str,
reflected: bool,
compare_to: Optional[
Union["Table", "Index", "Column", "UniqueConstraint"]
],
compare_to: Optional[Union[Table, Index, Column, UniqueConstraint]],
) -> bool:
"""Run the context's object filters and return True if the targets
should be part of the autogenerate operation.
Expand Down Expand Up @@ -476,8 +474,8 @@ class RevisionContext:

def __init__(
self,
config: "Config",
script_directory: "ScriptDirectory",
config: Config,
script_directory: ScriptDirectory,
command_args: Dict[str, Any],
process_revision_directives: Optional[Callable] = None,
) -> None:
Expand All @@ -492,8 +490,8 @@ def __init__(
self.generated_revisions = [self._default_revision()]

def _to_script(
self, migration_script: "MigrationScript"
) -> Optional["Script"]:
self, migration_script: MigrationScript
) -> Optional[Script]:
template_args: Dict[str, Any] = self.template_args.copy()

if getattr(migration_script, "_needs_render", False):
Expand Down Expand Up @@ -522,19 +520,19 @@ def _to_script(
)

def run_autogenerate(
self, rev: tuple, migration_context: "MigrationContext"
self, rev: tuple, migration_context: MigrationContext
) -> None:
self._run_environment(rev, migration_context, True)

def run_no_autogenerate(
self, rev: tuple, migration_context: "MigrationContext"
self, rev: tuple, migration_context: MigrationContext
) -> None:
self._run_environment(rev, migration_context, False)

def _run_environment(
self,
rev: tuple,
migration_context: "MigrationContext",
migration_context: MigrationContext,
autogenerate: bool,
) -> None:
if autogenerate:
Expand Down Expand Up @@ -587,7 +585,7 @@ def _run_environment(
for migration_script in self.generated_revisions:
migration_script._needs_render = True

def _default_revision(self) -> "MigrationScript":
def _default_revision(self) -> MigrationScript:
command_args: Dict[str, Any] = self.command_args
op = ops.MigrationScript(
rev_id=command_args["rev_id"] or util.rev_id(),
Expand All @@ -602,6 +600,6 @@ def _default_revision(self) -> "MigrationScript":
)
return op

def generate_scripts(self) -> Iterator[Optional["Script"]]:
def generate_scripts(self) -> Iterator[Optional[Script]]:
for generated_revision in self.generated_revisions:
yield self._to_script(generated_revision)
Loading

0 comments on commit 3b09a89

Please sign in to comment.