Skip to content

Fix SQL parameter type #757

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 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions pandas-stubs/io/sql.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from collections.abc import (
Callable,
Generator,
Iterable,
Mapping,
)
import sqlite3
from typing import (
Expand All @@ -21,6 +22,7 @@ from pandas._libs.lib import NoDefault
from pandas._typing import (
DtypeArg,
DtypeBackend,
Scalar,
npt,
)

Expand Down Expand Up @@ -65,7 +67,7 @@ def read_sql_query(
con: _SQLConnection,
index_col: str | list[str] | None = ...,
coerce_float: bool = ...,
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
*,
chunksize: int,
Expand All @@ -78,7 +80,7 @@ def read_sql_query(
con: _SQLConnection,
index_col: str | list[str] | None = ...,
coerce_float: bool = ...,
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
chunksize: None = ...,
dtype: DtypeArg | None = ...,
Expand All @@ -90,7 +92,7 @@ def read_sql(
con: _SQLConnection,
index_col: str | list[str] | None = ...,
coerce_float: bool = ...,
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
columns: list[str] = ...,
*,
Expand All @@ -104,7 +106,7 @@ def read_sql(
con: _SQLConnection,
index_col: str | list[str] | None = ...,
coerce_float: bool = ...,
params: list[str] | tuple[str, ...] | dict[str, str] | None = ...,
params: list[Scalar] | tuple[Scalar, ...] | Mapping[str, Scalar] | None = ...,
parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ...,
columns: list[str] = ...,
chunksize: None = ...,
Expand Down
40 changes: 40 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,26 @@ def test_read_sql_via_sqlalchemy_engine():
engine.dispose()


def test_read_sql_via_sqlalchemy_engine_with_params():
with ensure_clean() as path:
db_uri = "sqlite:///" + path
engine = sqlalchemy.create_engine(db_uri)

check(assert_type(DF.to_sql("test", con=engine), Union[int, None]), int)
check(
assert_type(
read_sql(
"select * from test where a = :a and b = :b",
con=engine,
params={"a": 2, "b": 0.0},
),
DataFrame,
),
DataFrame,
)
engine.dispose()


def test_read_sql_generator():
with ensure_clean() as path:
con = sqlite3.connect(path)
Expand Down Expand Up @@ -1200,6 +1220,26 @@ def test_read_sql_query_generator():
con.close()


def test_read_sql_query_via_sqlalchemy_engine_with_params():
with ensure_clean() as path:
db_uri = "sqlite:///" + path
engine = sqlalchemy.create_engine(db_uri)

check(assert_type(DF.to_sql("test", con=engine), Union[int, None]), int)
check(
assert_type(
read_sql_query(
"select * from test where a = :a and b = :b",
con=engine,
params={"a": 2, "b": 0.0},
),
DataFrame,
),
DataFrame,
)
engine.dispose()


def test_read_html():
check(assert_type(DF.to_html(), str), str)
with ensure_clean() as path:
Expand Down