Skip to content

Commit

Permalink
fix: Harden "async" check for users with out-of-date sqlalchemy lib…
Browse files Browse the repository at this point in the history
…raries (#17029)
  • Loading branch information
alexander-beedie authored Jun 17, 2024
1 parent 9093d75 commit de45530
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions py-polars/polars/io/database/_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,21 +318,30 @@ def _inject_type_overrides(
@staticmethod
def _is_alchemy_async(conn: Any) -> bool:
"""Check if the cursor/connection/session object is async."""
from sqlalchemy.ext.asyncio import (
AsyncConnection,
AsyncSession,
async_sessionmaker,
)
try:
from sqlalchemy.ext.asyncio import (
AsyncConnection,
AsyncSession,
async_sessionmaker,
)

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

@staticmethod
def _is_alchemy_engine(conn: Any) -> bool:
"""Check if the cursor/connection/session object is async."""
from sqlalchemy.engine import Engine
from sqlalchemy.ext.asyncio import AsyncEngine

return isinstance(conn, (Engine, AsyncEngine))
if isinstance(conn, Engine):
return True
try:
from sqlalchemy.ext.asyncio import AsyncEngine

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

@staticmethod
def _is_alchemy_session(conn: Any) -> bool:
Expand Down

0 comments on commit de45530

Please sign in to comment.