Strange behaviour with type-hinting in PyCharm

Say I have a function, do_something:

from typing import *

def do_something(argument: Sequence[Tuple[int, str]]):
    pass

Say I also have a dictionary, D, the keys of which are solely ints, and the values of which are solely strings:

D = {1: 'a', 2: 'b', 3: 'c'}

In PyCharm, this will pass the type-checker with flying colours:

do_something(
    ((1, 'a'), (2, 'b'), (3, 'c'))
)

But this, according to PyCharm, fails the type-checker, despite being identical in what it produces:

do_something(tuple(D.items()))

Is this expected behaviour — am I missing something here? — or is this a bug with PyCharm’s type-checker?

Answer

This is a bug in PyCharm. There are many similar bugs, e.g. this, this, this.

Leave a Reply

Your email address will not be published. Required fields are marked *