Skip to content

Commit 193095a

Browse files
bashtageKevin Sheppard
and
Kevin Sheppard
authored
ENH: Allow TextClause (#316)
Allow sqlalchemy TextClause for sql select statements Co-authored-by: Kevin Sheppard <kevin.sheppard@gmail.com>
1 parent b502889 commit 193095a

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

pandas-stubs/io/sql.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ _SQLConnection = Union[
2828
sqlite3.Connection,
2929
]
3030

31+
_SQLStatement = Union[
32+
str, sqlalchemy.sql.expression.Selectable, sqlalchemy.sql.expression.TextClause
33+
]
34+
3135
@overload
3236
def read_sql_table(
3337
table_name: str,
@@ -53,7 +57,7 @@ def read_sql_table(
5357
) -> DataFrame: ...
5458
@overload
5559
def read_sql_query(
56-
sql: str | sqlalchemy.sql.expression.Selectable,
60+
sql: _SQLStatement,
5761
con: _SQLConnection,
5862
index_col: str | list[str] | None = ...,
5963
coerce_float: bool = ...,
@@ -65,7 +69,7 @@ def read_sql_query(
6569
) -> Generator[DataFrame, None, None]: ...
6670
@overload
6771
def read_sql_query(
68-
sql: str | sqlalchemy.sql.expression.Selectable,
72+
sql: _SQLStatement,
6973
con: _SQLConnection,
7074
index_col: str | list[str] | None = ...,
7175
coerce_float: bool = ...,
@@ -76,7 +80,7 @@ def read_sql_query(
7680
) -> DataFrame: ...
7781
@overload
7882
def read_sql(
79-
sql: str | sqlalchemy.sql.expression.Selectable,
83+
sql: _SQLStatement,
8084
con: _SQLConnection,
8185
index_col: str | list[str] | None = ...,
8286
coerce_float: bool = ...,
@@ -88,7 +92,7 @@ def read_sql(
8892
) -> Generator[DataFrame, None, None]: ...
8993
@overload
9094
def read_sql(
91-
sql: str | sqlalchemy.sql.expression.Selectable,
95+
sql: _SQLStatement,
9296
con: _SQLConnection,
9397
index_col: str | list[str] | None = ...,
9498
coerce_float: bool = ...,

tests/test_io.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,3 +819,16 @@ class Temp(Base):
819819
pd.read_sql(
820820
session.query(Temp.quantity).statement, session.connection()
821821
)
822+
823+
824+
def test_sqlalchemy_text() -> None:
825+
with ensure_clean() as path:
826+
db_uri = "sqlite:///" + path
827+
engine = sqlalchemy.create_engine(db_uri)
828+
sql_select = sqlalchemy.text("select * from test")
829+
with engine.connect() as conn:
830+
check(assert_type(DF.to_sql("test", con=conn), Union[int, None]), int)
831+
check(
832+
assert_type(read_sql(sql_select, con=conn), DataFrame),
833+
DataFrame,
834+
)

0 commit comments

Comments
 (0)