Skip to content

False positive when iterating through a TypedDict in mypy 0.650+ #6082

@michael-k

Description

@michael-k

I have a TypedDict whose values are all dicts. When iterating through the TypedDict with .items() or .values(), the values' type information is lost. mypy assumes type object for the values instead of dict (or more generally a Union of all types used as value). See below for details.

This behavior is new in mypy 0.650 and I believe it was introduced by PR #5933 (related issue: #5927).

Are you reporting a bug, or opening a feature request?

A bug.

Please insert below the code you are checking with mypy.

from mypy_extensions import TypedDict class MyDict(TypedDict): foo: dict bar: dict my_dict: MyDict = {"foo": {}, "bar": {}} for key, value in my_dict.items(): print(value.keys()) # line 12 print(value.values()) print(value.items()) for value in my_dict.values(): print(value.keys()) # line 17 print(value.values()) print(value.items())

What is the actual behavior/output?

tmp.py:12: error: "object" has no attribute "keys" tmp.py:13: error: "object" has no attribute "values" tmp.py:14: error: "object" has no attribute "items" tmp.py:17: error: "object" has no attribute "keys" tmp.py:18: error: "object" has no attribute "values" tmp.py:19: error: "object" has no attribute "items" 

What is the behavior/output you expect?

No errors.

What are the versions of mypy and Python you are using?

  • mypy 0.650 and 0.660+dev.cf253192be8f6ded0bd64025697d54e416a9a7fc
  • Python 3.6.7

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions