-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Unable to remove Optional wrapper when using Mapping[str, str] instead of dict #11618
Comments
This does look like a bug in mypy, probably in the TypeVar constraint solver logic. This type checks fine in pyright. Interestingly, if you add a mapping = assign_if_none(mapping, cast(Mapping[str, str], {})) or empty: Mapping[str, str] = {}
mapping = assign_if_none(mapping, empty) |
Does |
My guess would be that mypy infers |
Yeah, looks like that's it. A similar thing happens with List, but not with Sequence |
This example got fixed in #16994 |
Hello, I'm not sure if this is a bug, but perhaps it'll help to clarify the difference between using
dict
andMapping[str, str]
as annotations.This is a common pattern in Python to avoid mutable default arguments:
So I tried writing a function to capture this pattern.
Now you can use it like this:
But mypy raises errors when I use a type annotation with
Mapping
orDict
instead ofdict
.Full example:
func_1
has no errors, butfunc_2
does.I tried putting
reveal_type(mapping)
before and after the call toassign_if_none
in both functions. Here are the results:func_1
:func_2
:So in
func_1
, mypy correctly infers that the call toassign_if_none
removes theOptional
wrapper. But this doesn't happen infunc_2
.If this isn't a bug with mypy, is there a better way to annotate the function
assign_if_none
so it works infunc_2
?Environment
The text was updated successfully, but these errors were encountered: