Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jonathanloske opened this issue Oct 6, 2022 · 2 comments
Labels
bug Something isn't working pep 484 typing related issues

Comments

@jonathanloske
Copy link

jonathanloske commented Oct 6, 2022

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
@jonathanloske jonathanloske added the requires triage New issue that requires categorization label Oct 6, 2022
@CaselIT
Copy link
Member

CaselIT commented Oct 6, 2022

Hi,

Thanks for the report, this looks like a bug on alembic side.

@CaselIT CaselIT added bug Something isn't working pep 484 typing related issues and removed requires triage New issue that requires categorization labels Oct 6, 2022
@sqla-tester
Copy link
Collaborator

CaselIT has proposed a fix for this issue in the main branch:

Add BatchOperations stub methods https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4518

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pep 484 typing related issues
Projects
None yet
Development

No branches or pull requests

3 participants