From fdfc6f4ad63cb2937a073aa152ca0cb25b723ddb Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Wed, 3 May 2023 23:11:46 +0530 Subject: [PATCH 1/4] GH-676 --- pandas-stubs/_typing.pyi | 2 +- tests/test_io.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pandas-stubs/_typing.pyi b/pandas-stubs/_typing.pyi index 6bdb6f91e..74a5a51bd 100644 --- a/pandas-stubs/_typing.pyi +++ b/pandas-stubs/_typing.pyi @@ -76,7 +76,7 @@ class FulldatetimeDict(YearMonthDayDict, total=False): # dtypes NpDtype: TypeAlias = str | np.dtype[np.generic] | type[str | complex | bool | object] Dtype: TypeAlias = ExtensionDtype | NpDtype -DtypeArg: TypeAlias = Dtype | dict[Any, Dtype] +DtypeArg: TypeAlias = Dtype | Mapping[Any, Dtype] DtypeBackend: TypeAlias = Literal["pyarrow", "numpy_nullable"] BooleanDtypeArg: TypeAlias = ( # Builtin bool type and its string alias diff --git a/tests/test_io.py b/tests/test_io.py index 99c069455..41c8c891c 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1388,3 +1388,31 @@ def test_read_with_lxml_dtype_backend() -> None: check( assert_type(read_xml(path, dtype_backend="pyarrow"), DataFrame), DataFrame ) + + +def test_read_sql_str_dtype() -> None: + with ensure_clean() as path: + con = sqlite3.connect(path) + check(assert_type(DF.to_sql("test", con), Union[int, None]), int) + check( + assert_type( + read_sql_query( + "select * from test", + con=con, + index_col="index", + dtype="int", + ), + DataFrame, + ), + DataFrame, + ) + con.close() + + if TYPE_CHECKING: + # sqlite3 doesn't support read_table, which is required for this function + # Could only run in pytest if SQLAlchemy was installed + with ensure_clean() as path: + co1 = sqlite3.connect(path) + assert_type(DF.to_sql("test", con=co1), Union[int, None]) + assert_type(read_sql_table("test", con=co1, dtype="int"), DataFrame) + co1.close() From 9afff8a87ff74d1b0ef56b676317a13f445d724a Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Wed, 3 May 2023 23:16:21 +0530 Subject: [PATCH 2/4] Update test_io.py --- tests/test_io.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/test_io.py b/tests/test_io.py index 41c8c891c..e8fcf57fc 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1407,12 +1407,3 @@ def test_read_sql_str_dtype() -> None: DataFrame, ) con.close() - - if TYPE_CHECKING: - # sqlite3 doesn't support read_table, which is required for this function - # Could only run in pytest if SQLAlchemy was installed - with ensure_clean() as path: - co1 = sqlite3.connect(path) - assert_type(DF.to_sql("test", con=co1), Union[int, None]) - assert_type(read_sql_table("test", con=co1, dtype="int"), DataFrame) - co1.close() From 3f958daba013675f1110637500941e82b46883d6 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 6 May 2023 11:22:29 +0530 Subject: [PATCH 3/4] Update tests/test_io.py Co-authored-by: Irv Lustig --- tests/test_io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_io.py b/tests/test_io.py index e8fcf57fc..85d57b218 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1390,7 +1390,7 @@ def test_read_with_lxml_dtype_backend() -> None: ) -def test_read_sql_str_dtype() -> None: +def test_read_sql_dict_str_value_dtype() -> None: with ensure_clean() as path: con = sqlite3.connect(path) check(assert_type(DF.to_sql("test", con), Union[int, None]), int) From d5719e979d0a1a308daf771d202c07a3e5768e59 Mon Sep 17 00:00:00 2001 From: ramvikrams Date: Sat, 6 May 2023 11:28:34 +0530 Subject: [PATCH 4/4] Update test_io.py --- tests/test_io.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_io.py b/tests/test_io.py index 85d57b218..1a5b3c05a 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -1391,6 +1391,7 @@ def test_read_with_lxml_dtype_backend() -> None: def test_read_sql_dict_str_value_dtype() -> None: + # GH 676 with ensure_clean() as path: con = sqlite3.connect(path) check(assert_type(DF.to_sql("test", con), Union[int, None]), int) @@ -1400,7 +1401,7 @@ def test_read_sql_dict_str_value_dtype() -> None: "select * from test", con=con, index_col="index", - dtype="int", + dtype={"a": "int"}, ), DataFrame, ),