Skip to content

Fix #849: Update converters type in read_excel for better Pyright compatibility #1297

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Sachi-Parashar
Copy link

Summary

This PR addresses issue #849, where pyright fails to type-check the converters argument in read_excel when using functools.partial or lambda functions.

Fix

Updated all read_excel and parse overloads in pandas/io/excel/_base.pyi:

- converters: Mapping[int | str, Callable[[object], object]] | None
+ converters: Mapping[int | str, Callable[[Any], Any]] | None

This relaxes the type signature to accept functools.partial and similar callables.

Test

Added a test tests/test_excel_converters.py with assert_type to confirm the fix works with pyright and mypy.

from functools import partial
import pandas as pd
from typing_extensions import assert_type

conv = partial(pd.to_datetime, errors="coerce")
df = pd.read_excel("foo.xlsx", converters={"col1": conv})
assert_type(df, pd.DataFrame)

Notes

  • Verified with both mypy and pyright. - Worked✅
  • If needed, I'm open to updating ConvertersArg in _typing.pyi for broader consistency in a follow-up PR.

Closes #849

@Sachi-Parashar Sachi-Parashar changed the title Add files via upload Fix #849: Update converters type in read_excel for better Pyright compatibility Jul 28, 2025
Remove redundant test_excel_converters.py after moving test to test_io.py
Add test_converters_partial() to test_io.py for read_excel converters
tests/test_io.py Outdated

partial_func = partial(pd.to_datetime, errors="coerce")
df = pd.read_excel("foo.xlsx", converters={"field_1": partial_func})
assert_type(df, pd.DataFrame)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use check(assert_type(df, pd.DataFrame), pd.DataFrame)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used check(assert_type(df, pd.DataFrame), pd.DataFrame) as suggested.

Comment on lines +308 to +309
assert_type(read_sas(path, iterator=True), Union[SAS7BDATReader, XportReader]),
SAS7BDATReader,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why you indented here and elsewhere. Make sure to install the pre-commit and run black

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see from the CI that the pre-commit failed because of this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type checking with converters= on read_excel
2 participants