Skip to content

Commit 0e22870

Browse files
authored
handle pyright inferring mixed float and int lists (#1097)
1 parent 3547cda commit 0e22870

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

pandas-stubs/core/series.pyi

+41-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class Series(IndexOpsMixin[S1], NDFrame):
343343
copy: bool = ...,
344344
) -> IntervalSeries[_OrderableT]: ...
345345
@overload
346-
def __new__(
346+
def __new__( # type: ignore[overload-overlap]
347347
cls,
348348
data: Scalar | _ListLike | dict[HashableT1, Any] | None,
349349
index: Axes | None = ...,
@@ -353,6 +353,46 @@ class Series(IndexOpsMixin[S1], NDFrame):
353353
copy: bool = ...,
354354
) -> Self: ...
355355
@overload
356+
def __new__( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
357+
cls,
358+
data: Sequence[bool],
359+
index: Axes | None = ...,
360+
*,
361+
dtype: Dtype = ...,
362+
name: Hashable = ...,
363+
copy: bool = ...,
364+
) -> Series[bool]: ...
365+
@overload
366+
def __new__( # type: ignore[overload-overlap]
367+
cls,
368+
data: Sequence[int],
369+
index: Axes | None = ...,
370+
*,
371+
dtype: Dtype = ...,
372+
name: Hashable = ...,
373+
copy: bool = ...,
374+
) -> Series[int]: ...
375+
@overload
376+
def __new__(
377+
cls,
378+
data: Sequence[float],
379+
index: Axes | None = ...,
380+
*,
381+
dtype: Dtype = ...,
382+
name: Hashable = ...,
383+
copy: bool = ...,
384+
) -> Series[float]: ...
385+
@overload
386+
def __new__( # type: ignore[overload-cannot-match] # pyright: ignore[reportOverlappingOverload]
387+
cls,
388+
data: Sequence[int | float],
389+
index: Axes | None = ...,
390+
*,
391+
dtype: Dtype = ...,
392+
name: Hashable = ...,
393+
copy: bool = ...,
394+
) -> Series[float]: ...
395+
@overload
356396
def __new__(
357397
cls,
358398
data: S1 | _ListLike[S1] | dict[HashableT1, S1] | dict_keys[S1, Any],

tests/test_series.py

+13
Original file line numberDiff line numberDiff line change
@@ -3539,3 +3539,16 @@ def test_series_dict() -> None:
35393539
pd.Series,
35403540
str,
35413541
)
3542+
3543+
3544+
def test_series_int_float() -> None:
3545+
# pyright infers mixtures of int and float in a list as list[int | float]
3546+
check(assert_type(pd.Series([1, 2, 3]), "pd.Series[int]"), pd.Series, np.integer)
3547+
check(
3548+
assert_type(pd.Series([1.0, 2.0, 3.0]), "pd.Series[float]"),
3549+
pd.Series,
3550+
np.float64,
3551+
)
3552+
check(
3553+
assert_type(pd.Series([1, 2.0, 3]), "pd.Series[float]"), pd.Series, np.float64
3554+
)

0 commit comments

Comments
 (0)