-
-
Notifications
You must be signed in to change notification settings - Fork 148
Closed
Labels
mypy bugRequires mypy to fix a bugRequires mypy to fix a bug
Description
Describe the bug
A pd.Series variable changes type after an isinstance check.
To Reproduce
- 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) # <- ???
- Indicate which type checker you are using (
mypy
orpyright
).
mypy - 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
Metadata
Metadata
Assignees
Labels
mypy bugRequires mypy to fix a bugRequires mypy to fix a bug
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Dr-Irv commentedon Feb 25, 2024
pyright
1.1.351 gets it right:I think this is a
mypy
bug. See python/mypy#15322I'll keep this open in case
mypy
ever fixes that issue.loicdiridollou commentedon Nov 11, 2024
I am unable to reproduce this one, used the same example as @tvdboom above and this is what
mypy
returns to me:Not sure exactly where it got fixed but it seems like it does not map to
TimestampSeries
anymore.Dr-Irv commentedon 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 aSeries[float]
. The firstreveal_type()
showed that theUnion
containedSeries[Any]
, and you'd expect the same from the secondreveal_type()
.So
mypy
still has to address the linked issue.loicdiridollou commentedon Nov 11, 2024
Ohh I understand, thanks a lot for all the details!
loicdiridollou commentedon Aug 7, 2025
With the release of 1.17 for mypy I checked this issue again and seems like it is fixed.
With pyright we get pd.Series for each of the reveal_type:
And for mypy I get the Union for the first reveal_type and Series[Any] for the latter:
Is this the result that we are expecting?
Dr-Irv commentedon Aug 7, 2025
Yes. I will close as a result.