Skip to content

Commit 4ce84de

Browse files
authored
CLN: remove sqlalchemy<14 compat (pandas-dev#45410)
1 parent 53c8e1d commit 4ce84de

File tree

2 files changed

+38
-103
lines changed

2 files changed

+38
-103
lines changed

pandas/io/sql.py

+10-38
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from pandas.core.base import PandasObject
4747
import pandas.core.common as com
4848
from pandas.core.tools.datetimes import to_datetime
49-
from pandas.util.version import Version
5049

5150

5251
class DatabaseError(OSError):
@@ -57,16 +56,6 @@ class DatabaseError(OSError):
5756
# -- Helper functions
5857

5958

60-
def _gt14() -> bool:
61-
"""
62-
Check if sqlalchemy.__version__ is at least 1.4.0, when several
63-
deprecations were made.
64-
"""
65-
import sqlalchemy
66-
67-
return Version(sqlalchemy.__version__) >= Version("1.4.0")
68-
69-
7059
def _convert_params(sql, params):
7160
"""Convert SQL and params args to DBAPI2.0 compliant format."""
7261
args = [sql]
@@ -814,10 +803,7 @@ def sql_schema(self):
814803

815804
def _execute_create(self):
816805
# Inserting table into database, add to MetaData object
817-
if _gt14():
818-
self.table = self.table.to_metadata(self.pd_sql.meta)
819-
else:
820-
self.table = self.table.tometadata(self.pd_sql.meta)
806+
self.table = self.table.to_metadata(self.pd_sql.meta)
821807
self.table.create(bind=self.pd_sql.connectable)
822808

823809
def create(self):
@@ -986,10 +972,9 @@ def read(self, coerce_float=True, parse_dates=None, columns=None, chunksize=None
986972
if self.index is not None:
987973
for idx in self.index[::-1]:
988974
cols.insert(0, self.table.c[idx])
989-
sql_select = select(*cols) if _gt14() else select(cols)
975+
sql_select = select(*cols)
990976
else:
991-
sql_select = select(self.table) if _gt14() else self.table.select()
992-
977+
sql_select = select(self.table)
993978
result = self.pd_sql.execute(sql_select)
994979
column_names = result.keys()
995980

@@ -1633,19 +1618,11 @@ def check_case_sensitive(
16331618
if not name.isdigit() and not name.islower():
16341619
# check for potentially case sensitivity issues (GH7815)
16351620
# Only check when name is not a number and name is not lower case
1636-
engine = self.connectable.engine
1637-
with self.connectable.connect() as conn:
1638-
if _gt14():
1639-
from sqlalchemy import inspect
1621+
from sqlalchemy import inspect
16401622

1641-
insp = inspect(conn)
1642-
table_names = insp.get_table_names(
1643-
schema=schema or self.meta.schema
1644-
)
1645-
else:
1646-
table_names = engine.table_names(
1647-
schema=schema or self.meta.schema, connection=conn
1648-
)
1623+
with self.connectable.connect() as conn:
1624+
insp = inspect(conn)
1625+
table_names = insp.get_table_names(schema=schema or self.meta.schema)
16491626
if name not in table_names:
16501627
msg = (
16511628
f"The provided table name '{name}' is not found exactly as "
@@ -1749,15 +1726,10 @@ def tables(self):
17491726
return self.meta.tables
17501727

17511728
def has_table(self, name: str, schema: str | None = None):
1752-
if _gt14():
1753-
from sqlalchemy import inspect
1729+
from sqlalchemy import inspect
17541730

1755-
insp = inspect(self.connectable)
1756-
return insp.has_table(name, schema or self.meta.schema)
1757-
else:
1758-
return self.connectable.run_callable(
1759-
self.connectable.dialect.has_table, name, schema or self.meta.schema
1760-
)
1731+
insp = inspect(self.connectable)
1732+
return insp.has_table(name, schema or self.meta.schema)
17611733

17621734
def get_table(self, table_name: str, schema: str | None = None):
17631735
from sqlalchemy import (

pandas/tests/io/test_sql.py

+28-65
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
SQLAlchemyEngine,
5959
SQLDatabase,
6060
SQLiteDatabase,
61-
_gt14,
6261
get_engine,
6362
pandasSQL_builder,
6463
read_sql_query,
@@ -385,10 +384,10 @@ def mysql_pymysql_engine(iris_path, types_data):
385384
"mysql+pymysql://root@localhost:3306/pandas",
386385
connect_args={"client_flag": pymysql.constants.CLIENT.MULTI_STATEMENTS},
387386
)
388-
check_target = sqlalchemy.inspect(engine) if _gt14() else engine
389-
if not check_target.has_table("iris"):
387+
insp = sqlalchemy.inspect(engine)
388+
if not insp.has_table("iris"):
390389
create_and_load_iris(engine, iris_path, "mysql")
391-
if not check_target.has_table("types"):
390+
if not insp.has_table("types"):
392391
for entry in types_data:
393392
entry.pop("DateColWithTz")
394393
create_and_load_types(engine, types_data, "mysql")
@@ -412,10 +411,10 @@ def postgresql_psycopg2_engine(iris_path, types_data):
412411
engine = sqlalchemy.create_engine(
413412
"postgresql+psycopg2://postgres:postgres@localhost:5432/pandas"
414413
)
415-
check_target = sqlalchemy.inspect(engine) if _gt14() else engine
416-
if not check_target.has_table("iris"):
414+
insp = sqlalchemy.inspect(engine)
415+
if not insp.has_table("iris"):
417416
create_and_load_iris(engine, iris_path, "postgresql")
418-
if not check_target.has_table("types"):
417+
if not insp.has_table("types"):
419418
create_and_load_types(engine, types_data, "postgresql")
420419
yield engine
421420
with engine.connect() as conn:
@@ -1450,8 +1449,7 @@ def test_query_by_select_obj(self):
14501449
)
14511450

14521451
iris = iris_table_metadata(self.flavor)
1453-
iris_select = iris if _gt14() else [iris]
1454-
name_select = select(iris_select).where(iris.c.Name == bindparam("name"))
1452+
name_select = select(iris).where(iris.c.Name == bindparam("name"))
14551453
iris_df = sql.read_sql(name_select, self.conn, params={"name": "Iris-setosa"})
14561454
all_names = set(iris_df["Name"])
14571455
assert all_names == {"Iris-setosa"}
@@ -1624,46 +1622,33 @@ def test_to_sql_empty(self, test_frame1):
16241622
self._to_sql_empty(test_frame1)
16251623

16261624
def test_create_table(self):
1625+
from sqlalchemy import inspect
1626+
16271627
temp_conn = self.connect()
16281628
temp_frame = DataFrame(
16291629
{"one": [1.0, 2.0, 3.0, 4.0], "two": [4.0, 3.0, 2.0, 1.0]}
16301630
)
1631-
16321631
pandasSQL = sql.SQLDatabase(temp_conn)
16331632
assert pandasSQL.to_sql(temp_frame, "temp_frame") == 4
16341633

1635-
if _gt14():
1636-
from sqlalchemy import inspect
1637-
1638-
insp = inspect(temp_conn)
1639-
assert insp.has_table("temp_frame")
1640-
else:
1641-
assert temp_conn.has_table("temp_frame")
1634+
insp = inspect(temp_conn)
1635+
assert insp.has_table("temp_frame")
16421636

16431637
def test_drop_table(self):
1644-
temp_conn = self.connect()
1638+
from sqlalchemy import inspect
16451639

1640+
temp_conn = self.connect()
16461641
temp_frame = DataFrame(
16471642
{"one": [1.0, 2.0, 3.0, 4.0], "two": [4.0, 3.0, 2.0, 1.0]}
16481643
)
1649-
16501644
pandasSQL = sql.SQLDatabase(temp_conn)
16511645
assert pandasSQL.to_sql(temp_frame, "temp_frame") == 4
16521646

1653-
if _gt14():
1654-
from sqlalchemy import inspect
1655-
1656-
insp = inspect(temp_conn)
1657-
assert insp.has_table("temp_frame")
1658-
else:
1659-
assert temp_conn.has_table("temp_frame")
1647+
insp = inspect(temp_conn)
1648+
assert insp.has_table("temp_frame")
16601649

16611650
pandasSQL.drop_table("temp_frame")
1662-
1663-
if _gt14():
1664-
assert not insp.has_table("temp_frame")
1665-
else:
1666-
assert not temp_conn.has_table("temp_frame")
1651+
assert not insp.has_table("temp_frame")
16671652

16681653
def test_roundtrip(self, test_frame1):
16691654
self._roundtrip(test_frame1)
@@ -2156,14 +2141,10 @@ def bar(connection, data):
21562141
data.to_sql(name="test_foo_data", con=connection, if_exists="append")
21572142

21582143
def baz(conn):
2159-
if _gt14():
2160-
# https://github.com/sqlalchemy/sqlalchemy/commit/
2161-
# 00b5c10846e800304caa86549ab9da373b42fa5d#r48323973
2162-
foo_data = foo(conn)
2163-
bar(conn, foo_data)
2164-
else:
2165-
foo_data = conn.run_callable(foo)
2166-
conn.run_callable(bar, foo_data)
2144+
# https://github.com/sqlalchemy/sqlalchemy/commit/
2145+
# 00b5c10846e800304caa86549ab9da373b42fa5d#r48323973
2146+
foo_data = foo(conn)
2147+
bar(conn, foo_data)
21672148

21682149
def main(connectable):
21692150
if isinstance(connectable, Engine):
@@ -2216,14 +2197,9 @@ def test_temporary_table(self):
22162197
)
22172198
from sqlalchemy.orm import (
22182199
Session,
2219-
sessionmaker,
2200+
declarative_base,
22202201
)
22212202

2222-
if _gt14():
2223-
from sqlalchemy.orm import declarative_base
2224-
else:
2225-
from sqlalchemy.ext.declarative import declarative_base
2226-
22272203
test_data = "Hello, World!"
22282204
expected = DataFrame({"spam": [test_data]})
22292205
Base = declarative_base()
@@ -2234,24 +2210,13 @@ class Temporary(Base):
22342210
id = Column(Integer, primary_key=True)
22352211
spam = Column(Unicode(30), nullable=False)
22362212

2237-
if _gt14():
2238-
with Session(self.conn) as session:
2239-
with session.begin():
2240-
conn = session.connection()
2241-
Temporary.__table__.create(conn)
2242-
session.add(Temporary(spam=test_data))
2243-
session.flush()
2244-
df = sql.read_sql_query(sql=select(Temporary.spam), con=conn)
2245-
else:
2246-
Session = sessionmaker()
2247-
session = Session(bind=self.conn)
2248-
with session.transaction:
2213+
with Session(self.conn) as session:
2214+
with session.begin():
22492215
conn = session.connection()
22502216
Temporary.__table__.create(conn)
22512217
session.add(Temporary(spam=test_data))
22522218
session.flush()
2253-
df = sql.read_sql_query(sql=select([Temporary.spam]), con=conn)
2254-
2219+
df = sql.read_sql_query(sql=select(Temporary.spam), con=conn)
22552220
tm.assert_frame_equal(df, expected)
22562221

22572222
# -- SQL Engine tests (in the base class for now)
@@ -2349,12 +2314,10 @@ def test_row_object_is_named_tuple(self):
23492314
Integer,
23502315
String,
23512316
)
2352-
from sqlalchemy.orm import sessionmaker
2353-
2354-
if _gt14():
2355-
from sqlalchemy.orm import declarative_base
2356-
else:
2357-
from sqlalchemy.ext.declarative import declarative_base
2317+
from sqlalchemy.orm import (
2318+
declarative_base,
2319+
sessionmaker,
2320+
)
23582321

23592322
BaseModel = declarative_base()
23602323

0 commit comments

Comments
 (0)