Skip to content

Commit 52f2f0c

Browse files
authored
Fix Series.__new__ (#722)
* Fix Series.__new__ * dtype + bound * Literal -> TimestampDtypeArg
1 parent 6f6586b commit 52f2f0c

File tree

3 files changed

+19
-63
lines changed

3 files changed

+19
-63
lines changed

pandas-stubs/_libs/interval.pyi

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ from pandas._typing import (
2727

2828
VALID_CLOSED: frozenset[str]
2929

30-
_OrderableScalarT = TypeVar("_OrderableScalarT", int, float)
31-
_OrderableTimesT = TypeVar("_OrderableTimesT", Timestamp, Timedelta)
32-
_OrderableT = TypeVar("_OrderableT", int, float, Timestamp, Timedelta)
30+
_OrderableScalarT = TypeVar("_OrderableScalarT", bound=int | float)
31+
_OrderableTimesT = TypeVar("_OrderableTimesT", bound=Timestamp | Timedelta)
32+
_OrderableT = TypeVar("_OrderableT", bound=int | float | Timestamp | Timedelta)
3333

3434
class _LengthDescriptor:
3535
@overload

pandas-stubs/_typing.pyi

+1-7
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,7 @@ NDFrameT = TypeVar("NDFrameT", bound=NDFrame)
360360
IndexT = TypeVar("IndexT", bound=Index)
361361

362362
# Interval closed type
363-
IntervalT = TypeVar(
364-
"IntervalT",
365-
Interval[int],
366-
Interval[float],
367-
Interval[Timestamp],
368-
Interval[Timedelta],
369-
)
363+
IntervalT = TypeVar("IntervalT", bound=Interval)
370364
IntervalClosedType: TypeAlias = Literal["left", "right", "both", "neither"]
371365

372366
TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer]

pandas-stubs/core/series.pyi

+15-53
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ from matplotlib.axes import (
2727
import numpy as np
2828
from pandas import (
2929
Period,
30+
PeriodDtype,
3031
Timedelta,
3132
Timestamp,
3233
)
@@ -104,6 +105,7 @@ from pandas._typing import (
104105
CategoryDtypeArg,
105106
ComplexDtypeArg,
106107
CompressionOptions,
108+
Dtype,
107109
DtypeBackend,
108110
DtypeObj,
109111
FilePath,
@@ -209,92 +211,55 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
209211
_ListLike: TypeAlias = ArrayLike | dict[_str, np.ndarray] | list | tuple | Index
210212
__hash__: ClassVar[None]
211213

214+
# TODO: can __new__ be converted to __init__? Pandas implements __init__
212215
@overload
213216
def __new__(
214217
cls,
215218
data: DatetimeIndex | Sequence[Timestamp | np.datetime64 | datetime],
216219
index: Axes | None = ...,
217-
dtype=...,
220+
dtype: TimestampDtypeArg = ...,
218221
name: Hashable | None = ...,
219222
copy: bool = ...,
220-
fastpath: bool = ...,
221223
) -> TimestampSeries: ...
222224
@overload
223225
def __new__(
224226
cls,
225227
data: _ListLike,
226-
dtype: Literal["datetime64[ns]"],
227228
index: Axes | None = ...,
229+
*,
230+
dtype: TimestampDtypeArg,
228231
name: Hashable | None = ...,
229232
copy: bool = ...,
230-
fastpath: bool = ...,
231233
) -> TimestampSeries: ...
232234
@overload
233235
def __new__(
234236
cls,
235237
data: PeriodIndex,
236238
index: Axes | None = ...,
237-
dtype=...,
239+
dtype: PeriodDtype = ...,
238240
name: Hashable | None = ...,
239241
copy: bool = ...,
240-
fastpath: bool = ...,
241242
) -> PeriodSeries: ...
242243
@overload
243244
def __new__(
244245
cls,
245246
data: TimedeltaIndex | Sequence[Timedelta | np.timedelta64 | timedelta],
246247
index: Axes | None = ...,
247-
dtype=...,
248+
dtype: TimedeltaDtypeArg = ...,
248249
name: Hashable | None = ...,
249250
copy: bool = ...,
250-
fastpath: bool = ...,
251251
) -> TimedeltaSeries: ...
252252
@overload
253253
def __new__(
254254
cls,
255-
data: IntervalIndex[Interval[int]] | Interval[int] | Sequence[Interval[int]],
256-
index: Axes | None = ...,
257-
dtype=...,
258-
name: Hashable | None = ...,
259-
copy: bool = ...,
260-
fastpath: bool = ...,
261-
) -> IntervalSeries[int]: ...
262-
@overload
263-
def __new__(
264-
cls,
265-
data: IntervalIndex[Interval[float]]
266-
| Interval[float]
267-
| Sequence[Interval[float]],
268-
index: Axes | None = ...,
269-
dtype=...,
270-
name: Hashable | None = ...,
271-
copy: bool = ...,
272-
fastpath: bool = ...,
273-
) -> IntervalSeries[float]: ...
274-
@overload
275-
def __new__(
276-
cls,
277-
data: IntervalIndex[Interval[Timestamp]]
278-
| Interval[Timestamp]
279-
| Sequence[Interval[Timestamp]],
280-
index: Axes | None = ...,
281-
dtype=...,
282-
name: Hashable | None = ...,
283-
copy: bool = ...,
284-
fastpath: bool = ...,
285-
) -> IntervalSeries[Timestamp]: ...
286-
@overload
287-
def __new__(
288-
cls,
289-
data: IntervalIndex[Interval[Timedelta]]
290-
| Interval[Timedelta]
291-
| Sequence[Interval[Timedelta]],
255+
data: IntervalIndex[Interval[_OrderableT]]
256+
| Interval[_OrderableT]
257+
| Sequence[Interval[_OrderableT]],
292258
index: Axes | None = ...,
293-
dtype=...,
259+
dtype: Literal["Interval"] = ...,
294260
name: Hashable | None = ...,
295261
copy: bool = ...,
296-
fastpath: bool = ...,
297-
) -> IntervalSeries[Timedelta]: ...
262+
) -> IntervalSeries[_OrderableT]: ...
298263
@overload
299264
def __new__(
300265
cls,
@@ -303,27 +268,24 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
303268
index: Axes | None = ...,
304269
name: Hashable | None = ...,
305270
copy: bool = ...,
306-
fastpath: bool = ...,
307271
) -> Self: ...
308272
@overload
309273
def __new__(
310274
cls,
311275
data: Series[S1] | dict[int, S1] | dict[_str, S1] = ...,
312276
index: Axes | None = ...,
313-
dtype=...,
277+
dtype: Dtype = ...,
314278
name: Hashable | None = ...,
315279
copy: bool = ...,
316-
fastpath: bool = ...,
317280
) -> Self: ...
318281
@overload
319282
def __new__(
320283
cls,
321284
data: object | _ListLike | None = ...,
322285
index: Axes | None = ...,
323-
dtype=...,
286+
dtype: Dtype = ...,
324287
name: Hashable | None = ...,
325288
copy: bool = ...,
326-
fastpath: bool = ...,
327289
) -> Series: ...
328290
@property
329291
def hasnans(self) -> bool: ...

0 commit comments

Comments
 (0)