<!-- Provide a general summary of your proposed changes in the Title field above -->
### 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! 🙂
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
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: #<issue number>` 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: #<issue number>` 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
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
# ... etc.
-def run_migrations_offline():
+def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
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
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"}
# ... etc.
-def run_migrations_offline():
+def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
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
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"}
# ... etc.
-def run_migrations_offline():
+def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
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
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]()
<%
% 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
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
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
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"}
--- /dev/null
+.. change::
+ :tags: feature, typing
+ :tickets: 764
+
+ Adds typing annotations to ``env.py`` and migration templates.
+ Pull request by Nikita Sobolev.
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),