Skip to content

Commit 0ab562c

Browse files
GH571 Update typehint when creating a Series from an empty list (#1029)
* GH571 Update typehint when creating a Series from an empty list * GH571 Change import source of Never to typing_extensions * GH571 PR feedback
1 parent 25b7a9e commit 0ab562c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pandas-stubs/core/series.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ class Series(IndexOpsMixin[S1], NDFrame):
241241
copy: bool = ...,
242242
) -> Series[float]: ...
243243
@overload
244+
def __new__( # type: ignore[overload-overlap]
245+
cls,
246+
data: Sequence[Never],
247+
index: Axes | None = ...,
248+
*,
249+
dtype: Dtype = ...,
250+
name: Hashable = ...,
251+
copy: bool = ...,
252+
) -> Series[Any]: ...
253+
@overload
244254
def __new__(
245255
cls,
246256
data: (

tests/test_frame.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Iterator,
88
Mapping,
99
MutableMapping,
10+
Sequence,
1011
)
1112
import csv
1213
import datetime
@@ -38,6 +39,7 @@
3839
from pandas.core.series import Series
3940
import pytest
4041
from typing_extensions import (
42+
Never,
4143
TypeAlias,
4244
assert_never,
4345
assert_type,
@@ -3566,3 +3568,12 @@ class MyDict(TypedDict):
35663568
my_dict = MyDict(a="", b="")
35673569
sr = pd.Series(my_dict)
35683570
check(assert_type(sr, pd.Series), pd.Series)
3571+
3572+
3573+
def test_series_empty_dtype() -> None:
3574+
"""Test for the creation of a Series from an empty list GH571 to map to a Series[Any]."""
3575+
new_tab: Sequence[Never] = [] # need to be typehinted to please mypy
3576+
check(assert_type(pd.Series(new_tab), "pd.Series[Any]"), pd.Series)
3577+
check(assert_type(pd.Series([]), "pd.Series[Any]"), pd.Series)
3578+
# ensure that an empty string does not get matched to Sequence[Never]
3579+
check(assert_type(pd.Series(""), "pd.Series[str]"), pd.Series)

0 commit comments

Comments
 (0)