-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Argument 2 to pop of MutableMapping has incompatible type #10152
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 only seems to be an issue if the value type is a type variable. There's probably some confusion between type variables in |
On 0.942 (and master) you now get three false-positive errors instead of just two: import typing as t
T = t.TypeVar('T')
V = t.TypeVar('V')
def test(key: T, value: V):
values: t.Dict[T, V] = {}
print(values.pop(key, None)) # error: Argument 2 to "pop" of "MutableMapping" has incompatible type "None"; expected "V"
values.pop(key, value) # ok
values.pop(key, None) # error: Argument 2 to "pop" of "MutableMapping" has incompatible type "None"; expected "V"
values.pop(key, 42) # error: Argument 2 to "pop" of "MutableMapping" has incompatible type "int"; expected "V" |
This issue may be fixed in mypy 1.6.0. mypy-play.net (@NiklasRosenstein's code) [edited: fixed links] |
Still seems fixed in 1.8.0. I suggest closing this issue. |
This does not appear to be fixed for dicts in 1.8.0 (also tried 1.9.0 and 1.10.0), although I'm having trouble figuring out exactly what conditions lead to it happening. Seems fine in simple examples, but in more complex code (some of which uses typevars, other parts do not but still trigger this) I get this
|
I can reproduce the old error like this: import sys
values_by_name = {"one": 1, "two": 2}
for value in values_by_name.values():
assert value > 0
if value := values_by_name.pop(sys.argv[1], None):
print(value)
else:
print("Not found")
Significantly, this only happens if mypy has already decided that the LHS of the assignment is of type
|
@bhperry Does this possibly explain what you were seeing? |
@alicederyn Great catch! Yes I think that is exactly what was happening. Didn't think about the LHS because it was a var used in a previous loop as |
@finite-state-machine should this be split off to a separate issue, or leave this one open to cover it? |
Closing since this issue no longer reproduces as of v1.6.0 (specifically after the typeshed sync #15792). @bhperry @alicederyn Feel free to open a new issue. At a glance, it looks like this falls in the bucket of cases where bidirectional inference can have bad results when there's a union of type variables in the return type. You'll find some related issues under the
topic-type-context
|
Bug Report
Using
MutableMapping.pop(key, default)
inconsistently results in an error for the type of thedefault
argument.To Reproduce
Expected Behavior
I expect no errors when checking the above code with Mypy. The definition of
MutableMapping.pop()
allows using a different type for thedefault
argument value (in which case a union of the map's value type and the default argument's type is returned).What is curious is that the line with
print()
does not result in an error, even though the.pop()
call is the same as two lines below.Your Environment
mypy.ini
(and other config files): N/aThe text was updated successfully, but these errors were encountered: