diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2022-04-21 12:39:08 -0400 |
---|---|---|
committer | sqla-tester <sqla-tester@sqlalchemy.org> | 2022-04-21 12:39:08 -0400 |
commit | e539704aae92bee5d266b1e4e5cfe54b14d544f1 (patch) | |
tree | ae7e5d6839467726ffe3c2c799b36f558ed50dfb /alembic/templates | |
parent | d770c1ebc596c58c2d010233457fe62ea04af509 (diff) | |
download | alembic-e539704aae92bee5d266b1e4e5cfe54b14d544f1.tar.gz |
Use `-> None` in script templates by default
<!-- 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
Diffstat (limited to 'alembic/templates')
-rw-r--r-- | alembic/templates/async/env.py | 7 | ||||
-rw-r--r-- | alembic/templates/async/script.py.mako | 4 | ||||
-rw-r--r-- | alembic/templates/generic/env.py | 4 | ||||
-rw-r--r-- | alembic/templates/generic/script.py.mako | 4 | ||||
-rw-r--r-- | alembic/templates/multidb/env.py | 4 | ||||
-rw-r--r-- | alembic/templates/multidb/script.py.mako | 8 | ||||
-rw-r--r-- | alembic/templates/pylons/env.py | 4 | ||||
-rw-r--r-- | alembic/templates/pylons/script.py.mako | 4 |
8 files changed, 20 insertions, 19 deletions
diff --git a/alembic/templates/async/env.py b/alembic/templates/async/env.py index 61030dc..c0071b3 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 2c01563..55df286 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 edeff1d..6626bfd 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 2c01563..55df286 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 687f584..f787824 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 c3970a5..946ab6a 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 18d47c8..99bc19e 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 2c01563..55df286 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"} |