Skip to content

Commit

Permalink
GH571 Update typehint when creating a Series from an empty list (pand…
Browse files Browse the repository at this point in the history
…as-dev#1029)

* GH571 Update typehint when creating a Series from an empty list

* GH571 Change import source of Never to typing_extensions

* GH571 PR feedback
  • Loading branch information
loicdiridollou authored Nov 11, 2024
1 parent 25b7a9e commit 0ab562c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down
11 changes: 11 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Iterator,
Mapping,
MutableMapping,
Sequence,
)
import csv
import datetime
Expand Down Expand Up @@ -38,6 +39,7 @@
from pandas.core.series import Series
import pytest
from typing_extensions import (
Never,
TypeAlias,
assert_never,
assert_type,
Expand Down Expand Up @@ -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)

0 comments on commit 0ab562c

Please sign in to comment.