From de4553013a9a54e7b93d776c336222a622254fab Mon Sep 17 00:00:00 2001 From: Alexander Beedie Date: Tue, 18 Jun 2024 00:06:31 +0400 Subject: [PATCH] fix: Harden "async" check for users with out-of-date `sqlalchemy` libraries (#17029) --- py-polars/polars/io/database/_executor.py | 25 +++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/py-polars/polars/io/database/_executor.py b/py-polars/polars/io/database/_executor.py index 49ddc6b6dc7c..d7b2d963dd06 100644 --- a/py-polars/polars/io/database/_executor.py +++ b/py-polars/polars/io/database/_executor.py @@ -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: