Skip to content

Commit 2f63354

Browse files
Fix #849: Update converters type in read_excel for better Pyright compatibility (#1297)
* Add files via upload * Add files via upload * Delete tests/test_excel_converters.py Remove redundant test_excel_converters.py after moving test to test_io.py * Move converter test to test_io.py Add test_converters_partial() to test_io.py for read_excel converters * Used ensure_clean() * Update test_io.py black formatting * Update test_io.py * Update test_io.py --------- Co-authored-by: Loic Diridollou <[email protected]>
1 parent 8e5c754 commit 2f63354

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

pandas-stubs/io/excel/_base.pyi

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def read_excel(
5454
usecols: str | UsecolsArgType = ...,
5555
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
5656
engine: ExcelReadEngine | None = ...,
57-
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
57+
converters: Mapping[int | str, Callable[[Any], Any]] | None = ...,
5858
true_values: Iterable[Hashable] | None = ...,
5959
false_values: Iterable[Hashable] | None = ...,
6060
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,
@@ -97,7 +97,7 @@ def read_excel(
9797
usecols: str | UsecolsArgType = ...,
9898
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
9999
engine: ExcelReadEngine | None = ...,
100-
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
100+
converters: Mapping[int | str, Callable[[Any], Any]] | None = ...,
101101
true_values: Iterable[Hashable] | None = ...,
102102
false_values: Iterable[Hashable] | None = ...,
103103
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,
@@ -141,7 +141,7 @@ def read_excel( # type: ignore[overload-cannot-match]
141141
usecols: str | UsecolsArgType = ...,
142142
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
143143
engine: ExcelReadEngine | None = ...,
144-
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
144+
converters: Mapping[int | str, Callable[[Any], Any]] | None = ...,
145145
true_values: Iterable[Hashable] | None = ...,
146146
false_values: Iterable[Hashable] | None = ...,
147147
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,
@@ -184,7 +184,7 @@ def read_excel(
184184
usecols: str | UsecolsArgType = ...,
185185
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
186186
engine: ExcelReadEngine | None = ...,
187-
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
187+
converters: Mapping[int | str, Callable[[Any], Any]] | None = ...,
188188
true_values: Iterable[Hashable] | None = ...,
189189
false_values: Iterable[Hashable] | None = ...,
190190
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,
@@ -264,7 +264,7 @@ class ExcelFile:
264264
names: ListLikeHashable | None = ...,
265265
index_col: int | Sequence[int] | None = ...,
266266
usecols: str | UsecolsArgType = ...,
267-
converters: dict[int | str, Callable[[object], object]] | None = ...,
267+
converters: dict[int | str, Callable[[Any], Any]] | None = ...,
268268
true_values: Iterable[Hashable] | None = ...,
269269
false_values: Iterable[Hashable] | None = ...,
270270
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,
@@ -292,7 +292,7 @@ class ExcelFile:
292292
names: ListLikeHashable | None = ...,
293293
index_col: int | Sequence[int] | None = ...,
294294
usecols: str | UsecolsArgType = ...,
295-
converters: dict[int | str, Callable[[object], object]] | None = ...,
295+
converters: dict[int | str, Callable[[Any], Any]] | None = ...,
296296
true_values: Iterable[Hashable] | None = ...,
297297
false_values: Iterable[Hashable] | None = ...,
298298
skiprows: int | Sequence[int] | Callable[[object], bool] | None = ...,

tests/test_io.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import defaultdict
22
from collections.abc import Generator
33
import csv
4+
from functools import partial
45
import io
56
import os.path
67
import pathlib
@@ -1787,3 +1788,14 @@ def test_read_json_engine() -> None:
17871788
pd.read_json(dd, lines=False, engine="pyarrow") # type: ignore[call-overload] # pyright: ignore[reportArgumentType, reportCallIssue]
17881789
pd.read_json(io.StringIO(data), engine="pyarrow") # type: ignore[call-overload] # pyright: ignore[reportArgumentType]
17891790
pd.read_json(io.StringIO(data), lines=True, engine="pyarrow") # type: ignore[call-overload] # pyright: ignore[reportArgumentType, reportCallIssue]
1791+
1792+
1793+
def test_converters_partial() -> None:
1794+
df = pd.DataFrame({"field_1": ["2020-01-01", "not a date"]})
1795+
partial_func = partial(pd.to_datetime, errors="coerce")
1796+
1797+
with ensure_clean(".xlsx") as path:
1798+
check(assert_type(df.to_excel(path, index=False), None), type(None))
1799+
1800+
result = pd.read_excel(path, converters={"field_1": partial_func})
1801+
check(assert_type(result, pd.DataFrame), pd.DataFrame)

0 commit comments

Comments
 (0)