Skip to content

Commit 79f24c9

Browse files
committed
change TypeVar to use Union where possible
1 parent 2668636 commit 79f24c9

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

pandas-stubs/_typing.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Incomplete: TypeAlias = Any
5757
ArrayLike: TypeAlias = ExtensionArray | np.ndarray[Any, Any]
5858
AnyArrayLike: TypeAlias = Index[Any] | Series[Any] | np.ndarray[Any, Any]
5959
PythonScalar: TypeAlias = str | bool | complex
60-
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", Period, Timestamp, Timedelta)
60+
DatetimeLikeScalar = TypeVar("DatetimeLikeScalar", bound=Period | Timestamp | Timedelta)
6161
PandasScalar: TypeAlias = bytes | datetime.date | datetime.datetime | datetime.timedelta
6262
IntStrT = TypeVar("IntStrT", int, str)
6363
# Scalar: TypeAlias = PythonScalar | PandasScalar
@@ -490,7 +490,8 @@ AxisColumn: TypeAlias = Literal["columns", 1]
490490
Axis: TypeAlias = AxisIndex | AxisColumn
491491
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
492492
KeysArgType: TypeAlias = Any
493-
ListLike = TypeVar("ListLike", Sequence, np.ndarray, Series, Index)
493+
ListLike: TypeAlias = Sequence | np.ndarray | Series | Index
494+
ListLikeT = TypeVar("ListLikeT", bound=ListLike)
494495
ListLikeExceptSeriesAndStr: TypeAlias = (
495496
MutableSequence[Any] | np.ndarray[Any, Any] | tuple[Any, ...] | Index[Any]
496497
)

pandas-stubs/core/dtypes/concat.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from pandas import (
66
Series,
77
)
88

9-
_CatT = TypeVar("_CatT", Categorical, CategoricalIndex, Series)
9+
_CatT = TypeVar("_CatT", bound=Categorical | CategoricalIndex | Series)
1010

1111
def union_categoricals(
1212
to_union: list[_CatT], sort_categories: bool = ..., ignore_order: bool = ...

pandas-stubs/core/indexes/accessors.pyi

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ from pandas._typing import (
4242

4343
class Properties(PandasDelegate, NoNewAttributesMixin): ...
4444

45-
_DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", Series[int], Index[int])
45+
_DTFieldOpsReturnType = TypeVar("_DTFieldOpsReturnType", bound=Series[int] | Index[int])
4646

4747
class _DayLikeFieldOps(Generic[_DTFieldOpsReturnType]):
4848
@property
@@ -84,7 +84,9 @@ class _DatetimeFieldOps(
8484
_DayLikeFieldOps[_DTFieldOpsReturnType], _MiniSeconds[_DTFieldOpsReturnType]
8585
): ...
8686

87-
_DTBoolOpsReturnType = TypeVar("_DTBoolOpsReturnType", Series[bool], np_ndarray_bool)
87+
_DTBoolOpsReturnType = TypeVar(
88+
"_DTBoolOpsReturnType", bound=Series[bool] | np_ndarray_bool
89+
)
8890

8991
class _IsLeapYearProperty(Generic[_DTBoolOpsReturnType]):
9092
@property
@@ -106,7 +108,7 @@ class _DatetimeBoolOps(
106108
@property
107109
def is_year_end(self) -> _DTBoolOpsReturnType: ...
108110

109-
_DTFreqReturnType = TypeVar("_DTFreqReturnType", str, BaseOffset)
111+
_DTFreqReturnType = TypeVar("_DTFreqReturnType", bound=str | BaseOffset)
110112

111113
class _FreqProperty(Generic[_DTFreqReturnType]):
112114
@property
@@ -121,10 +123,10 @@ class _DatetimeObjectOps(
121123
): ...
122124

123125
_DTOtherOpsDateReturnType = TypeVar(
124-
"_DTOtherOpsDateReturnType", Series[dt.date], np.ndarray
126+
"_DTOtherOpsDateReturnType", bound=Series[dt.date] | np.ndarray
125127
)
126128
_DTOtherOpsTimeReturnType = TypeVar(
127-
"_DTOtherOpsTimeReturnType", Series[dt.time], np.ndarray
129+
"_DTOtherOpsTimeReturnType", bound=Series[dt.time] | np.ndarray
128130
)
129131

130132
class _DatetimeOtherOps(Generic[_DTOtherOpsDateReturnType, _DTOtherOpsTimeReturnType]):
@@ -157,11 +159,7 @@ class _DatetimeLikeOps(
157159

158160
_DTTimestampTimedeltaReturnType = TypeVar(
159161
"_DTTimestampTimedeltaReturnType",
160-
Series,
161-
TimestampSeries,
162-
TimedeltaSeries,
163-
DatetimeIndex,
164-
TimedeltaIndex,
162+
bound=Series | TimestampSeries | TimedeltaSeries | DatetimeIndex | TimedeltaIndex,
165163
)
166164

167165
class _DatetimeRoundingMethods(Generic[_DTTimestampTimedeltaReturnType]):
@@ -199,8 +197,10 @@ class _DatetimeRoundingMethods(Generic[_DTTimestampTimedeltaReturnType]):
199197
_DTNormalizeReturnType = TypeVar(
200198
"_DTNormalizeReturnType", TimestampSeries, DatetimeIndex
201199
)
202-
_DTStrKindReturnType = TypeVar("_DTStrKindReturnType", Series[str], Index)
203-
_DTToPeriodReturnType = TypeVar("_DTToPeriodReturnType", PeriodSeries, PeriodIndex)
200+
_DTStrKindReturnType = TypeVar("_DTStrKindReturnType", bound=Series[str] | Index)
201+
_DTToPeriodReturnType = TypeVar(
202+
"_DTToPeriodReturnType", bound=PeriodSeries | PeriodIndex
203+
)
204204

205205
class _DatetimeLikeNoTZMethods(
206206
_DatetimeRoundingMethods[_DTTimestampTimedeltaReturnType],
@@ -289,9 +289,11 @@ class DatetimeProperties(
289289
def as_unit(self, unit: TimeUnit) -> _DTTimestampTimedeltaReturnType: ...
290290

291291
_TDNoRoundingMethodReturnType = TypeVar(
292-
"_TDNoRoundingMethodReturnType", Series[int], Index
292+
"_TDNoRoundingMethodReturnType", bound=Series[int] | Index
293+
)
294+
_TDTotalSecondsReturnType = TypeVar(
295+
"_TDTotalSecondsReturnType", bound=Series[float] | Index
293296
)
294-
_TDTotalSecondsReturnType = TypeVar("_TDTotalSecondsReturnType", Series[float], Index)
295297

296298
class _TimedeltaPropertiesNoRounding(
297299
Generic[_TDNoRoundingMethodReturnType, _TDTotalSecondsReturnType]
@@ -318,11 +320,15 @@ class TimedeltaProperties(
318320
def unit(self) -> TimeUnit: ...
319321
def as_unit(self, unit: TimeUnit) -> TimedeltaSeries: ...
320322

321-
_PeriodDTReturnTypes = TypeVar("_PeriodDTReturnTypes", TimestampSeries, DatetimeIndex)
322-
_PeriodIntReturnTypes = TypeVar("_PeriodIntReturnTypes", Series[int], Index[int])
323-
_PeriodStrReturnTypes = TypeVar("_PeriodStrReturnTypes", Series[str], Index)
324-
_PeriodDTAReturnTypes = TypeVar("_PeriodDTAReturnTypes", DatetimeArray, DatetimeIndex)
325-
_PeriodPAReturnTypes = TypeVar("_PeriodPAReturnTypes", PeriodArray, PeriodIndex)
323+
_PeriodDTReturnTypes = TypeVar(
324+
"_PeriodDTReturnTypes", bound=TimestampSeries | DatetimeIndex
325+
)
326+
_PeriodIntReturnTypes = TypeVar("_PeriodIntReturnTypes", bound=Series[int] | Index[int])
327+
_PeriodStrReturnTypes = TypeVar("_PeriodStrReturnTypes", bound=Series[str] | Index)
328+
_PeriodDTAReturnTypes = TypeVar(
329+
"_PeriodDTAReturnTypes", bound=DatetimeArray | DatetimeIndex
330+
)
331+
_PeriodPAReturnTypes = TypeVar("_PeriodPAReturnTypes", bound=PeriodArray | PeriodIndex)
326332

327333
class _PeriodProperties(
328334
Generic[

pandas-stubs/core/strings.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ from pandas._typing import (
2828
)
2929

3030
# The _TS type is what is used for the result of str.split with expand=True
31-
_TS = TypeVar("_TS", DataFrame, MultiIndex)
31+
_TS = TypeVar("_TS", bound=DataFrame | MultiIndex)
3232
# The _TS2 type is what is used for the result of str.split with expand=False
33-
_TS2 = TypeVar("_TS2", Series[list[str]], Index[list[str]])
33+
_TS2 = TypeVar("_TS2", bound=Series[list[str]] | Index[list[str]])
3434
# The _TM type is what is used for the result of str.match
35-
_TM = TypeVar("_TM", Series[bool], np_ndarray_bool)
35+
_TM = TypeVar("_TM", bound=Series[bool] | np_ndarray_bool)
3636

3737
class StringMethods(NoNewAttributesMixin, Generic[T, _TS, _TM, _TS2]):
3838
def __init__(self, data: T) -> None: ...

0 commit comments

Comments
 (0)