From: Nikita Sobolev Date: Thu, 21 Apr 2022 16:39:08 +0000 (-0400) Subject: Use `-> None` in script templates by default X-Git-Tag: rel_1_8_0~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e539704aae92bee5d266b1e4e5cfe54b14d544f1;p=thirdparty%2Fsqlalchemy%2Falembic.git Use `-> None` in script templates by default ### Description When working together with `mypy` we have a problem with the default template. `mypy` raises errors on all migrations when used with https://mypy.readthedocs.io/en/stable/config_file.html#confval-disallow_untyped_defs So, there are several options: 1. Remove this flag for `[mypy-db.alembic.*]` in `mypy.ini`. This is not a very good idea, because custom code that might get written in this directory might be not fully typed. 2. Edit our own template. This is what we end up doing right now, but this requires some manual work and many users might miss it 3. Update the source! I think that this is harmless (because only python3.6+ is supported), but allows better type-checking ### Checklist Openning this as a draft for now. Will fix tests after I can see what is failing in the CI. Issue is also on its way! 🙂 This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [ ] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [x] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. Fixes https://github.com/sqlalchemy/alembic/issues/764 **Have a nice day!** Closes: #1012 Pull-request: https://github.com/sqlalchemy/alembic/pull/1012 Pull-request-sha: c1d17b202f414a97e6215cc2a513509462a9db09 Change-Id: Ib077157b5ebd697bf648644a954ed5a9a3b33080 --- diff --git a/alembic/templates/async/env.py b/alembic/templates/async/env.py index 61030dcf..c0071b34 100644 --- a/alembic/templates/async/env.py +++ b/alembic/templates/async/env.py @@ -3,6 +3,7 @@ from logging.config import fileConfig from sqlalchemy import engine_from_config from sqlalchemy import pool +from sqlalchemy.engine import Connection from sqlalchemy.ext.asyncio import AsyncEngine from alembic import context @@ -28,7 +29,7 @@ target_metadata = None # ... etc. -def run_migrations_offline(): +def run_migrations_offline() -> None: """Run migrations in 'offline' mode. This configures the context with just a URL @@ -52,14 +53,14 @@ def run_migrations_offline(): context.run_migrations() -def do_run_migrations(connection): +def do_run_migrations(connection: Connection) -> None: context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() -async def run_migrations_online(): +async def run_migrations_online() -> None: """Run migrations in 'online' mode. In this scenario we need to create an Engine diff --git a/alembic/templates/async/script.py.mako b/alembic/templates/async/script.py.mako index 2c015630..55df2863 100644 --- a/alembic/templates/async/script.py.mako +++ b/alembic/templates/async/script.py.mako @@ -16,9 +16,9 @@ branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)} -def upgrade(): +def upgrade() -> None: ${upgrades if upgrades else "pass"} -def downgrade(): +def downgrade() -> None: ${downgrades if downgrades else "pass"} diff --git a/alembic/templates/generic/env.py b/alembic/templates/generic/env.py index edeff1d5..6626bfd0 100644 --- a/alembic/templates/generic/env.py +++ b/alembic/templates/generic/env.py @@ -26,7 +26,7 @@ target_metadata = None # ... etc. -def run_migrations_offline(): +def run_migrations_offline() -> None: """Run migrations in 'offline' mode. This configures the context with just a URL @@ -50,7 +50,7 @@ def run_migrations_offline(): context.run_migrations() -def run_migrations_online(): +def run_migrations_online() -> None: """Run migrations in 'online' mode. In this scenario we need to create an Engine diff --git a/alembic/templates/generic/script.py.mako b/alembic/templates/generic/script.py.mako index 2c015630..55df2863 100644 --- a/alembic/templates/generic/script.py.mako +++ b/alembic/templates/generic/script.py.mako @@ -16,9 +16,9 @@ branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)} -def upgrade(): +def upgrade() -> None: ${upgrades if upgrades else "pass"} -def downgrade(): +def downgrade() -> None: ${downgrades if downgrades else "pass"} diff --git a/alembic/templates/multidb/env.py b/alembic/templates/multidb/env.py index 687f5843..f787824c 100644 --- a/alembic/templates/multidb/env.py +++ b/alembic/templates/multidb/env.py @@ -43,7 +43,7 @@ target_metadata = {} # ... etc. -def run_migrations_offline(): +def run_migrations_offline() -> None: """Run migrations in 'offline' mode. This configures the context with just a URL @@ -79,7 +79,7 @@ def run_migrations_offline(): context.run_migrations(engine_name=name) -def run_migrations_online(): +def run_migrations_online() -> None: """Run migrations in 'online' mode. In this scenario we need to create an Engine diff --git a/alembic/templates/multidb/script.py.mako b/alembic/templates/multidb/script.py.mako index c3970a52..946ab6a9 100644 --- a/alembic/templates/multidb/script.py.mako +++ b/alembic/templates/multidb/script.py.mako @@ -19,11 +19,11 @@ branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)} -def upgrade(engine_name): +def upgrade(engine_name: str) -> None: globals()["upgrade_%s" % engine_name]() -def downgrade(engine_name): +def downgrade(engine_name: str) -> None: globals()["downgrade_%s" % engine_name]() <% @@ -35,11 +35,11 @@ def downgrade(engine_name): % for db_name in re.split(r',\s*', db_names): -def upgrade_${db_name}(): +def upgrade_${db_name}() -> None: ${context.get("%s_upgrades" % db_name, "pass")} -def downgrade_${db_name}(): +def downgrade_${db_name}() -> None: ${context.get("%s_downgrades" % db_name, "pass")} % endfor diff --git a/alembic/templates/pylons/env.py b/alembic/templates/pylons/env.py index 18d47c85..99bc19ef 100644 --- a/alembic/templates/pylons/env.py +++ b/alembic/templates/pylons/env.py @@ -38,7 +38,7 @@ meta = __import__( target_metadata = None -def run_migrations_offline(): +def run_migrations_offline() -> None: """Run migrations in 'offline' mode. This configures the context with just a URL @@ -60,7 +60,7 @@ def run_migrations_offline(): context.run_migrations() -def run_migrations_online(): +def run_migrations_online() -> None: """Run migrations in 'online' mode. In this scenario we need to create an Engine diff --git a/alembic/templates/pylons/script.py.mako b/alembic/templates/pylons/script.py.mako index 2c015630..55df2863 100644 --- a/alembic/templates/pylons/script.py.mako +++ b/alembic/templates/pylons/script.py.mako @@ -16,9 +16,9 @@ branch_labels = ${repr(branch_labels)} depends_on = ${repr(depends_on)} -def upgrade(): +def upgrade() -> None: ${upgrades if upgrades else "pass"} -def downgrade(): +def downgrade() -> None: ${downgrades if downgrades else "pass"} diff --git a/docs/build/unreleased/764.rst b/docs/build/unreleased/764.rst new file mode 100644 index 00000000..184c0ac7 --- /dev/null +++ b/docs/build/unreleased/764.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: feature, typing + :tickets: 764 + + Adds typing annotations to ``env.py`` and migration templates. + Pull request by Nikita Sobolev. diff --git a/tests/test_script_production.py b/tests/test_script_production.py index 2edae267..05167f0b 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -618,7 +618,7 @@ def downgrade(): assert ( ( """ -def upgrade(): +def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.create_table('test_table', sa.Column('id', sa.Integer(), nullable=False),