You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Apparent False Positive With Dict Update Method #5849
Please provide more information to help us understand the issue:
When I define a somewhat complex and heterogeneous dictionary (meant to later be converted to JSON) it causes mypy to alert me to errors which I do not believe are valid.
Are you reporting a bug, or opening a feature request?
Bug
Please insert below the code you are checking with mypy,
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
I would also appreciate any tips that anyone has on how to properly type annotate highly heterogeneous dictionaries to be used for the purpose of interacting with REST APIs.
I would also appreciate any tips that anyone has on how to properly type annotate highly heterogeneous dictionaries to be used for the purpose of interacting with REST APIs.
The issue itself is valid, and similar to how mypy overreacts to concatenating lists with different types, the latter can be just fixed in typeshed, while this one might require some special-casing. Btw, have you tried giving an explicit annotation to self.d3?
Thanks, typeddict looks very promising. I just tried giving self.d3 an explicit annotation and it appears to not make a difference (same error pops up when I run mypy).
I assume that what's happening is that the type of the RHS of that assignment is being resolved before mypy is aware that B.foo should have the same type as A.foo.
This can be fixed by annotating foo. But I think this is something of a problem for libraries which offer inheritance as part of their interface. If a 3rd party developer inherits from A, they have to annotate foo if they override it.
Hinting with processed: dict seems to silence the warning. I think this solves the issue, but it would be awesome if mypy could derive the type of processed to be typing.TypedDict('<something...>', id=str, body=str, document=str). I don't know if mypy is capable of doing that, or if this type of inference has precedents, though.
Activity
lvfrazao commentedon Oct 28, 2018
I would also appreciate any tips that anyone has on how to properly type annotate highly heterogeneous dictionaries to be used for the purpose of interacting with REST APIs.
ilevkivskyi commentedon Oct 28, 2018
https://mypy.readthedocs.io/en/latest/more_types.html#typeddict
The issue itself is valid, and similar to how mypy overreacts to concatenating lists with different types, the latter can be just fixed in typeshed, while this one might require some special-casing. Btw, have you tried giving an explicit annotation to
self.d3?lvfrazao commentedon Oct 28, 2018
Thanks,
typeddictlooks very promising. I just tried givingself.d3an explicit annotation and it appears to not make a difference (same error pops up when I run mypy).sirosen commentedon Mar 12, 2021
I just came across an interesting variant of this error:
I assume that what's happening is that the type of the RHS of that assignment is being resolved before mypy is aware that
B.fooshould have the same type asA.foo.This can be fixed by annotating
foo. But I think this is something of a problem for libraries which offer inheritance as part of their interface. If a 3rd party developer inherits fromA, they have to annotatefooif they override it.jdferreira commentedon Jun 29, 2021
I've stumbled upon the same issue.
Hinting with
processed: dictseems to silence the warning. I think this solves the issue, but it would be awesome if mypy could derive the type ofprocessedto betyping.TypedDict('<something...>', id=str, body=str, document=str). I don't know if mypy is capable of doing that, or if this type of inference has precedents, though.erictraut commentedon Aug 10, 2023
This bug appears to have been fixed. The latest version of mypy doesn't emit an error for any of the above code samples.