Skip to content

Wrong type for sqltext argument of op.execute #1277

Description

@lanzz

Describe the bug
Documentation for op.execute describes the sqltext argument thus:

Parameters: sqltext – Any legal SQLAlchemy expression, including:

But the sqltext argument is actually declared as sqltext: str | TextClause | UpdateTextClause and Update are the only SQL expressions it is typed to accept, and any attempt to use any other expression type (e.g. an Insert or Delete) will cause mypy to not validate the call, and IDE typecheckers (e.g. VS Code's Pylance) to highlight an error.

Also, sqlalchemy.sql.expression.insert() is listed once as its own bullet point, and a second time in a separate bullet point, together with ...update() and ...delete().

Expected behavior
op.execute should be declared to accept all of the documented types of SQL expressions. Using expressions documented as acceptable should not produce mypy errors.

To Reproduce

# in migration script:
...
def upgrade() -> None:
    table = op.create_table(...)
    op.execute(table.insert().values(...))
...

Error

$ mypy alembic/versions/...version_filename....py 
...version_filename....py:52: error: Argument 1 to "execute" has incompatible type "Insert"; expected "str | TextClause | Update"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Versions.

  • OS: macOS 13.4.1
  • Python: 3.11.4
  • Alembic: 1.11.1
  • SQLAlchemy: 2.0.19
  • Database: irrelevant
  • DBAPI: irrelevant

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpep 484typing related issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions