Repository: pandas-dev/pandas-stubs (language: python)

Issue:
Switch `to_timedelta` argument type from `list` to `Sequence`
**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**
```python
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:**
 - OS: Win
 - OS Version 10
 - python 3.9.7
 - version of type checker mypy 1.8.0
 - version of installed `pandas-stubs` 2.2.2.240603

Switch `to_timedelta` argument type from `list` to `Sequence`
**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**
```python
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:**
 - OS: Win
 - OS Version 10
 - python 3.9.7
 - version of type checker mypy 1.8.0
 - version of installed `pandas-stubs` 2.2.2.240603



Discussion / hints:
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.
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.

Fix patch:
diff --git a/pandas-stubs/core/tools/timedeltas.pyi b/pandas-stubs/core/tools/timedeltas.pyi
index aa2c34ac..38ed0c07 100644
--- a/pandas-stubs/core/tools/timedeltas.pyi
+++ b/pandas-stubs/core/tools/timedeltas.pyi
@@ -14,6 +14,7 @@ from pandas._libs.tslibs.timedeltas import TimeDeltaUnitChoices
 from pandas._typing import (
     ArrayLike,
     RaiseCoerce,
+    SequenceNotStr,
 )
 
 @overload
@@ -31,8 +32,8 @@ def to_timedelta(
 @overload
 def to_timedelta(
     arg: (
-        Sequence[float | timedelta]
-        | list[str | float | timedelta]
+        SequenceNotStr
+        | Sequence[float | timedelta]
         | tuple[str | float | timedelta, ...]
         | range
         | ArrayLike


Test patch:
diff --git a/tests/test_timefuncs.py b/tests/test_timefuncs.py
index e3dcb9a8..a61bb92c 100644
--- a/tests/test_timefuncs.py
+++ b/tests/test_timefuncs.py
@@ -140,6 +140,11 @@ def test_timedelta_series_arithmetic() -> None:
     r4: pd.TimedeltaIndex = tds1 / 10.2
 
 
+def test_timedelta_series_string() -> None:
+    seq_list = ["1 day"]
+    check(assert_type(pd.to_timedelta(seq_list), pd.TimedeltaIndex), pd.TimedeltaIndex)
+
+
 def test_timestamp_timedelta_series_arithmetic() -> None:
     ts1 = pd.to_datetime(pd.Series(["2022-03-05", "2022-03-06"]))
     assert isinstance(ts1.iloc[0], pd.Timestamp)