pandas-dev / pandas-stubs

Public type stubs for pandas
BSD 3-Clause "New" or "Revised" License
234 stars 124 forks source link

dtype arg of read_excel is too strict #440

Closed derhintze closed 1 year ago

derhintze commented 1 year ago

Describe the bug The dtype arg of read_excel does not accept

To Reproduce

import pandas as pd
import numpy as np

dtypes = {"a": np.int64, "b": str, "c": np.float64}

data = pd.read_excel("my/path/file.xlsx", dtype=dtypes)

running this through mypy yields:

test.py:8: error: Argument "dtype" to "read_excel" has incompatible type "Dict[str, type]"; expected "Union[str, Union[ExtensionDtype, Union[str, dtype[generic], Type[str], Type[complex], Type[bool], Type[object]]], Dict[str, Union[str, Union[ExtensionDtype, Union[str, dtype[generic], Type[str], Type[complex], Type[bool], Type[object]]]]], None]"  [arg-type]
test.py:8: note: "Dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
test.py:8: note: Consider using "Mapping" instead, which is covariant in the value type
Found 1 error in 1 file (checked 1 source file)

Please complete the following information:

Dr-Irv commented 1 year ago

I think the following change will work: In the two places where there are arguments for dtype for read_excel, we have

 dtype: str | Dtype | dict[str, str | Dtype] | None = ...,

Should be changed to

dtype: str | Dtype | Mapping[str, str | NpDtype] | None = ...,
derhintze commented 1 year ago

Yeah, thought sth like this, too. Could you have a look at the converters arg, too? I think it's kind of like the same issue. Thanks!

Dr-Irv commented 1 year ago

Yeah, thought sth like this, too. Could you have a look at the converters arg, too? I think it's kind of like the same issue. Thanks!

Would need to see an example (and probably create another issue) that illustrates the issue for converters

shubhankarunhale commented 1 year ago

Hi, I would like to work on this, thanks!