Search code examples
pythonpycharmtyping

Why does PyCharm incorrectly guess type for a list of dict_items?


For this piece of code:

from typing import Dict, List, Tuple, Any


def test(d: Dict[int, Any]):
    item_list: List[Tuple[int, Any]] = list(d.items())

PyCharm guesses that item_list will be of type List[int] for some reason:

PyCharm screenshot

Am I missing something or this is quite an easy case of deducing the type and PyCharm is in the wrong here?


Solution

  • Your code is correct. It's a bug in PyCharm. You can track its status on their bug tracker here: Incorrect type inference of dict.items(). Consider voting on it so it will be fixed sooner.