-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Incorrect error message Dict[str, str] not allowed to Dict[Hashable, Any] #12464
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
Comments
This is correct since Mapping is invariant in its key type. See https://mypy.readthedocs.io/en/stable/common_issues.html#invariance-vs-covariance. As a workaround, use |
OK, you could say that, but what about the examples at the end, where you see this: col_map = {"a": "b"}
myfun4(col_map) # incorrect error reported but this reports no error: myfun4({"a": "b"}) # no error reported Here |
That's a frequently observed consequence of how mypy infers types. If you assign it to a variable first, it sets the variable's type to |
Well, I don't see what is more precise than reveal_type(col_map)
reveal_type({"a": "b"}) I get
In the example, the error message is:
Why doesn't |
Because |
So when And why does |
You can check with
Do you mean that |
Oh, so you're saying that Note: pyright isn't complaining about this, so that's where some of the confusion is arising. |
"precise" was a bad choice of words on my part. It's more like the type is a better match for the expected type. I think pyright has a bug here. A simpler example: from typing import Hashable
def f(x: list[Hashable]):
x.append("x")
def g(x: list[object]):
pass
l: list[int] = [1]
f(l) # no pyright error
g(l) # error (as expected) |
Bug Report
If mypy infers a variable to be
Dict[str, str]
, then it does not allow it to be passed to a function with the type ofMapping[Hashable, Any]
orUnion[Mapping[Hashable, Any], Hashable]
mypy does not allow
{1, 1}
to be passed to an argument of typeUnion[Mapping[Hashable, Any], Hashable]
, but is OK with a type ofMapping[Hashable, Any]
To Reproduce
(Write your steps here:)
Expected Behavior
No errors reported
Actual Behavior
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: