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

Conversation

loicdiridollou
Copy link
Member

Potential fix for #571 to typehint a Series created from an empty list to be typed as pd.Series[Any].
Open for feedback if this is not the optimal type.


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.

Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

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

@Dr-Irv Dr-Irv merged commit 0ab562c into pandas-dev:main Nov 11, 2024
10 checks passed
@loicdiridollou loicdiridollou deleted the gh571_series_from_empty_list branch December 14, 2024 19:58
pan-vlados added a commit to pan-vlados/pandas-stubs that referenced this pull request Dec 18, 2024
    - fix Index.str.split method return wrong result;
    - add test for Index.str.split method with expand=False;
    - return changes performed in pull request pandas-dev#1029.
Dr-Irv added a commit that referenced this pull request Dec 18, 2024
…nd=False (#1075)

* GH1074 Add type hint Series[list[str]] for Series.str.split with expand=False

* Updates:

    - fix Index.str.split method return wrong result;
    - add test for Index.str.split method with expand=False;
    - return changes performed in pull request #1029.

* Update tests/test_indexes.py

Co-authored-by: Irv Lustig <irv@princeton.com>

* Update tests/test_series.py

Co-authored-by: Irv Lustig <irv@princeton.com>

* Update tests/test_series.py

Co-authored-by: Irv Lustig <irv@princeton.com>

* Updates:

    - combine two str.split overloads and keep only _TS and _TS2;
    - fix test_indexes.py test for test_str_split().

* pre-commit fixes

* Add type hints and tests for str.rsplit() for expand=False

---------

Co-authored-by: Irv Lustig <irv@princeton.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty Series is considered TimestampSeries
2 participants