Skip to content

Commit

Permalink
Merge pull request #34 from lsst/tickets/DM-38553
Browse files Browse the repository at this point in the history
Disable sqlalchemy 2 workaround for pandas 2 (DM-38553)
  • Loading branch information
andy-slac committed Apr 4, 2023
2 parents 0340944 + c4051f5 commit bebdf9f
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions python/lsst/dax/apdb/apdbSql.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,51 @@
_LOG = logging.getLogger(__name__)


class _ConnectionHackSA2(sqlalchemy.engine.Connectable):
"""Terrible hack to workaround Pandas incomplete support for sqlalchemy 2.
if pandas.__version__.partition(".")[0] == "1":

We need to pass a Connection instance to pandas method, but in SA 2 the
Connection class lost ``connect`` method which is used by Pandas.
"""
class _ConnectionHackSA2(sqlalchemy.engine.Connectable):
"""Terrible hack to workaround Pandas 1 incomplete support for
sqlalchemy 2.
We need to pass a Connection instance to pandas method, but in SA 2 the
Connection class lost ``connect`` method which is used by Pandas.
"""

def __init__(self, connection: sqlalchemy.engine.Connection):
self._connection = connection
def __init__(self, connection: sqlalchemy.engine.Connection):
self._connection = connection

def connect(self) -> Any:
return self
def connect(self, **kwargs: Any) -> Any:
return self

@property
def execute(self) -> Callable:
return self._connection.execute
@property
def execute(self) -> Callable:
return self._connection.execute

@property
def execution_options(self) -> Callable:
return self._connection.execution_options
@property
def execution_options(self) -> Callable:
return self._connection.execution_options

@property
def connection(self) -> Any:
return self._connection.connection
@property
def connection(self) -> Any:
return self._connection.connection

def __enter__(self) -> sqlalchemy.engine.Connection:
return self._connection
def __enter__(self) -> sqlalchemy.engine.Connection:
return self._connection

def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
# Do not close connection here
pass
def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
# Do not close connection here
pass

@inspection._inspects(_ConnectionHackSA2)
def _connection_insp(conn: _ConnectionHackSA2) -> Inspector:
return Inspector._construct(Inspector._init_connection, conn._connection)

@inspection._inspects(_ConnectionHackSA2)
def _connection_insp(conn: _ConnectionHackSA2) -> Inspector:
return Inspector._construct(Inspector._init_connection, conn._connection)
else:
# Pandas 2.0 supports SQLAlchemy 2 correctly.
def _ConnectionHackSA2( # type: ignore[no-redef]
conn: sqlalchemy.engine.Connectable,
) -> sqlalchemy.engine.Connectable:
return conn


def _coerce_uint64(df: pandas.DataFrame) -> pandas.DataFrame:
Expand Down

0 comments on commit bebdf9f

Please sign in to comment.