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
False positive [arg-type] error when updating a dict with a TypedDict #9086
Comments
Since TypedDict objects use structural subtyping, there could be additional items not visible through the type |
I agree that In the above sample code, Perhaps I am missing something, can you provide a more specific example of where updating a plain |
I figured out my misunderstanding, and I now agree with Jukka that it's not a false positive. TL;DR: one has to inform mypy using either a type comment or variable annotation that I have explained my reasoning below: Example 1 (original example, restated) foo: Foo = {"baz": 9000}
reveal_type(foo) # note: Revealed type is 'TypedDict('updating_with_typeddict.Foo', {'baz': builtins.int})'
spam = {"ham": {"eggs": 5}}
reveal_type(spam["ham"]) # note: Revealed type is 'builtins.dict*[builtins.str*, builtins.int*]'
spam["ham"].update(foo) # error: Argument 1 to "update" of "dict" has incompatible type "Foo";
# expected "Mapping[str, int]" [arg-type] In my original post, I had assumed that the type of This then gets to the Type Consistency section of PEP 589. One can read there to understand why mypy is raising an error. Example 2 (with correct method) Here is the other example from the original question. We can see corge = {"plugh": 9001}
reveal_type(corge) # note: Revealed type is 'builtins.dict[builtins.str*, builtins.int*]'
corge.update(foo) # error: Argument 1 to "update" of "dict" has incompatible type "Foo";
# expected "Mapping[str, int]" [arg-type]
spam["ham"].update(corge) # no error, because the types match up The correct method, in which mypy no longer raises an error, is to specify the desired type of corge2: dict = {"plugh": 9001}
reveal_type(corge2) # note: Revealed type is 'builtins.dict[Any, Any]'
corge2.update(foo) # no error Original Example Corrected And to apply the same fix to the original example 1: spam2: Dict[str, dict] = {"ham": {"eggs": 5}}
reveal_type(spam2["ham"]) # note: Revealed type is 'builtins.dict*[Any, Any]'
spam2["ham"].update(foo) # no error |
I have found that mypy raises an error when using
update
on adict
and passing in an instance of aTypedDict
.According to the mypy docs for TypedDict:
Thus, I think this is a false positive.
Repro
This code runs fine with Python 3.8.2 and mypy 0.782.
The text was updated successfully, but these errors were encountered: