Skip to content

TYP:Replace union of subclasses with base class. #49587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 58 additions & 18 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

from __future__ import annotations

from abc import (
ABC,
abstractmethod,
)
from contextlib import contextmanager
from datetime import (
date,
Expand Down Expand Up @@ -282,9 +286,7 @@ def read_sql_table(
if not pandas_sql.has_table(table_name):
raise ValueError(f"Table {table_name} not found")

# error: Item "SQLiteDatabase" of "Union[SQLDatabase, SQLiteDatabase]"
# has no attribute "read_table"
table = pandas_sql.read_table( # type: ignore[union-attr]
table = pandas_sql.read_table(
table_name,
index_col=index_col,
coerce_float=coerce_float,
Expand Down Expand Up @@ -578,7 +580,6 @@ def read_sql(
_is_table_name = False

if _is_table_name:
pandas_sql.meta.reflect(bind=pandas_sql.connectable, only=[sql])
return pandas_sql.read_table(
sql,
index_col=index_col,
Expand Down Expand Up @@ -735,7 +736,7 @@ def has_table(table_name: str, con, schema: str | None = None) -> bool:
table_exists = has_table


def pandasSQL_builder(con, schema: str | None = None) -> SQLDatabase | SQLiteDatabase:
def pandasSQL_builder(con, schema: str | None = None) -> PandasSQL:
"""
Convenience function to return the correct PandasSQL subclass based on the
provided parameters.
Expand Down Expand Up @@ -1252,17 +1253,37 @@ def _get_dtype(self, sqltype):
return object


class PandasSQL(PandasObject):
class PandasSQL(PandasObject, ABC):
"""
Subclasses Should define read_sql and to_sql.
Subclasses Should define read_query and to_sql.
"""

def read_sql(self, *args, **kwargs):
raise ValueError(
"PandasSQL must be created with an SQLAlchemy "
"connectable or sqlite connection"
)
def read_table(
self,
table_name: str,
index_col: str | list[str] | None = None,
coerce_float: bool = True,
parse_dates=None,
columns=None,
schema: str | None = None,
chunksize: int | None = None,
) -> DataFrame | Iterator[DataFrame]:
raise NotImplementedError

@abstractmethod
def read_query(
self,
sql: str,
index_col: str | list[str] | None = None,
coerce_float: bool = True,
parse_dates=None,
params=None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
) -> DataFrame | Iterator[DataFrame]:
pass

@abstractmethod
def to_sql(
self,
frame,
Expand All @@ -1274,11 +1295,29 @@ def to_sql(
chunksize=None,
dtype: DtypeArg | None = None,
method=None,
engine: str = "auto",
**engine_kwargs,
) -> int | None:
raise ValueError(
"PandasSQL must be created with an SQLAlchemy "
"connectable or sqlite connection"
)
pass

@abstractmethod
def execute(self, *args, **kwargs):
pass

@abstractmethod
def has_table(self, name: str, schema: str | None = None) -> bool:
pass

@abstractmethod
def _create_sql_schema(
self,
frame: DataFrame,
table_name: str,
keys: list[str] | None = None,
dtype: DtypeArg | None = None,
schema: str | None = None,
):
pass


class BaseEngine:
Expand Down Expand Up @@ -2066,8 +2105,8 @@ def read_query(
sql,
index_col=None,
coerce_float: bool = True,
params=None,
parse_dates=None,
params=None,
chunksize: int | None = None,
dtype: DtypeArg | None = None,
) -> DataFrame | Iterator[DataFrame]:
Expand Down Expand Up @@ -2117,7 +2156,8 @@ def to_sql(
chunksize=None,
dtype: DtypeArg | None = None,
method=None,
**kwargs,
engine: str = "auto",
**engine_kwargs,
) -> int | None:
"""
Write records stored in a DataFrame to a SQL database.
Expand Down