-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Open
Labels
Description
I'm seeing an Incompatible return value type error in 0.981 that wasn't present in previous versions of mypy.
To Reproduce
from typing import TypeVar
T = TypeVar("T", dict, float)
def method(x: T) -> T:
if isinstance(x, dict):
return {}
else:
return 0.0
Expected Behavior
% mypy --version
mypy 0.971 (compiled: yes)
% mypy test-mypy-error.py
Success: no issues found in 1 source file
Actual Behavior
% mypy --version
mypy 0.981 (compiled: yes)
% mypy test-mypy-error.py
[Success: no issues found in 1 source file](test-mypy-error.py:7: error: Incompatible return value type (got "Dict[<nothing>, <nothing>]", expected "float"))
Your Environment
mypy version 0.981
python version 3.9.13
Activity
hauntsaninja commentedon Oct 3, 2022
Interesting,
mypy_primer -p ~/dev/mypy_primer/test.py --bisect --new v0.981 --old v0.971 --debug
bisects this to #13386hauntsaninja commentedon Oct 3, 2022
Looks like it's from python/typeshed#8465, cc @sobolevn
sobolevn commentedon Oct 3, 2022
Very strange 😨
How can
__hash__
affect this?hauntsaninja commentedon Oct 3, 2022
Yeah, it's pretty strange, but it's true. Fixes itself on master if you add
__hash__
back to float. These dunders...sobolevn commentedon Oct 3, 2022
It does not feel right. It looks like it exposes some other bug. I will have a look.
sobolevn commentedon Oct 4, 2022
So, I am diving into it.
First of all, I was able to reproduce this problem.
Secondly, this works:
Also,
set
andlist
work.But,
float
andstr
does not work.At this point I am sure that this is a bug.
Revealed types
Let's reveal types:
Output:
Looks like
Revealed type is "ex.<subclass of "dict" and "str">"
should not ever happen.What about types that do work?
Outputs:
So, let's find why the intersection of two instances is created in the first place 🤔
.intersect_instances
This happens, because
self.intersect_instances((v, t), ctx)
works this way:headtr1ck commentedon Oct 29, 2022
Possibly related: #13956
Even though this fails also for previous mypy versions.
emirkmo commentedon Nov 24, 2022
I am not sure if this is related but I wanted to add another data point on 0.991
Error:
Without the
assert isinstance(df, pd.DataFrame) # for mypy type failure
Mypy falsely warns thatSeries.rename
does not have the proper overload, butdf
should already be narrowed to apd.DataFrame
by that point. Pylance/pyright gets it right.Environment:
mypy 0.991 (compiled: yes)
Python 3.10.6
Options:
--allow-redefinition --allow-untyped-globals --ignore-missing-imports --implicit-reexport --enable-incomplete-feature=Unpack
tyralla commentedon Jan 3, 2023
Are we sure the initial report reveals a bug or is this behaviour by design? If I am right, we can consider it only a bug if the order of the type variable constraints matters. Then Mypy should behave as follows:
Are there any promises that Mypy or other type checkers prioritise the first over the second type variable constraint when estimating return types in the context of multiple inheritance?
If we agree that order matters (and if I do not miss other potential problems here), adjusting Mypy should be manageable with reasonable effort.
tyralla commentedon Jan 4, 2023
It seems to be a known issue. I found the following test case (added by @Michael0x2a):
8 remaining items