From 01736d783ad7aa29b02bdedd86687b671fd13bd4 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sun, 10 Nov 2024 18:04:29 -0500 Subject: [PATCH 1/3] GH571 Update typehint when creating a Series from an empty list --- pandas-stubs/core/series.pyi | 10 ++++++++++ tests/test_frame.py | 8 ++++++++ 2 files changed, 18 insertions(+) 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..be863faf4 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -7,6 +7,7 @@ Iterator, Mapping, MutableMapping, + Sequence, ) import csv import datetime @@ -21,6 +22,7 @@ Any, Callable, Generic, + Never, TypedDict, TypeVar, Union, @@ -3556,3 +3558,9 @@ 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.""" + new_tab: Sequence[Never] = [] + check(assert_type(pd.Series(new_tab), "pd.Series[Any]"), pd.Series) From 8f1694ce37e220aa7608b38d8cfb5e1f28839758 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Sun, 10 Nov 2024 18:17:20 -0500 Subject: [PATCH 2/3] GH571 Change import source of Never to typing_extensions --- tests/test_frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_frame.py b/tests/test_frame.py index be863faf4..1356fd8df 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -22,7 +22,6 @@ Any, Callable, Generic, - Never, TypedDict, TypeVar, Union, @@ -40,6 +39,7 @@ from pandas.core.series import Series import pytest from typing_extensions import ( + Never, TypeAlias, assert_never, assert_type, From 85c1e2aca2c5f676c85e5faa7a2bcd925c83a838 Mon Sep 17 00:00:00 2001 From: Loic Diridollou Date: Mon, 11 Nov 2024 13:13:31 -0500 Subject: [PATCH 3/3] GH571 PR feedback --- tests/test_frame.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_frame.py b/tests/test_frame.py index 1356fd8df..5edd0901e 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -3561,6 +3561,9 @@ class MyDict(TypedDict): def test_series_empty_dtype() -> None: - """Test for the creation of a Series from an empty list GH571.""" - new_tab: Sequence[Never] = [] + """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)