From 0ab562cffa99867f4fc9897a6f355e426182f533 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 11 Nov 2024 14:09:56 -0500 Subject: [PATCH] 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 --- pandas-stubs/core/series.pyi | 10 ++++++++++ tests/test_frame.py | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index ce27d44e..7dabbf76 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 bc148b4b..33d09e11 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, @@ -3566,3 +3568,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)