pandas-dev / pandas-stubs

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

Switch `to_timedelta` argument type from `list` to `Sequence` #956

Open headtr1ck opened 6 days ago

headtr1ck commented 6 days ago

Describe the bug to_timedelta uses a list of a union argument type. This will raise false positive errors when passing a list of a single type because list is invariant.

To Reproduce

import pandas as pd

pd.to_timedelta(["1 day"])

produces:

error: Argument 1 to "to_timedelta" has incompatible type "list[str]"; expected "Union[Sequence[Union[float, timedelta]], list[Union[str, float, timedelta]], tuple[Union[str, float, timedelta], ...], range, Union[ExtensionArray, ndarray[Any, Any]], Index[Any]]" [arg-type] note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance note: Consider using "Sequence" instead, which is covariant

Please complete the following information:

Dr-Irv commented 6 days ago

Thanks for the report. I agree - we can change list to Sequence at https://github.com/pandas-dev/pandas-stubs/blob/9d5348ead9216053d4e8d7d2b1c592c0065b9760/pandas-stubs/core/tools/timedeltas.pyi#L35

PR with test welcome.