From: Mike Bayer Date: Thu, 16 Jul 2026 16:58:30 +0000 (-0400) Subject: update for latest mypy; update to black 26.5.1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7b2af57eba318a712b7b4f79c7bc6b3669055636;p=thirdparty%2Fsqlalchemy%2Falembic.git update for latest mypy; update to black 26.5.1 Change-Id: Ifcc7e9686f233381087dba6aaeba75da4eca8035 --- diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5f45d054..af478101 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/python/black - rev: 25.9.0 + rev: 26.5.1 hooks: - id: black diff --git a/alembic/__init__.py b/alembic/__init__.py index 2d8ea2d3..f41cb208 100644 --- a/alembic/__init__.py +++ b/alembic/__init__.py @@ -2,5 +2,4 @@ from . import context from . import op from .runtime import plugins - __version__ = "1.18.6" diff --git a/alembic/autogenerate/render.py b/alembic/autogenerate/render.py index 4cae1cf3..f202c6c8 100644 --- a/alembic/autogenerate/render.py +++ b/alembic/autogenerate/render.py @@ -754,7 +754,7 @@ def _render_column( opts: List[Tuple[str, Any]] = [] if column.server_default: - rendered = _render_server_default( # type:ignore[assignment] + rendered = _render_server_default( # type: ignore[assignment] column.server_default, autogen_context ) if rendered: diff --git a/alembic/command.py b/alembic/command.py index 2cb94ad1..0f374f2a 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -442,7 +442,7 @@ def merge( head=revisions, branch_labels=branch_label, splice=splice, - **template_args, # type:ignore[arg-type] + **template_args, # type: ignore[arg-type] ) diff --git a/alembic/config.py b/alembic/config.py index 121a4459..a5f6406f 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -28,7 +28,6 @@ from . import util from .util import compat from .util.pyfiles import _preserving_path_as_str - log = logging.getLogger(__name__) @@ -180,7 +179,7 @@ class Config: return None return Path(self.toml_file_name) - config_ini_section: str = None # type:ignore[assignment] + config_ini_section: str = None # type: ignore[assignment] """Name of the config file section to read basic configuration from. Defaults to ``alembic``, that is the ``[alembic]`` section of the .ini file. This value is modified using the ``-n/--name`` @@ -927,11 +926,11 @@ class CommandLine: if arg in self._KWARGS_OPTS: kwarg_opt = self._KWARGS_OPTS[arg] args, opts = kwarg_opt[0:-1], kwarg_opt[-1] - subparser.add_argument(*args, **opts) # type:ignore + subparser.add_argument(*args, **opts) # type: ignore for arg in positional: opts = self._POSITIONAL_OPTS.get(arg, {}) - subparser.add_argument(arg, **opts) # type:ignore + subparser.add_argument(arg, **opts) # type: ignore def _inspect_function(self, fn: CommandFunction) -> tuple[Any, Any, str]: spec = compat.inspect_getfullargspec(fn) diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index 964cd1f3..dc4fbaa6 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -328,7 +328,7 @@ class DefaultImpl(metaclass=ImplMeta): cls_( table_name, column_name, - server_default, # type:ignore[arg-type] + server_default, # type: ignore[arg-type] schema=schema, existing_type=existing_type, existing_server_default=existing_server_default, diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index 91cd9e42..52ef34d0 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -199,7 +199,7 @@ class MSSQLImpl(DefaultImpl): index.table.append_column(Column(col, sqltypes.NullType)) self._exec(CreateIndex(index, **kw)) - def bulk_insert( # type:ignore[override] + def bulk_insert( # type: ignore[override] self, table: Union[TableClause, Table], rows: List[dict], **kw: Any ) -> None: if self.as_sql: diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index cc03f453..3659375d 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -52,7 +52,6 @@ from ..operations.base import Operations from ..util import sqla_compat from ..util.sqla_compat import compiles - if TYPE_CHECKING: from typing import Literal @@ -637,7 +636,7 @@ class CreateExcludeConstraintOp(ops.AddConstraintOp): self.kw = kw @classmethod - def from_constraint( # type:ignore[override] + def from_constraint( # type: ignore[override] cls, constraint: ExcludeConstraint ) -> CreateExcludeConstraintOp: constraint_table = sqla_compat._table_for_constraint(constraint) @@ -803,7 +802,7 @@ def _exclude_constraint( "(%s, %r)" % ( _render_potential_column( - sqltext, # type:ignore[arg-type] + sqltext, # type: ignore[arg-type] autogen_context, ), opstring, diff --git a/alembic/operations/__init__.py b/alembic/operations/__init__.py index 26197cbe..fabe0e20 100644 --- a/alembic/operations/__init__.py +++ b/alembic/operations/__init__.py @@ -5,7 +5,6 @@ from .base import Operations from .ops import MigrateOperation from .ops import MigrationScript - __all__ = [ "AbstractOperations", "Operations", diff --git a/alembic/operations/base.py b/alembic/operations/base.py index 67aefdd4..c9e976c5 100644 --- a/alembic/operations/base.py +++ b/alembic/operations/base.py @@ -487,7 +487,7 @@ class AbstractOperations(util.ModuleClsProxy): a particular constraint name is already converted. """ - return conv(name) + return conv(name) # type: ignore[no-any-return] def inline_literal( self, value: Union[str, int], type_: Optional[TypeEngine[Any]] = None diff --git a/alembic/operations/batch.py b/alembic/operations/batch.py index 9b48be59..6f50bb57 100644 --- a/alembic/operations/batch.py +++ b/alembic/operations/batch.py @@ -511,10 +511,10 @@ class ApplyBatchImpl: # pop named constraints for Boolean/Enum for rename if ( isinstance(resolved_existing_type, SchemaEventTarget) - and resolved_existing_type.name # type:ignore[attr-defined] # noqa E501 + and resolved_existing_type.name # type: ignore[attr-defined] # noqa E501 ): self.named_constraints.pop( - resolved_existing_type.name, # type:ignore[attr-defined] # noqa E501 + resolved_existing_type.name, # type: ignore[attr-defined] # noqa E501 None, ) @@ -527,8 +527,8 @@ class ApplyBatchImpl: # Operations.implementation_for(alter_column) if isinstance(existing.type, SchemaEventTarget): - existing.type._create_events = ( # type:ignore[attr-defined] - existing.type.create_constraint # type:ignore[attr-defined] # noqa + existing.type._create_events = ( # type: ignore[attr-defined] + existing.type.create_constraint # type: ignore[attr-defined] # noqa ) = False self.impl.cast_for_batch_migrate( @@ -639,10 +639,10 @@ class ApplyBatchImpl: if ( "existing_type" in kw and isinstance(kw["existing_type"], SchemaEventTarget) - and kw["existing_type"].name # type:ignore[attr-defined] + and kw["existing_type"].name # type: ignore[attr-defined] ): self.named_constraints.pop( - kw["existing_type"].name, None # type:ignore[attr-defined] + kw["existing_type"].name, None # type: ignore[attr-defined] ) def create_column_comment(self, column): diff --git a/alembic/runtime/environment.py b/alembic/runtime/environment.py index 5817e2d9..f9e0b1e3 100644 --- a/alembic/runtime/environment.py +++ b/alembic/runtime/environment.py @@ -173,12 +173,12 @@ class EnvironmentContext(util.ModuleClsProxy): _migration_context: Optional[MigrationContext] = None - config: Config = None # type:ignore[assignment] + config: Config = None # type: ignore[assignment] """An instance of :class:`.Config` representing the configuration file contents as well as other variables set programmatically within it.""" - script: ScriptDirectory = None # type:ignore[assignment] + script: ScriptDirectory = None # type: ignore[assignment] """An instance of :class:`.ScriptDirectory` which provides programmatic access to version files within the ``versions/`` directory. diff --git a/alembic/runtime/migration.py b/alembic/runtime/migration.py index 3fccf22a..a108539d 100644 --- a/alembic/runtime/migration.py +++ b/alembic/runtime/migration.py @@ -170,13 +170,13 @@ class MigrationContext: if "output_encoding" in opts: self.output_buffer = EncodedIO( opts.get("output_buffer") - or sys.stdout, # type:ignore[arg-type] + or sys.stdout, # type: ignore[arg-type] opts["output_encoding"], ) else: self.output_buffer = opts.get( "output_buffer", sys.stdout - ) # type:ignore[assignment] # noqa: E501 + ) # type: ignore[assignment] # noqa: E501 self.transactional_ddl = transactional_ddl @@ -645,7 +645,7 @@ class MigrationContext: def _in_connection_transaction(self) -> bool: try: - meth = self.connection.in_transaction # type:ignore[union-attr] + meth = self.connection.in_transaction # type: ignore[union-attr] except AttributeError: return False else: @@ -1036,7 +1036,7 @@ class RevisionStep(MigrationStep): return (self.revision.revision,) @property - def from_revisions_no_deps( # type:ignore[override] + def from_revisions_no_deps( # type: ignore[override] self, ) -> Tuple[str, ...]: if self.is_upgrade: @@ -1052,7 +1052,7 @@ class RevisionStep(MigrationStep): return self.revision._normalized_down_revisions @property - def to_revisions_no_deps( # type:ignore[override] + def to_revisions_no_deps( # type: ignore[override] self, ) -> Tuple[str, ...]: if self.is_upgrade: @@ -1264,13 +1264,13 @@ class StampStep(MigrationStep): return self.to_ @property - def from_revisions_no_deps( # type:ignore[override] + def from_revisions_no_deps( # type: ignore[override] self, ) -> Tuple[str, ...]: return self.from_ @property - def to_revisions_no_deps( # type:ignore[override] + def to_revisions_no_deps( # type: ignore[override] self, ) -> Tuple[str, ...]: return self.to_ diff --git a/alembic/testing/env.py b/alembic/testing/env.py index ad4de783..f8711f2a 100644 --- a/alembic/testing/env.py +++ b/alembic/testing/env.py @@ -96,14 +96,11 @@ def script_file_fixture(txt): def env_file_fixture(txt): dir_ = _join_path(_get_staging_directory(), "scripts") - txt = ( - """ + txt = """ from alembic import context config = context.config -""" - + txt - ) +""" + txt path = _join_path(dir_, "env.py") pyc_path = util.pyc_file_from_path(path) @@ -128,8 +125,7 @@ def _sqlite_testing_config(sourceless=False, future=False): sqlalchemy_future = future or ("future" in config.db.__class__.__module__) - return _write_config_file( - f""" + return _write_config_file(f""" [alembic] script_location = {dir_} sqlalchemy.url = {url} @@ -164,8 +160,7 @@ keys = generic [formatter_generic] format = %%(levelname)-5.5s [%%(name)s] %%(message)s datefmt = %%H:%%M:%%S - """ - ) + """) def _multi_dir_testing_config(sourceless=False, extra_version_location=""): @@ -174,8 +169,7 @@ def _multi_dir_testing_config(sourceless=False, extra_version_location=""): url = "sqlite:///%s/foo.db" % dir_ - return _write_config_file( - f""" + return _write_config_file(f""" [alembic] script_location = {dir_} sqlalchemy.url = {url} @@ -208,8 +202,7 @@ keys = generic [formatter_generic] format = %%(levelname)-5.5s [%%(name)s] %%(message)s datefmt = %%H:%%M:%%S - """ - ) + """) def _no_sql_pyproject_config(dialect="postgresql", directives=""): @@ -260,8 +253,7 @@ def _no_sql_testing_config(dialect="postgresql", directives=""): """use a postgresql url with no host so that connections guaranteed to fail""" dir_ = _join_path(_get_staging_directory(), "scripts") - return _write_config_file( - f""" + return _write_config_file(f""" [alembic] script_location ={dir_} sqlalchemy.url = {dialect}:// @@ -291,8 +283,7 @@ keys = generic format = %%(levelname)-5.5s [%%(name)s] %%(message)s datefmt = %%H:%%M:%%S -""" - ) +""") def _write_toml_config(tomltext, initext): @@ -541,8 +532,7 @@ def _multidb_testing_config(engines): for key, value in engines.items() ) - return _write_config_file( - f""" + return _write_config_file(f""" [alembic] script_location = {dir_} sourceless = false @@ -573,8 +563,7 @@ keys = generic [formatter_generic] format = %%(levelname)-5.5s [%%(name)s] %%(message)s datefmt = %%H:%%M:%%S - """ - ) + """) def _join_path(base: str, *more: str): diff --git a/docs/build/conf.py b/docs/build/conf.py index e0658445..28780730 100644 --- a/docs/build/conf.py +++ b/docs/build/conf.py @@ -15,7 +15,6 @@ import os import sys - # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. diff --git a/pyproject.toml b/pyproject.toml index 0fbdb9c9..25ebc56b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,7 @@ alembic = "alembic.config:main" tests = [ "pytest>8,<10", "pytest-xdist", - "black==25.9.0", # for test_post_write.py + "black==26.5.1", # for test_post_write.py "zimports", # for stub testing "tzdata", "junitparser" @@ -78,7 +78,7 @@ lint = [ "flake8-rst-docstrings", "pydocstyle", "pygments", - "black==25.9.0" + "black==26.5.1" ] mypy = [ diff --git a/reap_dbs.py b/reap_dbs.py index 6b2215df..66e503b6 100644 --- a/reap_dbs.py +++ b/reap_dbs.py @@ -16,7 +16,6 @@ import sys from sqlalchemy.testing import provision - logging.basicConfig() logging.getLogger(provision.__name__).setLevel(logging.INFO) diff --git a/tests/conftest.py b/tests/conftest.py index fc3871f5..69172ab3 100755 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,7 @@ This script is an extension to py.test which installs SQLAlchemy's testing plugin into the local environment. """ + import os import pytest diff --git a/tests/test_batch.py b/tests/test_batch.py index fa582829..0dfd273b 100644 --- a/tests/test_batch.py +++ b/tests/test_batch.py @@ -2467,8 +2467,7 @@ class OfflineTest(TestBase): batch_op.drop_column('foo') batch_op.add_column(Column('bar', String)) - """ # noqa: E501 - % a, + """ % a, # noqa: E501 ) yield go @@ -2510,8 +2509,7 @@ class OfflineTest(TestBase): batch_op.drop_column('foo') batch_op.add_column(Column('bar', String)) - """ - % a, + """ % a, ) yield go diff --git a/tests/test_bulk_insert.py b/tests/test_bulk_insert.py index 0841c9a9..a4894bca 100644 --- a/tests/test_bulk_insert.py +++ b/tests/test_bulk_insert.py @@ -238,17 +238,13 @@ class RoundTripTest(TestBase): def setUp(self): self.conn = config.db.connect() with self.conn.begin(): - self.conn.execute( - text( - """ + self.conn.execute(text(""" create table foo( id integer primary key, data varchar(50), x integer ) - """ - ) - ) + """)) context = MigrationContext.configure(self.conn) self.op = op.Operations(context) self.t1 = table("foo", column("id"), column("data"), column("x")) diff --git a/tests/test_command.py b/tests/test_command.py index 391a19d0..16e2b1cc 100644 --- a/tests/test_command.py +++ b/tests/test_command.py @@ -74,8 +74,7 @@ class HistoryTest(_BufMixin, TestBase): @classmethod def _setup_env_file(self): - env_file_fixture( - r""" + env_file_fixture(r""" from sqlalchemy import MetaData, engine_from_config target_metadata = MetaData() @@ -97,8 +96,7 @@ try: finally: connection.close() engine.dispose() -""" - ) +""") def _eq_cmd_output(self, buf, expected, env_token=False, currents=()): script = ScriptDirectory.from_config(self.cfg) @@ -222,8 +220,7 @@ class RevisionEnvironmentTest(_BufMixin, TestBase): @classmethod def _setup_env_file(self): - env_file_fixture( - r""" + env_file_fixture(r""" from sqlalchemy import MetaData, engine_from_config target_metadata = MetaData() @@ -245,8 +242,7 @@ try: finally: connection.close() engine.dispose() -""" - ) +""") def _assert_env_token(self, buf, expected): if expected: @@ -423,8 +419,7 @@ class RevisionTest(TestBase): clear_staging_env() def _env_fixture(self, version_table_pk=True): - env_file_fixture( - """ + env_file_fixture(""" from sqlalchemy import MetaData, engine_from_config target_metadata = MetaData() @@ -447,9 +442,7 @@ finally: connection.close() engine.dispose() -""" - % (version_table_pk,) - ) +""" % (version_table_pk,)) def test_create_rev_plain_db_not_up_to_date(self): self._env_fixture() @@ -684,8 +677,7 @@ class CheckTest(TestBase): clear_staging_env() def _env_fixture(self, version_table_pk=True): - env_file_fixture( - f""" + env_file_fixture(f""" from sqlalchemy import MetaData, engine_from_config target_metadata = MetaData() @@ -710,8 +702,7 @@ finally: connection.close() engine.dispose() -""" - ) +""") def test_check_no_changes(self): self._env_fixture() @@ -749,8 +740,7 @@ class CheckTestMultiDB(CheckTest): ) def _env_fixture(self): - env_file_fixture( - """ + env_file_fixture(""" import re from sqlalchemy import MetaData, engine_from_config @@ -776,8 +766,7 @@ for db_name in re.split(r",\\s*", db_names): engine.dispose() -""" - ) +""") class _StampTest: @@ -1092,8 +1081,7 @@ class LiveStampTest(TestBase): """ revision = '%s' down_revision = None -""" - % a, +""" % a, ) script.generate_revision(b, None, refresh=True) write_script( @@ -1102,8 +1090,7 @@ down_revision = None """ revision = '%s' down_revision = '%s' -""" - % (b, a), +""" % (b, a), ) def tearDown(self): @@ -1623,10 +1610,8 @@ class CommandLineTest(TestBase): root = pathlib.Path(_get_staging_directory()) with (root / "pyproject.toml").open("w") as file_: - file_.write( - """[tool.sometool] -someconfig = 'bar'""" - ) + file_.write("""[tool.sometool] +someconfig = 'bar'""") yield config.Config( self.cfg.config_file_name, toml_file=root / "pyproject.toml" ) @@ -1663,10 +1648,8 @@ someconfig = 'bar'""" with open(cfg.toml_file_name, "r") as f: file_content = f.read() - assert file_content.startswith( - """[tool.sometool] -someconfig = 'bar'\n\n[tool.alembic]""" - ) + assert file_content.startswith("""[tool.sometool] +someconfig = 'bar'\n\n[tool.alembic]""") toml = compat.tomllib.loads(file_content) eq_( toml, diff --git a/tests/test_config.py b/tests/test_config.py index 0dc840f8..74102e4b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -28,12 +28,10 @@ from alembic.testing.fixtures import TestBase class FileConfigTest(TestBase): def test_config_args(self): - cfg = _write_config_file( - """ + cfg = _write_config_file(""" [alembic] migrations = %(base_path)s/db/migrations -""" - ) +""") test_cfg = config.Config( cfg.config_file_name, config_args=dict(base_path="/home/alembic") ) @@ -61,12 +59,10 @@ class ConfigTest(TestBase): logger.addHandler(handler) logger.setLevel(logging.INFO) - cfg = _write_config_file( - """ + cfg = _write_config_file(""" [alembic] script_location = %(base_path)s/db/migrations -""" - ) +""") test_cfg = config.Config( cfg.config_file_name, config_args=dict(base_path="/tmp") ) @@ -464,14 +460,12 @@ class PyprojectConfigTest(TestBase): def test_script_location(self, pyproject_only_env): cfg = pyproject_only_env with cfg._toml_file_path.open("wb") as file_: - file_.write( - rb""" + file_.write(rb""" [tool.alembic] script_location = "%(here)s/scripts" -""" - ) +""") new_cfg = config.Config( file_=cfg.config_file_name, toml_file=cfg._toml_file_path @@ -486,13 +480,11 @@ script_location = "%(here)s/scripts" cfg = pyproject_only_env with cfg._toml_file_path.open("ba") as file_: - file_.write( - b""" + file_.write(b""" version_locations = [ "%(here)s/foo/bar" ] -""" - ) +""") if "toml_alembic_config" in cfg.__dict__: cfg.__dict__.pop("toml_alembic_config") @@ -510,8 +502,7 @@ version_locations = [ cfg = pyproject_only_env with cfg._toml_file_path.open("wb") as file_: - file_.write( - rb""" + file_.write(rb""" [tool.alembic] script_location = "%(here)s/scripts" @@ -521,8 +512,7 @@ prepend_sys_path = [ "%(here)s/path/to/python", "c:\\some\\path" ] -""" - ) +""") if "toml_alembic_config" in cfg.__dict__: cfg.__dict__.pop("toml_alembic_config") @@ -542,8 +532,7 @@ prepend_sys_path = [ cfg = pyproject_only_env with cfg._toml_file_path.open("wb") as file_: - file_.write( - rb""" + file_.write(rb""" [tool.alembic] script_location = "%(here)s/scripts" @@ -554,8 +543,7 @@ type = "exec" executable = "%(here)s/.venv/bin/ruff" options = "-l 79 REVISION_SCRIPT_FILENAME" -""" - ) +""") if "toml_alembic_config" in cfg.__dict__: cfg.__dict__.pop("toml_alembic_config") @@ -579,8 +567,7 @@ options = "-l 79 REVISION_SCRIPT_FILENAME" cfg = pyproject_only_env with cfg._toml_file_path.open("wb") as file_: - file_.write( - rb""" + file_.write(rb""" [tool.alembic] script_location = "%(here)s/scripts" @@ -590,8 +577,7 @@ my_list = [ "two %(here)s three" ] -""" - ) +""") if "toml_alembic_config" in cfg.__dict__: cfg.__dict__.pop("toml_alembic_config") @@ -626,15 +612,13 @@ my_list = [ else: argtype.fail() - file_.write( - rf""" + file_.write(rf""" [tool.alembic] script_location = "%(here)s/scripts" {config_option} -""" - ) +""") if "toml_alembic_config" in cfg.__dict__: cfg.__dict__.pop("toml_alembic_config") @@ -668,14 +652,12 @@ script_location = "%(here)s/scripts" else: arg_type.fail() - file_.write( - rf""" + file_.write(rf""" [tool.alembic] script_location = "%(here)s/scripts" {config_option} -""" - ) +""") if "toml_alembic_config" in cfg.__dict__: cfg.__dict__.pop("toml_alembic_config") diff --git a/tests/test_environment.py b/tests/test_environment.py index fb98bf11..edbbe051 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -92,9 +92,7 @@ def upgrade(): do some SQL thing with a % percent sign % ''') -""".format( - a_rev - ), +""".format(a_rev), ) with capture_context_buffer(transactional_ddl=True) as buf: command.upgrade(self.cfg, "arev", sql=True) @@ -129,8 +127,7 @@ def upgrade(): def downgrade(): pass -""" - % a_rev, +""" % a_rev, ) migration_fn = mock.MagicMock() diff --git a/tests/test_offline_environment.py b/tests/test_offline_environment.py index 8188d2cf..1c0d958d 100644 --- a/tests/test_offline_environment.py +++ b/tests/test_offline_environment.py @@ -27,50 +27,40 @@ class OfflineEnvironmentTest(TestBase): clear_staging_env() def test_not_requires_connection(self): - env_file_fixture( - """ + env_file_fixture(""" assert not context.requires_connection() -""" - ) +""") command.upgrade(self.cfg, a, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) def test_requires_connection(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.requires_connection() -""" - ) +""") command.upgrade(self.cfg, a) command.downgrade(self.cfg, a) def test_starting_rev_post_context(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite', starting_rev='x') assert context.get_starting_revision_argument() == 'x' -""" - ) +""") command.upgrade(self.cfg, a, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) command.current(self.cfg) command.stamp(self.cfg, a) def test_starting_rev_pre_context(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_starting_revision_argument() == 'x' -""" - ) +""") command.upgrade(self.cfg, "x:y", sql=True) command.downgrade(self.cfg, "x:y", sql=True) def test_starting_rev_pre_context_cmd_w_no_startrev(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_starting_revision_argument() == 'x' -""" - ) +""") assert_raises_message( util.CommandError, "No starting revision argument is available.", @@ -79,11 +69,9 @@ assert context.get_starting_revision_argument() == 'x' ) def test_starting_rev_current_pre_context(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_starting_revision_argument() is None -""" - ) +""") assert_raises_message( util.CommandError, "No starting revision argument is available.", @@ -92,57 +80,42 @@ assert context.get_starting_revision_argument() is None ) def test_destination_rev_pre_context(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_revision_argument() == '%s' -""" - % b - ) +""" % b) command.upgrade(self.cfg, b, sql=True) command.stamp(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (c, b), sql=True) def test_destination_rev_pre_context_multihead(self): d, e, f = multi_heads_fixture(self.cfg, a, b, c) - env_file_fixture( - """ + env_file_fixture(""" assert set(context.get_revision_argument()) == set(('%s', '%s', '%s', )) -""" - % (f, e, c) - ) +""" % (f, e, c)) command.upgrade(self.cfg, "heads", sql=True) def test_destination_rev_post_context(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') assert context.get_revision_argument() == '%s' -""" - % b - ) +""" % b) command.upgrade(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (c, b), sql=True) command.stamp(self.cfg, b, sql=True) def test_destination_rev_post_context_multihead(self): d, e, f = multi_heads_fixture(self.cfg, a, b, c) - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') assert set(context.get_revision_argument()) == set(('%s', '%s', '%s', )) -""" - % (f, e, c) - ) +""" % (f, e, c)) command.upgrade(self.cfg, "heads", sql=True) def test_head_rev_pre_context(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_head_revision() == '%s' assert context.get_head_revisions() == ('%s', ) -""" - % (c, c) - ) +""" % (c, c)) command.upgrade(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) command.stamp(self.cfg, b, sql=True) @@ -150,26 +123,20 @@ assert context.get_head_revisions() == ('%s', ) def test_head_rev_pre_context_multihead(self): d, e, f = multi_heads_fixture(self.cfg, a, b, c) - env_file_fixture( - """ + env_file_fixture(""" assert set(context.get_head_revisions()) == set(('%s', '%s', '%s', )) -""" - % (e, f, c) - ) +""" % (e, f, c)) command.upgrade(self.cfg, e, sql=True) command.downgrade(self.cfg, "%s:%s" % (e, b), sql=True) command.stamp(self.cfg, c, sql=True) command.current(self.cfg) def test_head_rev_post_context(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') assert context.get_head_revision() == '%s' assert context.get_head_revisions() == ('%s', ) -""" - % (c, c) - ) +""" % (c, c)) command.upgrade(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) command.stamp(self.cfg, b, sql=True) @@ -177,72 +144,57 @@ assert context.get_head_revisions() == ('%s', ) def test_head_rev_post_context_multihead(self): d, e, f = multi_heads_fixture(self.cfg, a, b, c) - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') assert set(context.get_head_revisions()) == set(('%s', '%s', '%s', )) -""" - % (e, f, c) - ) +""" % (e, f, c)) command.upgrade(self.cfg, e, sql=True) command.downgrade(self.cfg, "%s:%s" % (e, b), sql=True) command.stamp(self.cfg, c, sql=True) command.current(self.cfg) def test_tag_pre_context(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_tag_argument() == 'hi' -""" - ) +""") command.upgrade(self.cfg, b, sql=True, tag="hi") command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True, tag="hi") def test_tag_pre_context_None(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_tag_argument() is None -""" - ) +""") command.upgrade(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) def test_tag_cmd_arg(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') assert context.get_tag_argument() == 'hi' -""" - ) +""") command.upgrade(self.cfg, b, sql=True, tag="hi") command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True, tag="hi") def test_tag_cfg_arg(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite', tag='there') assert context.get_tag_argument() == 'there' -""" - ) +""") command.upgrade(self.cfg, b, sql=True, tag="hi") command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True, tag="hi") def test_tag_None(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') assert context.get_tag_argument() is None -""" - ) +""") command.upgrade(self.cfg, b, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) def test_downgrade_wo_colon(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') -""" - ) +""") assert_raises_message( util.CommandError, "downgrade with --sql requires :", @@ -253,13 +205,11 @@ context.configure(dialect_name='sqlite') ) def test_upgrade_with_output_encoding(self): - env_file_fixture( - """ + env_file_fixture(""" url = config.get_main_option('sqlalchemy.url') context.configure(url=url, output_encoding='utf-8') assert not context.requires_connection() -""" - ) +""") command.upgrade(self.cfg, a, sql=True) command.downgrade(self.cfg, "%s:%s" % (b, a), sql=True) @@ -275,44 +225,34 @@ assert not context.requires_connection() ) def test_starting_rev_pre_context_abbreviated(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_starting_revision_argument() == '%s' -""" - % b[0:4] - ) +""" % b[0:4]) command.upgrade(self.cfg, "%s:%s" % (b[0:4], c), sql=True) command.stamp(self.cfg, "%s:%s" % (b[0:4], c), sql=True) command.downgrade(self.cfg, "%s:%s" % (b[0:4], a), sql=True) def test_destination_rev_pre_context_abbreviated(self): - env_file_fixture( - """ + env_file_fixture(""" assert context.get_revision_argument() == '%s' -""" - % b[0:4] - ) +""" % b[0:4]) command.upgrade(self.cfg, "%s:%s" % (a, b[0:4]), sql=True) command.stamp(self.cfg, b[0:4], sql=True) command.downgrade(self.cfg, "%s:%s" % (c, b[0:4]), sql=True) def test_starting_rev_context_runs_abbreviated(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') context.run_migrations() -""" - ) +""") command.upgrade(self.cfg, "%s:%s" % (b[0:4], c), sql=True) command.downgrade(self.cfg, "%s:%s" % (b[0:4], a), sql=True) def test_destination_rev_context_runs_abbreviated(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite') context.run_migrations() -""" - ) +""") command.upgrade(self.cfg, "%s:%s" % (a, b[0:4]), sql=True) command.stamp(self.cfg, b[0:4], sql=True) command.downgrade(self.cfg, "%s:%s" % (c, b[0:4]), sql=True) diff --git a/tests/test_post_write.py b/tests/test_post_write.py index fe2afe97..c1a5c17f 100644 --- a/tests/test_post_write.py +++ b/tests/test_post_write.py @@ -63,8 +63,7 @@ class RunHookTest(TestBase): ) ) else: - self.cfg = _no_sql_pyproject_config( - directives=""" + self.cfg = _no_sql_pyproject_config(directives=""" [[tool.alembic.post_write_hooks]] name="hook1" @@ -75,8 +74,7 @@ class RunHookTest(TestBase): name="hook2" type="hook2" arg1="bar" - """ - ) + """) rev = command.revision(self.cfg, message="x") eq_( @@ -109,11 +107,9 @@ class RunHookTest(TestBase): if config.ini: self.cfg = _no_sql_testing_config(directives="") else: - self.cfg = _no_sql_pyproject_config( - directives=""" + self.cfg = _no_sql_pyproject_config(directives=""" - """ - ) + """) command.revision(self.cfg, message="x") @@ -133,14 +129,12 @@ class RunHookTest(TestBase): ) ) else: - self.cfg = _no_sql_pyproject_config( - directives=""" + self.cfg = _no_sql_pyproject_config(directives=""" [[tool.alembic.post_write_hooks]] name="foo" bar="somebar" - """ - ) + """) assert_raises_message( util.CommandError, @@ -480,15 +474,11 @@ ruff.cwd = /path/to/cwd ) def test_module_config_missing(self): - self.cfg = _no_sql_testing_config( - directives=( - """ + self.cfg = _no_sql_testing_config(directives=(""" [post_write_hooks] hooks = ruff ruff.type = module - """ - ) - ) + """)) assert_raises_message( util.CommandError, "Key ruff.module is required for post write hook 'ruff'", @@ -498,16 +488,12 @@ ruff.type = module ) def test_module_not_found(self): - self.cfg = _no_sql_testing_config( - directives=( - """ + self.cfg = _no_sql_testing_config(directives=(""" [post_write_hooks] hooks = ruff ruff.type = module ruff.module = ruff_not_found - """ - ) - ) + """)) assert_raises_message( util.CommandError, "Could not find module ruff_not_found", diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index d0304651..685b93be 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -64,7 +64,6 @@ from alembic.testing.fixtures import TablesTest from alembic.testing.fixtures import TestBase from alembic.testing.suite._autogen_fixtures import AutogenFixtureTest - if True: from alembic.autogenerate.compare.server_defaults import ( _render_server_default_for_compare, @@ -559,8 +558,7 @@ def upgrade(): def downgrade(): op.drop_table("sometable") -""" - % self.rid, +""" % self.rid, ) def _distinct_enum_script(self): @@ -588,8 +586,7 @@ def downgrade(): op.drop_table("sometable") ENUM(name="pgenum").drop(op.get_bind(), checkfirst=False) -""" - % self.rid, +""" % self.rid, ) def test_offline_inline_enum_create(self): @@ -638,16 +635,12 @@ class PostgresqlInlineLiteralTest(TablesTest): @classmethod def insert_data(cls, connection): - connection.execute( - text( - """ + connection.execute(text(""" insert into tab (col) values ('old data 1'), ('old data 2.1'), ('old data 3') - """ - ) - ) + """)) def test_inline_percent(self, connection, ops_context): # TODO: here's the issue, you need to escape this. diff --git a/tests/test_script_consumption.py b/tests/test_script_consumption.py index 0ea6e471..0858ac2a 100644 --- a/tests/test_script_consumption.py +++ b/tests/test_script_consumption.py @@ -187,8 +187,7 @@ class ApplyVersionsFunctionalTest(PatchEnvironment, TestBase): def downgrade(): op.execute("DROP TABLE foo") - """ - % a, + """ % a, sourceless=self.sourceless, ) @@ -210,8 +209,7 @@ class ApplyVersionsFunctionalTest(PatchEnvironment, TestBase): def downgrade(): op.execute("DROP TABLE bar") - """ - % (b, a), + """ % (b, a), sourceless=self.sourceless, ) @@ -233,8 +231,7 @@ class ApplyVersionsFunctionalTest(PatchEnvironment, TestBase): def downgrade(): op.execute("DROP TABLE bat") - """ - % (c, b), + """ % (c, b), sourceless=self.sourceless, ) @@ -325,9 +322,7 @@ class CallbackEnvironmentTest(ApplyVersionsFunctionalTest): @staticmethod def _env_file_fixture(): - env_file_fixture( - textwrap.dedent( - """\ + env_file_fixture(textwrap.dedent("""\ import alembic from alembic import context from sqlalchemy import engine_from_config, pool @@ -364,9 +359,7 @@ class CallbackEnvironmentTest(ApplyVersionsFunctionalTest): run_migrations_offline() else: run_migrations_online() - """ - ) - ) + """)) def test_steps(self): import alembic @@ -483,8 +476,7 @@ def upgrade(): def downgrade(): pass -""" - % (a,), +""" % (a,), ) script.generate_revision(b, "revision b", refresh=True) write_script( @@ -508,8 +500,7 @@ def upgrade(): def downgrade(): pass -""" - % (b, a), +""" % (b, a), ) script.generate_revision(c, "revision c", refresh=True) write_script( @@ -530,8 +521,7 @@ def upgrade(): def downgrade(): pass -""" - % (c, b), +""" % (c, b), ) return a, b, c @@ -598,8 +588,7 @@ def downgrade(): def test_noerr_transaction_opened_externally(self): a, b, c = self._opened_transaction_fixture() - env_file_fixture( - """ + env_file_fixture(""" from sqlalchemy import engine_from_config, pool def run_migrations_online(): @@ -621,8 +610,7 @@ def run_migrations_online(): run_migrations_online() -""" - ) +""") command.stamp(self.cfg, c) @@ -650,8 +638,7 @@ class EncodingTest(TestBase): write_script( script, self.a, - ( - """# coding: utf-8 + ("""# coding: utf-8 from __future__ import unicode_literals revision = '%s' down_revision = None @@ -664,9 +651,7 @@ def upgrade(): def downgrade(): op.execute("drôle de petite voix m’a réveillé") -""" - % self.a - ), +""" % self.a), encoding="utf-8", ) @@ -711,8 +696,7 @@ class VersionNameTemplateTest(TestBase): def downgrade(): op.execute("DROP TABLE foo") - """ - % a, + """ % a, ) script = ScriptDirectory.from_config(self.cfg) @@ -757,8 +741,7 @@ class VersionNameTemplateTest(TestBase): path = script.get_revision(a).path with open(path, "w") as fp: - fp.write( - """ + fp.write(""" down_revision = None from alembic import op @@ -771,8 +754,7 @@ def upgrade(): def downgrade(): op.execute("DROP TABLE foo") -""" - ) +""") pyc_path = util.pyc_file_from_path(path) if pyc_path is not None and os.access(pyc_path, os.F_OK): os.unlink(pyc_path) @@ -886,8 +868,7 @@ class SourcelessNeedsFlagTest(TestBase): def downgrade(): op.execute("DROP TABLE foo") - """ - % a, + """ % a, sourceless=True, ) diff --git a/tests/test_script_production.py b/tests/test_script_production.py index a9145773..e14ace91 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -560,8 +560,7 @@ def upgrade(): def downgrade(): pass -""" - % (name, model, name), +""" % (name, model, name), ) def tearDown(self): @@ -747,9 +746,7 @@ def downgrade(): with open(rev.path) as handle: result = handle.read() - assert ( - ( - ''' + assert (''' def upgrade() -> None: """Upgrade schema.""" # ### commands auto generated by Alembic - please adjust! ### @@ -759,10 +756,7 @@ def upgrade() -> None: sa.PrimaryKeyConstraint('id') ) # ### end Alembic commands ### -''' - ) - in result - ) +''') in result class ScriptAccessorTest(TestBase): @@ -880,8 +874,7 @@ class MultiContextTest(TestBase): clear_staging_env() def test_autogen(self): - self._write_metadata( - """ + self._write_metadata(""" import sqlalchemy as sa m1 = sa.MetaData() @@ -893,8 +886,7 @@ sa.Table('e1t1', m1, sa.Column('x', sa.Integer)) sa.Table('e2t1', m2, sa.Column('y', sa.Integer)) sa.Table('e3t1', m3, sa.Column('z', sa.Integer)) -""" - ) +""") rev = command.revision( self.cfg, message="some message", autogenerate=True @@ -1432,18 +1424,14 @@ class TemplateArgsTest(TestBase): eq_(template_args, {"x": "x1", "y": "y2", "z": "z1", "q": "q1"}) def test_tmpl_args_revision(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite', template_args={"somearg":"somevalue"}) -""" - ) - script_file_fixture( - """ +""") + script_file_fixture(""" # somearg: ${somearg} revision = ${repr(up_revision)} down_revision = ${repr(down_revision)} -""" - ) +""") command.revision(self.cfg, message="some rev") script = ScriptDirectory.from_config(self.cfg) @@ -1454,16 +1442,12 @@ down_revision = ${repr(down_revision)} assert "somearg: somevalue" in text def test_bad_render(self): - env_file_fixture( - """ + env_file_fixture(""" context.configure(dialect_name='sqlite', template_args={"somearg":"somevalue"}) -""" - ) - script_file_fixture( - """ +""") + script_file_fixture(""" <% z = x + y %> -""" - ) +""") try: command.revision(self.cfg, message="some rev") @@ -1524,8 +1508,7 @@ def upgrade(): def downgrade(): pass -""" - % (name, model, name), +""" % (name, model, name), ) def tearDown(self): diff --git a/tools/write_pyi.py b/tools/write_pyi.py index bd32b383..7a30598f 100644 --- a/tools/write_pyi.py +++ b/tools/write_pyi.py @@ -272,15 +272,13 @@ def _generate_stub_for_meth( suffix = "..." if file_info.AddEllipsis and docs else "" - func_text = textwrap.dedent( - f""" + func_text = textwrap.dedent(f""" {overload} {contextmanager} def {name}{argspec}: {"..." if not docs else ""} {docs} {suffix} - """ - ) + """) return func_text