Skip to content

Wrong type for sqltext argument of op.execute #1277

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

Closed
lanzz opened this issue Jul 17, 2023 · 4 comments
Closed

Wrong type for sqltext argument of op.execute #1277

lanzz opened this issue Jul 17, 2023 · 4 comments
Labels
bug Something isn't working pep 484 typing related issues

Comments

@lanzz
Copy link
Contributor

lanzz commented Jul 17, 2023

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!

@lanzz lanzz added the requires triage New issue that requires categorization label Jul 17, 2023
@zzzeek zzzeek added bug Something isn't working pep 484 typing related issues and removed requires triage New issue that requires categorization labels Jul 17, 2023
@zzzeek
Copy link
Member

zzzeek commented Jul 17, 2023

cc @CaselIT that annotation looks like it was generated from a tool of some kind, it's completely wrong

@CaselIT
Copy link
Member

CaselIT commented Jul 17, 2023

it was generated from the test in the initial typing of alembic, so it's most likely not really correct :)

@sqla-tester
Copy link
Collaborator

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

Accept insert and delete expressions in op.execute https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4835

@sqla-tester
Copy link
Collaborator

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

Improve typing of op.execute https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/4835

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

Successfully merging a pull request may close this issue.

4 participants