From 3355333e9d36b1e6d5bcb1d74b6fd8ebba2ed5b4 Mon Sep 17 00:00:00 2001 From: Tolker-KU Date: Tue, 25 Jul 2023 13:20:19 +0200 Subject: [PATCH 1/2] Fix sql params type --- pandas-stubs/io/sql.pyi | 9 +++++---- tests/test_io.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/io/sql.pyi b/pandas-stubs/io/sql.pyi index 09af416e3..6de3331b9 100644 --- a/pandas-stubs/io/sql.pyi +++ b/pandas-stubs/io/sql.pyi @@ -2,6 +2,7 @@ from collections.abc import ( Callable, Generator, Iterable, + Mapping, ) import sqlite3 from typing import ( @@ -65,7 +66,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[Any] | tuple[Any, ...] | Mapping[str, Any] | None = ..., parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ..., *, chunksize: int, @@ -78,7 +79,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[Any] | tuple[Any, ...] | Mapping[str, Any] | None = ..., parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ..., chunksize: None = ..., dtype: DtypeArg | None = ..., @@ -90,7 +91,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[Any] | tuple[Any, ...] | Mapping[str, Any] | None = ..., parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ..., columns: list[str] = ..., *, @@ -104,7 +105,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[Any] | tuple[Any, ...] | Mapping[str, Any] | None = ..., parse_dates: list[str] | dict[str, str] | dict[str, dict[str, Any]] | None = ..., columns: list[str] = ..., chunksize: None = ..., diff --git a/tests/test_io.py b/tests/test_io.py index 3832f705e..e1d9e8a02 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -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) @@ -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: From 3d573754bcb7ddc8d451a14c2dabf41047d73c34 Mon Sep 17 00:00:00 2001 From: Tolker-KU Date: Tue, 25 Jul 2023 16:04:41 +0200 Subject: [PATCH 2/2] Use Scalar instead of Any --- pandas-stubs/io/sql.pyi | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/io/sql.pyi b/pandas-stubs/io/sql.pyi index 6de3331b9..045982500 100644 --- a/pandas-stubs/io/sql.pyi +++ b/pandas-stubs/io/sql.pyi @@ -22,6 +22,7 @@ from pandas._libs.lib import NoDefault from pandas._typing import ( DtypeArg, DtypeBackend, + Scalar, npt, ) @@ -66,7 +67,7 @@ def read_sql_query( con: _SQLConnection, index_col: str | list[str] | None = ..., coerce_float: bool = ..., - params: list[Any] | tuple[Any, ...] | Mapping[str, Any] | 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, @@ -79,7 +80,7 @@ def read_sql_query( con: _SQLConnection, index_col: str | list[str] | None = ..., coerce_float: bool = ..., - params: list[Any] | tuple[Any, ...] | Mapping[str, Any] | 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 = ..., @@ -91,7 +92,7 @@ def read_sql( con: _SQLConnection, index_col: str | list[str] | None = ..., coerce_float: bool = ..., - params: list[Any] | tuple[Any, ...] | Mapping[str, Any] | 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] = ..., *, @@ -105,7 +106,7 @@ def read_sql( con: _SQLConnection, index_col: str | list[str] | None = ..., coerce_float: bool = ..., - params: list[Any] | tuple[Any, ...] | Mapping[str, Any] | 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 = ...,