Skip to content

Using mypy with alembic 1.8.1 produces error: "BatchOperations" has no attribute XYZ #1093

@jonathanloske

Description

@jonathanloske

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpep 484typing related issues

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions