-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Mypy infers x.get("a") or x.get("b", "c")
to str | None
, even when x
is dict[str, str]
#15534
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
Very strange error. If I had to guess, I'd say it has to do with the slightly weird signature of You could try defining your own |
@AlexWaygood |
Ahh, never mind :( def f4(x: dict[str, str]) -> str:
q = x.get("a") or x.get("b", "c") # N: Revealed type is "Union[builtins.str, None]"
reveal_type(q)
return q # E: Incompatible return value type (got "str | None", expected "str") [return-value] |
It does feel like a type context issue, but I think @tmke8 may be right and this is python/typeshed#10293. Unfortunately, as the typeshed PR indicates, that issue may not be easy to fix. |
(I just merged a different typeshed PR that also hopefully fixes python/typeshed#10293, but had a much less horrifying report from mypy_primer.) |
I confirmed that with python/typeshed#10293, the issue is resolved, so no mypy changes will be necessary here. The fix might not make it into mypy v1.5, but it will definitely be included in mypy v1.6 if not. With mypy v1.4.1:
With mypy v1.4.1 and
|
Bug Report
Mypy wrongly infers the type of the expression
x.get("a") or x.get("b", "c")
to bestr | None
, even whenx
is explicitly adict[str, str]
.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=33335d3d1d19e5858beb76ed65ce8470
I realise the examples are not logically equivalent, but they should all evaluate to strings
Expected Behavior
No errors.
Actual Behavior
Mypy returns
main.py:8: error: Incompatible return value type (got "str | None", expected "str") [return-value]
Your Environment
mypy.ini
(and other config files): N/AThe text was updated successfully, but these errors were encountered: