Skip to content

GH571 Update typehint when creating a Series from an empty list #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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)
Loading