Skip to content

Commit 8ec4901

Browse files
authored
fix(arithmetic): #1418 🈲 disallow bool * timedelta for 3.0 (#1422)
* fix: #1418 disallow bool * timedelta * attempt to fix mypy for 3.10
1 parent 4e2684f commit 8ec4901

File tree

9 files changed

+209
-331
lines changed

9 files changed

+209
-331
lines changed

‎pandas-stubs/_typing.pyi‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,16 +872,15 @@ SeriesDType: TypeAlias = (
872872
| datetime.timedelta # includes pd.Timedelta
873873
)
874874
S1 = TypeVar("S1", bound=SeriesDType, default=Any)
875-
S1_CO_NSDT = TypeVar(
876-
"S1_CO_NSDT", bound=SeriesDTypeNoStrDateTime, default=Any, covariant=True
877-
)
878875
S1_CT_NDT = TypeVar(
879876
"S1_CT_NDT", bound=SeriesDTypeNoDateTime, default=Any, contravariant=True
880877
)
881878
S1_CO = TypeVar("S1_CO", bound=SeriesDType, default=Any, covariant=True)
882879
S1_CT = TypeVar("S1_CT", bound=SeriesDType, default=Any, contravariant=True)
883880
# Like S1, but without `default=Any`.
884881
S2 = TypeVar("S2", bound=SeriesDType)
882+
S2_CT = TypeVar("S2_CT", bound=SeriesDType, contravariant=True)
883+
S2_CO_NSDT = TypeVar("S2_CO_NSDT", bound=SeriesDTypeNoStrDateTime, covariant=True)
885884
S3 = TypeVar("S3", bound=SeriesDType)
886885

887886
# Constraint, instead of bound

‎pandas-stubs/core/indexes/base.pyi‎

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ from pandas._typing import (
5656
C2,
5757
S1,
5858
S1_CO,
59-
S1_CO_NSDT,
6059
S1_CT,
60+
S2_CO_NSDT,
61+
S2_CT,
6162
T_COMPLEX,
6263
T_INT,
6364
AnyAll,
@@ -753,30 +754,32 @@ class Index(IndexOpsMixin[S1]):
753754
@overload
754755
def __mul__(self, other: np_ndarray_dt) -> Never: ...
755756
@overload
756-
def __mul__(self: Index[complex], other: np_ndarray_td) -> Never: ...
757+
def __mul__(self: Index[bool] | Index[complex], other: np_ndarray_td) -> Never: ...
757758
# pandas-dev/pandas#62524: An index of Python native timedeltas can be
758759
# produced, instead of a TimedeltaIndex, hence the overload
759760
@overload
760761
def __mul__( # type: ignore[overload-overlap]
761-
self: Index[bool] | Index[int] | Index[float], other: Sequence[timedelta]
762+
self: Index[int] | Index[float], other: Sequence[timedelta]
762763
) -> Index[Timedelta]: ...
763764
@overload
764765
def __mul__(
765-
self: Index[bool] | Index[int] | Index[float],
766+
self: Index[int] | Index[float],
766767
other: timedelta | Sequence[Timedelta] | np.timedelta64 | np_ndarray_td,
767768
) -> TimedeltaIndex: ...
768769
@overload
769-
def __mul__(self: Index[Timedelta], other: np_ndarray_complex) -> Never: ...
770+
def __mul__(
771+
self: Index[Timedelta], other: np_ndarray_bool | np_ndarray_complex
772+
) -> Never: ...
770773
@overload
771774
def __mul__(
772775
self: Index[Timedelta],
773776
other: (
774-
float
775-
| Sequence[float]
776-
| np_ndarray_bool
777+
Just[int]
778+
| Just[float]
779+
| Sequence[Just[int]]
780+
| Sequence[Just[float]]
777781
| np_ndarray_anyint
778782
| np_ndarray_float
779-
| Index[bool]
780783
| Index[int]
781784
| Index[float]
782785
),
@@ -807,11 +810,11 @@ class Index(IndexOpsMixin[S1]):
807810
) -> Index[complex]: ...
808811
@overload
809812
def __mul__(
810-
self: Index[S1_CT],
813+
self: Index[S2_CT],
811814
other: (
812-
SupportsRMul[S1_CT, S1_CO_NSDT] | Sequence[SupportsRMul[S1_CT, S1_CO_NSDT]]
815+
SupportsRMul[S2_CT, S2_CO_NSDT] | Sequence[SupportsRMul[S2_CT, S2_CO_NSDT]]
813816
),
814-
) -> Index[S1_CO_NSDT]: ...
817+
) -> Index[S2_CO_NSDT]: ...
815818
@overload
816819
def __mul__(
817820
self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool]
@@ -843,30 +846,32 @@ class Index(IndexOpsMixin[S1]):
843846
@overload
844847
def __rmul__(self, other: np_ndarray_dt) -> Never: ...
845848
@overload
846-
def __rmul__(self: Index[complex], other: np_ndarray_td) -> Never: ...
849+
def __rmul__(self: Index[bool] | Index[complex], other: np_ndarray_td) -> Never: ...
847850
# pandas-dev/pandas#62524: An index of Python native timedeltas can be
848851
# produced, instead of a TimedeltaIndex, hence the overload
849852
@overload
850853
def __rmul__( # type: ignore[overload-overlap]
851-
self: Index[bool] | Index[int] | Index[float], other: Sequence[timedelta]
854+
self: Index[int] | Index[float], other: Sequence[timedelta]
852855
) -> Index[Timedelta]: ...
853856
@overload
854857
def __rmul__(
855-
self: Index[bool] | Index[int] | Index[float],
858+
self: Index[int] | Index[float],
856859
other: timedelta | Sequence[Timedelta] | np.timedelta64 | np_ndarray_td,
857860
) -> TimedeltaIndex: ...
858861
@overload
859-
def __rmul__(self: Index[Timedelta], other: np_ndarray_complex) -> Never: ...
862+
def __rmul__(
863+
self: Index[Timedelta], other: np_ndarray_bool | np_ndarray_complex
864+
) -> Never: ...
860865
@overload
861866
def __rmul__(
862867
self: Index[Timedelta],
863868
other: (
864-
float
865-
| Sequence[float]
866-
| np_ndarray_bool
869+
Just[int]
870+
| Just[float]
871+
| Sequence[Just[int]]
872+
| Sequence[Just[float]]
867873
| np_ndarray_anyint
868874
| np_ndarray_float
869-
| Index[bool]
870875
| Index[int]
871876
| Index[float]
872877
),
@@ -897,11 +902,11 @@ class Index(IndexOpsMixin[S1]):
897902
) -> Index[complex]: ...
898903
@overload
899904
def __rmul__(
900-
self: Index[S1_CT],
905+
self: Index[S2_CT],
901906
other: (
902-
SupportsMul[S1_CT, S1_CO_NSDT] | Sequence[SupportsMul[S1_CT, S1_CO_NSDT]]
907+
SupportsMul[S2_CT, S2_CO_NSDT] | Sequence[SupportsMul[S2_CT, S2_CO_NSDT]]
903908
),
904-
) -> Index[S1_CO_NSDT]: ...
909+
) -> Index[S2_CO_NSDT]: ...
905910
@overload
906911
def __rmul__(
907912
self: Index[T_COMPLEX], other: np_ndarray_bool | Index[bool]

‎pandas-stubs/core/indexes/timedeltas.pyi‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from pandas._libs import Timedelta
2929
from pandas._libs.tslibs import BaseOffset
3030
from pandas._typing import (
3131
AxesData,
32+
Just,
3233
TimedeltaConvertibleTypes,
3334
np_ndarray_anyint,
3435
np_ndarray_bool,
@@ -86,33 +87,33 @@ class TimedeltaIndex(
8687
self, other: dt.datetime | np.datetime64 | np_ndarray_dt | DatetimeIndex
8788
) -> DatetimeIndex: ...
8889
@overload # type: ignore[override]
89-
def __mul__(self, other: np_ndarray_complex) -> Never: ...
90+
def __mul__(self, other: np_ndarray_bool | np_ndarray_complex) -> Never: ...
9091
@overload
9192
def __mul__(
9293
self,
9394
other: (
94-
float
95-
| Sequence[float]
96-
| np_ndarray_bool
95+
Just[int]
96+
| Just[float]
97+
| Sequence[Just[int]]
98+
| Sequence[Just[float]]
9799
| np_ndarray_anyint
98100
| np_ndarray_float
99-
| Index[bool]
100101
| Index[int]
101102
| Index[float]
102103
),
103104
) -> Self: ...
104105
@overload # type: ignore[override]
105-
def __rmul__(self, other: np_ndarray_complex) -> Never: ...
106+
def __rmul__(self, other: np_ndarray_bool | np_ndarray_complex) -> Never: ...
106107
@overload
107108
def __rmul__(
108109
self,
109110
other: (
110-
float
111-
| Sequence[float]
112-
| np_ndarray_bool
111+
Just[int]
112+
| Just[float]
113+
| Sequence[Just[int]]
114+
| Sequence[Just[float]]
113115
| np_ndarray_anyint
114116
| np_ndarray_float
115-
| Index[bool]
116117
| Index[int]
117118
| Index[float]
118119
),

0 commit comments

Comments
 (0)