Skip to content

Commit 5a9abdd

Browse files
authored
Fix SQL parameter type (#757)
* Fix sql params type * Use Scalar instead of Any
1 parent 89e083b commit 5a9abdd

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

pandas-stubs/io/sql.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ from collections.abc import (
22
Callable,
33
Generator,
44
Iterable,
5+
Mapping,
56
)
67
import sqlite3
78
from typing import (
@@ -21,6 +22,7 @@ from pandas._libs.lib import NoDefault
2122
from pandas._typing import (
2223
DtypeArg,
2324
DtypeBackend,
25+
Scalar,
2426
npt,
2527
)
2628

@@ -65,7 +67,7 @@ def read_sql_query(
6567
con: _SQLConnection,
6668
index_col: str | list[str] | None = ...,
6769
coerce_float: bool = ...,
68-
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
70+
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
6971
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
7072
*,
7173
chunksize: int,
@@ -78,7 +80,7 @@ def read_sql_query(
7880
con: _SQLConnection,
7981
index_col: str | list[str] | None = ...,
8082
coerce_float: bool = ...,
81-
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
83+
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
8284
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
8385
chunksize: None = ...,
8486
dtype: DtypeArg | None = ...,
@@ -90,7 +92,7 @@ def read_sql(
9092
con: _SQLConnection,
9193
index_col: str | list[str] | None = ...,
9294
coerce_float: bool = ...,
93-
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
95+
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
9496
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
9597
columns: list[str] = ...,
9698
*,
@@ -104,7 +106,7 @@ def read_sql(
104106
con: _SQLConnection,
105107
index_col: str | list[str] | None = ...,
106108
coerce_float: bool = ...,
107-
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
109+
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
108110
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
109111
columns: list[str] = ...,
110112
chunksize: None = ...,

tests/test_io.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,26 @@ def test_read_sql_via_sqlalchemy_engine():
11411141
engine.dispose()
11421142

11431143

1144+
def test_read_sql_via_sqlalchemy_engine_with_params():
1145+
with ensure_clean() as path:
1146+
db_uri = "sqlite:///" + path
1147+
engine = sqlalchemy.create_engine(db_uri)
1148+
1149+
check(assert_type(DF.to_sql("test", con=engine), Union[int, None]), int)
1150+
check(
1151+
assert_type(
1152+
read_sql(
1153+
"select * from test where a = :a and b = :b",
1154+
con=engine,
1155+
params={"a": 2, "b": 0.0},
1156+
),
1157+
DataFrame,
1158+
),
1159+
DataFrame,
1160+
)
1161+
engine.dispose()
1162+
1163+
11441164
def test_read_sql_generator():
11451165
with ensure_clean() as path:
11461166
con = sqlite3.connect(path)
@@ -1200,6 +1220,26 @@ def test_read_sql_query_generator():
12001220
con.close()
12011221

12021222

1223+
def test_read_sql_query_via_sqlalchemy_engine_with_params():
1224+
with ensure_clean() as path:
1225+
db_uri = "sqlite:///" + path
1226+
engine = sqlalchemy.create_engine(db_uri)
1227+
1228+
check(assert_type(DF.to_sql("test", con=engine), Union[int, None]), int)
1229+
check(
1230+
assert_type(
1231+
read_sql_query(
1232+
"select * from test where a = :a and b = :b",
1233+
con=engine,
1234+
params={"a": 2, "b": 0.0},
1235+
),
1236+
DataFrame,
1237+
),
1238+
DataFrame,
1239+
)
1240+
engine.dispose()
1241+
1242+
12031243
def test_read_html():
12041244
check(assert_type(DF.to_html(), str), str)
12051245
with ensure_clean() as path:

0 commit comments

Comments
 (0)