Skip to content

Apparent False Positive With Dict Update Method #5849

Description

@lvfrazao

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.
fromtypingimportUnion, DictclassA(object):
def__init__(self, user: Dict[str, Union[str, bool, list, Dict[str, Union[bool, list]]]]):
d1= {
'a': user['name'],
}
d2= {
'c': 3,
}
self.d3= {**d1, **d2}
print(A({'name': 'victor'}).d3)
  • What is the actual behavior/output?
lvrf@LAPTOP-D0LJCLS0:~$ mypy sample_mypy_code.py
sample_mypy_code.py:13: error: Argument 1 to "update" of "dict" has incompatible type"Dict[str, int]"; expected "Mapping[str, Union[str, bool, List[Any], Dict[str, Union[bool, List[Any]]]]]"

Note, this behaviour only occurs when the 'user' input is typed.

  • What is the behavior/output you expect?

I would expect there to be no error for this operation.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

mypy version 0.641

  • What are the mypy flags you are using? (For example --strict-optional)

No flags

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions