diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 080c6a826..1729d3244 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -241,6 +241,16 @@ class Series(IndexOpsMixin[S1], NDFrame): copy: bool = ..., ) -> Series[float]: ... @overload + def __new__( # type: ignore[overload-overlap] + cls, + data: Sequence[Never], + index: Axes | None = ..., + *, + dtype: Dtype = ..., + name: Hashable = ..., + copy: bool = ..., + ) -> Series[Any]: ... + @overload def __new__( cls, data: ( diff --git a/tests/test_frame.py b/tests/test_frame.py index bee41a201..5edd0901e 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -7,6 +7,7 @@ Iterator, Mapping, MutableMapping, + Sequence, ) import csv import datetime @@ -38,6 +39,7 @@ from pandas.core.series import Series import pytest from typing_extensions import ( + Never, TypeAlias, assert_never, assert_type, @@ -3556,3 +3558,12 @@ class MyDict(TypedDict): my_dict = MyDict(a="", b="") sr = pd.Series(my_dict) check(assert_type(sr, pd.Series), pd.Series) + + +def test_series_empty_dtype() -> None: + """Test for the creation of a Series from an empty list GH571 to map to a Series[Any].""" + new_tab: Sequence[Never] = [] # need to be typehinted to please mypy + check(assert_type(pd.Series(new_tab), "pd.Series[Any]"), pd.Series) + check(assert_type(pd.Series([]), "pd.Series[Any]"), pd.Series) + # ensure that an empty string does not get matched to Sequence[Never] + check(assert_type(pd.Series(""), "pd.Series[str]"), pd.Series)