Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 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,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] = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the test, it should work without setting a type for new_tab. So if you change this line to new_tab = [], does the empty list match Sequence[Never] ?

also, double check that passing an empty string will NOT match this, because strings are sequences, and I'm not sure if "" would be an OK match for Sequence[Never]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, in order:

  • mypy raises an error when instantiating an empty sequence so I needed to annotate it
===========================================
Beginning: 'Run mypy on 'tests' (using the local stubs) and on the local stubs'
===========================================

tests/test_frame.py:3566: error: Need type annotation for "new_tab" (hint: "new_tab: list[<type>] = ...")  [var-annotated]
Found 1 error in 1 file (checked 226 source files)
  • I have added a test for the empty string

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this is the error that you get when trying to typehint "" as a Sequence[Never] so the types are well separated.

tests/test_frame.py:3571: error: Incompatible types in assignment (expression has type "str", variable has type "Sequence[Never]")  [assignment]

for mypy above and pyright below

Diagnostics: 
Type "Literal['']" is not assignable to declared type "Sequence[Never]"  
"Literal['']" is not assignable to "Sequence[Never]"                         
Type parameter "_T_co@Sequence" is covariant, but "str" is not a subtype of "Never"
Type "str" is not assignable to type "Never" [reportAssignmentType]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see that you added an explicit test for pd.Series([]), so the issue with new_tab is nothing to worry about.

check(assert_type(pd.Series(new_tab), "pd.Series[Any]"), pd.Series)
Loading