Using mypy with alembic 1.8.1 produces error: "BatchOperations" has no attribute XYZ
Describe the bug After upgrading to alembic 1.8.1, mypy complains about almost every single BatchOperations usage.
Our migrations contain a lot of lines like the following:
with op.batch_alter_table("my_item", schema=utils.get_schema()) as batch_op:
batch_op.add_column(sa.Column("price", sa.Float(), nullable=True))
Before alembic 1.8.1, mypy accepted this code. With 1.8.1, though, mypy complains as follows:
database/alembic/versions/add_my_item_price.py:23: error: "BatchOperations" has no attribute "add_column"
Expected behavior This should not produce an error. It is valid code that is known to work.
To Reproduce
Install mypy and alembic 1.8.1. Create a migration with alembic. Run mypy ..
Sample migration
"""add my_item price
Revision ID: da1eeb0857d1
Revises: 6087603e3d68
Create Date: 2020-11-20 15:25:30.501196
"""
import sqlalchemy as sa
from alembic import op
from database.alembic import utils
# revision identifiers, used by Alembic.
revision = "da1eeb0857d1"
down_revision = "4fb28123bc36"
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("my_item", schema=utils.get_schema()) as batch_op:
batch_op.add_column(sa.Column("price", sa.Float(), nullable=True))
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("my_item", schema=utils.get_schema()) as batch_op:
batch_op.drop_column("price")
# ### end Alembic commands ###
Error
database/alembic/versions/da1eeb0857d1_my_item_price.py:23: error: "BatchOperations" has no attribute "add_column"
database/alembic/versions/da1eeb0857d1_my_item_price.py:31: error: "BatchOperations" has no attribute "drop_column"
Versions.
- OS: macOS 12.6
- Python: 3.9.10
- Alembic: 1.8.1
- SQLAlchemy: 1.4.41
- Database: PostgreSQL
- DBAPI: ?
Additional context
Our current workaround is to ignore alembic errors in our mypy.ini:
[mypy-database.alembic.*]
ignore_errors = True
Hi,
Thanks for the report, this looks like a bug on alembic side.
CaselIT has proposed a fix for this issue in the main branch:
Add BatchOperations stub methods https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4518