Skip to content
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

pd.Series gets converted to pd.TimestampSeries after isinstance check #877

Open
tvdboom opened this issue Feb 24, 2024 · 4 comments
Open
Labels
mypy bug Requires mypy to fix a bug

Comments

@tvdboom
Copy link

tvdboom commented Feb 24, 2024

Describe the bug
A pd.Series variable changes type after an isinstance check.

To Reproduce

  1. Provide a minimal runnable pandas example that is not properly checked by the stubs.
df1: pd.Series | pd.DataFrame = pd.Series()
reveal_type(df1)  # <- As expected
if isinstance(df1, pd.Series):
    reveal_type(df1)  # <- ???
  1. Indicate which type checker you are using (mypy or pyright).
    mypy
  2. Show the error message received from that type checker while checking your example.
note: Revealed type is "Union[pandas.core.series.Series[Any], pandas.core.frame.DataFrame]"
note: Revealed type is "pandas.core.series.TimestampSeries"

Please complete the following information:

  • OS: Windows
  • OS Version: 10
  • python version: 3.11
  • version of type checker: 1.8.0
  • version of installed pandas-stubs: 2.2.0.240218
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Feb 25, 2024

pyright 1.1.351 gets it right:

issue877.py:4:13 - information: Type of "df1" is "Series[Unknown]"
issue877.py:5:4 - error: Unnecessary isinstance call; "Series[Unknown]" is always an instance of "Series[Unknown]" (reportUnnecessaryIsInstance)
issue877.py:6:17 - information: Type of "df1" is "Series[Unknown]"

I think this is a mypy bug. See python/mypy#15322

I'll keep this open in case mypy ever fixes that issue.

@Dr-Irv Dr-Irv added the mypy bug Requires mypy to fix a bug label Feb 25, 2024
@loicdiridollou
Copy link
Contributor

I am unable to reproduce this one, used the same example as @tvdboom above and this is what mypy returns to me:

===========================================
Beginning: 'Run mypy on 'tests' (using the local stubs) and on the local stubs'
===========================================

tests/test_frame.py:3584: note: Revealed type is "Union[pandas.core.series.Series[Any], pandas.core.frame.DataFrame]"
tests/test_frame.py:3586: note: Revealed type is "pandas.core.series.Series[builtins.float]"

Not sure exactly where it got fixed but it seems like it does not map to TimestampSeries anymore.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Nov 11, 2024

It's still an issue, because what happens now is that it is picking up the first overload for Series.__new__(), which returns a Series[float]. The first reveal_type() showed that the Union contained Series[Any], and you'd expect the same from the second reveal_type().

So mypy still has to address the linked issue.

@loicdiridollou
Copy link
Contributor

Ohh I understand, thanks a lot for all the details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mypy bug Requires mypy to fix a bug
Projects
None yet
Development

No branches or pull requests

3 participants