Skip to content

Commit f7aa277

Browse files
cdcadmanChuck Cadman
authored and
MarcoGorelli
committed
TYP:Replace union of subclasses with base class. (pandas-dev#49587)
Co-authored-by: Chuck Cadman <[email protected]>
1 parent 322a4de commit f7aa277

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

pandas/io/sql.py

+58-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
from __future__ import annotations
77

8+
from abc import (
9+
ABC,
10+
abstractmethod,
11+
)
812
from contextlib import contextmanager
913
from datetime import (
1014
date,
@@ -282,9 +286,7 @@ def read_sql_table(
282286
if not pandas_sql.has_table(table_name):
283287
raise ValueError(f"Table {table_name} not found")
284288

285-
# error: Item "SQLiteDatabase" of "Union[SQLDatabase, SQLiteDatabase]"
286-
# has no attribute "read_table"
287-
table = pandas_sql.read_table( # type: ignore[union-attr]
289+
table = pandas_sql.read_table(
288290
table_name,
289291
index_col=index_col,
290292
coerce_float=coerce_float,
@@ -578,7 +580,6 @@ def read_sql(
578580
_is_table_name = False
579581

580582
if _is_table_name:
581-
pandas_sql.meta.reflect(bind=pandas_sql.connectable, only=[sql])
582583
return pandas_sql.read_table(
583584
sql,
584585
index_col=index_col,
@@ -735,7 +736,7 @@ def has_table(table_name: str, con, schema: str | None = None) -> bool:
735736
table_exists = has_table
736737

737738

738-
def pandasSQL_builder(con, schema: str | None = None) -> SQLDatabase | SQLiteDatabase:
739+
def pandasSQL_builder(con, schema: str | None = None) -> PandasSQL:
739740
"""
740741
Convenience function to return the correct PandasSQL subclass based on the
741742
provided parameters.
@@ -1251,17 +1252,37 @@ def _get_dtype(self, sqltype):
12511252
return object
12521253

12531254

1254-
class PandasSQL(PandasObject):
1255+
class PandasSQL(PandasObject, ABC):
12551256
"""
1256-
Subclasses Should define read_sql and to_sql.
1257+
Subclasses Should define read_query and to_sql.
12571258
"""
12581259

1259-
def read_sql(self, *args, **kwargs):
1260-
raise ValueError(
1261-
"PandasSQL must be created with an SQLAlchemy "
1262-
"connectable or sqlite connection"
1263-
)
1260+
def read_table(
1261+
self,
1262+
table_name: str,
1263+
index_col: str | list[str] | None = None,
1264+
coerce_float: bool = True,
1265+
parse_dates=None,
1266+
columns=None,
1267+
schema: str | None = None,
1268+
chunksize: int | None = None,
1269+
) -> DataFrame | Iterator[DataFrame]:
1270+
raise NotImplementedError
12641271

1272+
@abstractmethod
1273+
def read_query(
1274+
self,
1275+
sql: str,
1276+
index_col: str | list[str] | None = None,
1277+
coerce_float: bool = True,
1278+
parse_dates=None,
1279+
params=None,
1280+
chunksize: int | None = None,
1281+
dtype: DtypeArg | None = None,
1282+
) -> DataFrame | Iterator[DataFrame]:
1283+
pass
1284+
1285+
@abstractmethod
12651286
def to_sql(
12661287
self,
12671288
frame,
@@ -1273,11 +1294,29 @@ def to_sql(
12731294
chunksize=None,
12741295
dtype: DtypeArg | None = None,
12751296
method=None,
1297+
engine: str = "auto",
1298+
**engine_kwargs,
12761299
) -> int | None:
1277-
raise ValueError(
1278-
"PandasSQL must be created with an SQLAlchemy "
1279-
"connectable or sqlite connection"
1280-
)
1300+
pass
1301+
1302+
@abstractmethod
1303+
def execute(self, *args, **kwargs):
1304+
pass
1305+
1306+
@abstractmethod
1307+
def has_table(self, name: str, schema: str | None = None) -> bool:
1308+
pass
1309+
1310+
@abstractmethod
1311+
def _create_sql_schema(
1312+
self,
1313+
frame: DataFrame,
1314+
table_name: str,
1315+
keys: list[str] | None = None,
1316+
dtype: DtypeArg | None = None,
1317+
schema: str | None = None,
1318+
):
1319+
pass
12811320

12821321

12831322
class BaseEngine:
@@ -2065,8 +2104,8 @@ def read_query(
20652104
sql,
20662105
index_col=None,
20672106
coerce_float: bool = True,
2068-
params=None,
20692107
parse_dates=None,
2108+
params=None,
20702109
chunksize: int | None = None,
20712110
dtype: DtypeArg | None = None,
20722111
) -> DataFrame | Iterator[DataFrame]:
@@ -2116,7 +2155,8 @@ def to_sql(
21162155
chunksize=None,
21172156
dtype: DtypeArg | None = None,
21182157
method=None,
2119-
**kwargs,
2158+
engine: str = "auto",
2159+
**engine_kwargs,
21202160
) -> int | None:
21212161
"""
21222162
Write records stored in a DataFrame to a SQL database.

0 commit comments

Comments
 (0)