Skip to content

Commit 48b7e30

Browse files
committed
Restore previous behaviour when con is a str without sqlalchemy installed
1 parent 7c0a246 commit 48b7e30

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pandas/io/sql.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,14 @@ def pandasSQL_builder(con, schema: str | None = None):
738738

739739
sqlalchemy = import_optional_dependency("sqlalchemy", errors="ignore")
740740

741-
if sqlalchemy is not None:
742-
if isinstance(con, str):
741+
if isinstance(con, str):
742+
if sqlalchemy is None:
743+
raise ImportError("Using URI string without sqlalchemy installed.")
744+
else:
743745
con = sqlalchemy.create_engine(con)
744746

745-
if isinstance(con, sqlalchemy.engine.Connectable):
746-
return SQLDatabase(con, schema=schema)
747+
if sqlalchemy is not None and isinstance(con, sqlalchemy.engine.Connectable):
748+
return SQLDatabase(con, schema=schema)
747749

748750
warnings.warn(
749751
"pandas only support SQLAlchemy connectable(engine/connection) or"

pandas/tests/io/test_sql.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,8 @@ def test_sql_open_close(self, test_frame3):
15241524
@pytest.mark.skipif(SQLALCHEMY_INSTALLED, reason="SQLAlchemy is installed")
15251525
def test_con_string_import_error(self):
15261526
conn = "mysql://root@localhost/pandas"
1527-
with pytest.raises(ImportError, match="SQLAlchemy"):
1527+
msg = "Using URI string without sqlalchemy installed"
1528+
with pytest.raises(ImportError, match=msg):
15281529
sql.read_sql("SELECT * FROM iris", conn)
15291530

15301531
@pytest.mark.skipif(SQLALCHEMY_INSTALLED, reason="SQLAlchemy is installed")

0 commit comments

Comments
 (0)