Skip to content

Commit

Permalink
fix(python): Harden alchemy session for old sqlalchemy versions (#17366)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobbaraujo authored Jul 6, 2024
1 parent f0a82d9 commit 03a769d
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions py-polars/polars/io/database/_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,18 @@ def _is_alchemy_engine(conn: Any) -> bool:
@staticmethod
def _is_alchemy_session(conn: Any) -> bool:
"""Check if the cursor/connection/session object is async."""
from sqlalchemy.ext.asyncio import (
AsyncSession,
async_sessionmaker,
)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session, sessionmaker

return isinstance(
conn, (Session, sessionmaker, AsyncSession, async_sessionmaker)
)
if isinstance(conn, (AsyncSession, Session, sessionmaker)):
return True

try:
from sqlalchemy.ext.asyncio import async_sessionmaker

return isinstance(conn, async_sessionmaker)
except ImportError:
return False

def _normalise_cursor(self, conn: Any) -> Cursor:
"""Normalise a connection object such that we have the query executor."""
Expand Down

0 comments on commit 03a769d

Please sign in to comment.