You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
changed the title [-]Mypy complains inconsistent MRO when using isinstance() on a generic Series object[/-][+]Mypy complains about inconsistent MRO when using `isinstance()` on a generic `Series` object[/+]on Sep 5, 2022
I don't think we can fix this. The code you wrote won't execute because pandas doesn't treat Series as generic. So trying to fix an issue for type checking with respect to code that won't execute doesn't seem like a good use of time.
We're using the generic form of Series to be able to limit certain operations (e.g., adding two series consisting of timestamps). But you can't declare something to be of type Series[sometype] due to differences between pandas and the stubs.
% mypy repro.py
repro.py:4: error: Subclass of "Series[int]" and "TimestampSeries" cannot exist: would have inconsistent method resolution order [unreachable]
Found 1 error in 1 file (checked 1 source file)
@thomkeh I tried this with pandas-stubs 1.4.4.220919 and cannot reproduce the latter example. Not sure what we have changed since then, so can you try with that version and see if it still happens?
Also, double check your mypy version - I was using 0.971 (which you said above)
I am guessing this is some kind of mypy bug. We test with warn-unreachable=False, because when using mypy on the pandas source, it produces some false positives.
It's pretty odd that it would report an error in your code related to what is inside the stubs.
@thomkeh did you find any resolution for this issue?
I am also having the same problem.
No idea if the issue is within mypy or pandas-stubs, but I agree that my code executes fine and it type hints with no issues if I just use a Series rather than a Series[float] in my case, so long as I have a type: ignore[type-arg]
I also found that if I have something like:
result = input.loc[x] if isinstance(input, Series) else input
results in a Item "float" of "Union[float, Series[float]]" has no attribute "loc" which it shouldn't do given that it only runs that code if input is a Series.
@jonyscathe I don't have a solution. My next step was going to be to open an issue in the mypy repository but I wanted to have a smaller reproducing code snippet first.
minimal_mro_problem.py:20: note: Revealed type is "minimal_mro_problem.Series[builtins.int]"
minimal_mro_problem.py:21: error: Subclass of "Series[int]" and "TimestampSeries" cannot exist: would have inconsistent method resolution order [unreachable]
Activity
[-]Mypy complains inconsistent MRO when using isinstance() on a generic Series object[/-][+]Mypy complains about inconsistent MRO when using `isinstance()` on a generic `Series` object[/+]Dr-Irv commentedon Sep 5, 2022
I don't think we can fix this. The code you wrote won't execute because
pandas
doesn't treatSeries
as generic. So trying to fix an issue for type checking with respect to code that won't execute doesn't seem like a good use of time.We're using the generic form of
Series
to be able to limit certain operations (e.g., adding two series consisting of timestamps). But you can't declare something to be of typeSeries[sometype]
due to differences between pandas and the stubs.tmke8 commentedon Sep 5, 2022
I have code like that in my code base and it executes perfectly fine:
Variable annotations aren't executed within functions so this definitely works.
tmke8 commentedon Sep 5, 2022
Here is a reproducer without the type annotation:
mypy:
Dr-Irv commentedon Sep 6, 2022
Thanks for the latter example. We'll look into it.
Dr-Irv commentedon Sep 25, 2022
@thomkeh I tried this with pandas-stubs 1.4.4.220919 and cannot reproduce the latter example. Not sure what we have changed since then, so can you try with that version and see if it still happens?
Also, double check your mypy version - I was using 0.971 (which you said above)
tmke8 commentedon Sep 28, 2022
I checked again and I noticed that I can only reproduce it with the
--warn-unreachable
flag:I just did it on mypy 0.981 and with pandas stub 1.5.0.220926.
Dr-Irv commentedon Sep 28, 2022
I am guessing this is some kind of
mypy
bug. We test withwarn-unreachable=False
, because when usingmypy
on the pandas source, it produces some false positives.It's pretty odd that it would report an error in your code related to what is inside the stubs.
jonyscathe commentedon Oct 4, 2022
@thomkeh did you find any resolution for this issue?
I am also having the same problem.
No idea if the issue is within mypy or pandas-stubs, but I agree that my code executes fine and it type hints with no issues if I just use a
Series
rather than aSeries[float]
in my case, so long as I have atype: ignore[type-arg]
I also found that if I have something like:
result = input.loc[x] if isinstance(input, Series) else input
results in a
Item "float" of "Union[float, Series[float]]" has no attribute "loc"
which it shouldn't do given that it only runs that code ifinput
is a Series.tmke8 commentedon Oct 4, 2022
@jonyscathe I don't have a solution. My next step was going to be to open an issue in the mypy repository but I wanted to have a smaller reproducing code snippet first.
tmke8 commentedon Oct 6, 2022
I found a standalone reproducer:
with
mypy --warn-unreachable
:__new__
returns a subclass and--warn-unreachable
is turned on python/mypy#13824tmke8 commentedon Oct 6, 2022
I reported it here: python/mypy#13824
Dr-Irv commentedon Nov 20, 2022
Bug still exists in mypy 0.990
twoertwein commentedon Aug 10, 2023
This seems to work now with mypy 1.4.1 and the latest pandas-stubs :)
tmke8 commentedon Aug 10, 2023
Hmm, I can still reproduce:
Or does
pip install git+https://github.com/pandas-dev/pandas-stubs.git
not give me the most recent version?twoertwein commentedon Aug 10, 2023
You are right - I tested it without
--warn-unreachable
Dr-Irv commentedon Jan 3, 2025
Bug in
mypy
still exists in mypy 1.14.1, but only when using--warn-unreachable
cmp0xff commentedon Jul 19, 2025
In
mypy
1.17.0, the issue does not seem to exist any more.Dr-Irv commentedon Jul 24, 2025
agreed. Thanks for checking. Closing as a result