Skip to content

Commit 4e2684f

Browse files
GH1418 Disallow pd.Timedelta * bool (#1421)
* GH1418 Disallow pd.Timedelta * bool * GH1418 Fix source of Never * GH1418 PR Feedback * GH1418 PR Feedback * GH1418 PR Feedback
1 parent 89343dc commit 4e2684f

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

pandas-stubs/_libs/tslibs/timedeltas.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ from pandas._libs.tslibs import (
3030
from pandas._libs.tslibs.period import Period
3131
from pandas._libs.tslibs.timestamps import Timestamp
3232
from pandas._typing import (
33+
Just,
3334
ShapeT,
3435
TimeUnit,
3536
np_1darray,
@@ -209,13 +210,13 @@ class Timedelta(timedelta):
209210
def __abs__(self) -> Self: ...
210211
# Override due to more types supported than timedelta
211212
@overload # type: ignore[override]
212-
def __mul__(self, other: float) -> Self: ...
213+
def __mul__(self, other: Just[float] | Just[int]) -> Self: ...
213214
@overload
214215
def __mul__(
215216
self, other: np_ndarray[ShapeT, np.bool_ | np.integer | np.floating]
216217
) -> np_ndarray[ShapeT, np.timedelta64]: ...
217218
@overload
218-
def __rmul__(self, other: float) -> Self: ...
219+
def __rmul__(self, other: Just[float] | Just[int]) -> Self: ...
219220
@overload
220221
def __rmul__(
221222
self, other: np_ndarray[ShapeT, np.bool_ | np.integer | np.floating]

tests/scalars/test_scalars.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,5 +2063,4 @@ def test_period_methods() -> None:
20632063

20642064
def test_nattype_hashable() -> None:
20652065
# GH 827
2066-
_aset = {pd.NaT}
20672066
check(assert_type(pd.NaT.__hash__(), int), int)

tests/scalars/timedelta/__init__.py

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Test file for arithmetic method on Timedelta objects."""
2+
3+
import pandas as pd
4+
from typing_extensions import assert_type
5+
6+
from tests import (
7+
TYPE_CHECKING_INVALID_USAGE,
8+
check,
9+
)
10+
11+
12+
def test_mul() -> None:
13+
"""Test checking that pd.Timedelta * int / float."""
14+
a = pd.Timedelta("1 day")
15+
b = True
16+
c = 1.0
17+
d = 5
18+
e = 1 + 3.0j
19+
20+
check(assert_type(a * c, pd.Timedelta), pd.Timedelta)
21+
check(assert_type(c * a, pd.Timedelta), pd.Timedelta)
22+
check(assert_type(a * d, pd.Timedelta), pd.Timedelta)
23+
check(assert_type(d * a, pd.Timedelta), pd.Timedelta)
24+
25+
if TYPE_CHECKING_INVALID_USAGE:
26+
# pd.Timedelta * bool is not allowed, see GH1418
27+
_0 = a * b # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
28+
_1 = b * a # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
29+
_2 = a * e # type: ignore[operator] # pyright: ignore[reportOperatorIssue]
30+
_3 = e * a # type: ignore[operator] # pyright: ignore[reportOperatorIssue]

0 commit comments

Comments
 (0)