]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
allow tuple type in down_revision in migration templates
authorSimon Tas <simon.tas.st@gmail.com>
Thu, 29 May 2025 00:11:58 +0000 (20:11 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Thu, 29 May 2025 00:11:58 +0000 (20:11 -0400)
<!-- Provide a general summary of your proposed changes in the Title field above -->

FIXES #1665

Currently the type of `down_revision` in migration templates is `Union[str, None]`. However, when you generate a migration using `alembic merge heads` it will generate a migration with a tuple of strings in `down_revision` which causes a type error.

### Description

f.e. when you have 2 heads and run `alembic merge heads` you will get a migration that looks like this:

```py
revision: str = "6cee9e23ed4b"
down_revision: Union[str, None] = ("aba1172f1a9f", "cdd5e67db84f")
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
```

which will give a type error with type checkers like mypy and pyright. This change fixes this. From the code it seems like just like `branch_labels` and `depends_on` the `down_revision` is first casted to a tuple if needed so typing it as `Sequence[str]` and not `Tuple[str, ...]` should be safe.

### Checklist
<!-- 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
- [x] 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.
- [ ] 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.

**Have a nice day!**

Closes: #1666
Pull-request: https://github.com/sqlalchemy/alembic/pull/1666
Pull-request-sha: 186df96240a621ba71247a4f5cbd6413e3bfc270

Change-Id: I7b203b85bf69f215871995e4c4f5b96c20528c7d

alembic/templates/async/script.py.mako
alembic/templates/generic/script.py.mako
alembic/templates/multidb/script.py.mako
alembic/templates/pyproject/script.py.mako

index 480b130d632ca677c11f23d9fe82cf4014d15e0c..11016301e749297acb67822efc7974ee53c905c6 100644 (file)
@@ -13,7 +13,7 @@ ${imports if imports else ""}
 
 # revision identifiers, used by Alembic.
 revision: str = ${repr(up_revision)}
-down_revision: Union[str, None] = ${repr(down_revision)}
+down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
 branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
 depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
 
index 480b130d632ca677c11f23d9fe82cf4014d15e0c..11016301e749297acb67822efc7974ee53c905c6 100644 (file)
@@ -13,7 +13,7 @@ ${imports if imports else ""}
 
 # revision identifiers, used by Alembic.
 revision: str = ${repr(up_revision)}
-down_revision: Union[str, None] = ${repr(down_revision)}
+down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
 branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
 depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
 
index 3caca7bfa54033036b16f1ae65f64f4ca770f402..8e667d84c8a46494a83c38d950e57e96abffd7b7 100644 (file)
@@ -16,7 +16,7 @@ ${imports if imports else ""}
 
 # revision identifiers, used by Alembic.
 revision: str = ${repr(up_revision)}
-down_revision: Union[str, None] = ${repr(down_revision)}
+down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
 branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
 depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
 
index 480b130d632ca677c11f23d9fe82cf4014d15e0c..11016301e749297acb67822efc7974ee53c905c6 100644 (file)
@@ -13,7 +13,7 @@ ${imports if imports else ""}
 
 # revision identifiers, used by Alembic.
 revision: str = ${repr(up_revision)}
-down_revision: Union[str, None] = ${repr(down_revision)}
+down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
 branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
 depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}