Closed
Description
Describe the bug
Some visit_XXX
compiler methods don't accept **kw
which means they blow up when .compile()
is called with compile_kwargs={...}
.
I specifically noticed this with the visit_create_index()
method on SQLiteCompiler
but from grepping the codebase there seem to be other dialects and methods with this issue.
Obviously render_postcompile
doesn't do anything in this case, but it would be good to be able to always pass this argument without having to worry which sorts of clause accept it.
Assuming this is accepted as a bug I'd be happy to submit a PR adding **kw
to the relevant methods. But I wanted to check first.
To Reproduce
from sqlalchemy import Column, Index, MetaData, Table
from sqlalchemy.schema import CreateIndex
from sqlalchemy.dialects.mssql.pymssql import MSDialect_pymssql
from sqlalchemy.dialects.mysql.pymysql import MySQLDialect_pymysql
from sqlalchemy.dialects.sqlite.pysqlite import SQLiteDialect_pysqlite
from sqlalchemy.engine.default import DefaultDialect
table = Table("table", MetaData(), Column("col"))
index = Index(None, table.c.col)
create_index = CreateIndex(index)
for dialect in [
DefaultDialect,
MSDialect_pymssql,
MySQLDialect_pymysql,
SQLiteDialect_pysqlite,
]:
print(f"Compiling with {dialect}")
query = create_index.compile(
dialect=dialect(), compile_kwargs={"render_postcompile": True}
)
print(query)
print()
Error
Compiling with <class 'sqlalchemy.engine.default.DefaultDialect'>
CREATE INDEX ix_table_col ON "table" (col)
Compiling with <class 'sqlalchemy.dialects.mssql.pymssql.MSDialect_pymssql'>
CREATE INDEX ix_table_col ON [table] (col)
Compiling with <class 'sqlalchemy.dialects.mysql.pymysql.MySQLDialect_pymysql'>
CREATE INDEX ix_table_col ON `table` (col)
Compiling with <class 'sqlalchemy.dialects.sqlite.pysqlite.SQLiteDialect_pysqlite'>
Traceback (most recent call last):
File "/home/dave/projects/ebmdatalab/databuilder/reproduce_error.py", line 14, in <module>
query = create_index.compile(dialect=dialect(), compile_kwargs={"render_postcompile": True})
File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 501, in compile
return self._compiler(dialect, **kw)
File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py", line 32, in _compiler
return dialect.ddl_compiler(dialect, self, **kw)
File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 451, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/compiler.py", line 486, in process
return obj._compiler_dispatch(self, **kwargs)
File "/home/dave/.virtualenvs/ebm-databuilder/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py", line 82, in _compiler_dispatch
return meth(self, **kw)
TypeError: visit_create_index() got an unexpected keyword argument 'render_postcompile'
Versions
- Python: 3.9
- SQLAlchemy: 1.4.39
Additional context
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
CaselIT commentedon Dec 15, 2022
Hi,
thanks for reporting.
For the PR let's wait for Mike, since I don't know if there was a rationale for not having the kw in some methods
zzzeek commentedon Dec 15, 2022
they've just not been needed for DDL compiler methods. "render_postcompile" is fairly recent, so kw can be added.
what would be nice though would be some way to assert all visit_ methods accept kw. I would propose gathering all SQLCompiler classes (using
__subclasses__()
) and then running through allvisit_XYZ
methods withinspect.getfullargspec()
to confirm.CaselIT commentedon Dec 15, 2022
I think we need to use the compuler as the superclass, since ddlcompiler does not inherit from sqlcompiler.
zzzeek commentedon Dec 15, 2022
yes, Compiler
sqla-tester commentedon Dec 16, 2022
Mike Bayer has proposed a fix for this issue in the main branch:
**ensure all visit methods accept kw https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/4307
4 remaining items