2.1: TypeError "Expected tuple, got Row" when DBAPI returns tuple subclasses (e.g. databricks-sql-connector)
Describe the bug
Starting in 2.1, fetching rows fails with TypeError: Expected tuple, got Row when the DBAPI cursor returns rows that are a subclass of tuple rather than a plain tuple. The Databricks SQL connector (databricks-sql-connector) does this: its databricks.sql.types.Row subclasses tuple, so every query through databricks-sqlalchemy fails on SQLAlchemy 2.1.x. It works on 2.0.x.
It looks like this comes from 95ab6ed79c7956bcf6ad7e18ad6241f0c9c90f23 ("perf tuning of BaseRow"), which changed the row construction from
data_tuple: Tuple[Any, ...] = (
_apply_processors(processors, data)
if processors is not None
else tuple(data)
)
to
self._set_attrs(
parent,
key_to_index,
(
_apply_processors(processors, data)
if processors is not None
else data if isinstance(data, tuple) else tuple(data)
),
)
A tuple subclass passes isinstance(data, tuple), so it is passed through as-is, but the compiled BaseRow then requires an exact tuple and raises. Using type(data) is tuple (or otherwise converting subclasses) would restore the 2.0 behavior.
Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected
No response
SQLAlchemy Version in Use
2.1.1 (also 2.1.0); works on 2.0.49
DBAPI (i.e. the database driver)
databricks-sql-connector 4.2.6 (reproduced below with sqlite3 and a tuple-subclass row)
Database Vendor and Major Version
Databricks SQL (reproduced below with SQLite)
Python Version
3.13
Operating system
Linux (production), macOS (repro)
To Reproduce
from sqlalchemy import create_engine, event, text
class Row(tuple):
"""Stand-in for databricks.sql.types.Row, a tuple subclass."""
class Cursor:
def __init__(self, real):
self._real = real
def __getattr__(self, name):
return getattr(self._real, name)
def fetchall(self):
return [Row(r) for r in self._real.fetchall()]
def fetchone(self):
r = self._real.fetchone()
return None if r is None else Row(r)
def fetchmany(self, *args):
return [Row(r) for r in self._real.fetchmany(*args)]
engine = create_engine("sqlite://")
@event.listens_for(engine, "do_execute")
def _wrap_cursor(cursor, statement, parameters, context):
cursor.execute(statement, parameters)
context.cursor = Cursor(cursor)
return True
with engine.connect() as conn:
print(conn.execute(text("select 1 as a, 'x' as b")).all())
Error
Traceback (most recent call last):
...
File "lib/sqlalchemy/engine/_result_cy.py", line 201, in sqlalchemy.engine._result_cy.BaseResultInternal._row_getter.many_rows_simple
File "lib/sqlalchemy/engine/_row_cy.py", line 72, in sqlalchemy.engine._row_cy.BaseRow.__init__
TypeError: Expected tuple, got Row
On 2.0.49 the same script prints [(1, 'x')].
Additional context
No response
Hi,
Thanks for reporting. I'm wondering if we could instead require sequence.
I think asyncpg also returns rows that are not tuples, but I don't know if they are tuples subclasses or not.
I'm wondering if we could instead require sequence.
it's likely best to avoid this, using the suggested type(...) is tuple instead
Hi, I'd like to work on this issue.
I've reviewed the reproduction and the current BaseRow handling. The isinstance(data, tuple) check allows tuple subclasses through, while the Cython BaseRow expects an exact tuple. The suggested type(data) is tuple approach looks like the right direction, with a regression test covering tuple subclasses.
Could you please assign this issue to me and mark it as open for pull requests? I won't open a PR until the issue is approved for external contribution.
Mike Bayer has proposed a fix for this issue in the main branch:
convert tuple subclasses to plain tuple in BaseRow https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/7008
Federico Caselli has proposed a fix for this issue in the main branch:
Avoid type error in base row https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/7009