Search code examples
pythonpython-3.xpycharmtypingtype-hinting

Typing problems in PyCharm


I have the following function:

def clock(dimS: Tuple[int] =(0)) -> Generator[Tuple[int], None, None]:
    """ Produce coordinates """
    itr = 0
    dim = len(dimS)
    maxItr = np.prod(dimS)

    if (dim < 1):
        raise ValueError(
            'function clock expected positive number of dimensions, received: 0'
        )

    while itr < maxItr:
        c = []
        ind = itr

        # build coordinate
        for i in range(dim):
            s = dimS[dim - i - 1]
            g = ind % s
            ind //= s       # update
            c.append(g)

        itr += 1
        yield tuple(reversed(c))

I'm using PyCharm to edit my code (love it). It tells me the type Generator[Tuple[int], None, None] was expected, but instead got no return ? When I change it to Generator[Tuple[int], None, bool] and add a line return True, as in the documentation example, the IDE highlights True and tells me Expected Generator[Tuple[int], None, bool], got bool. How do I fix this?


Here's a simpler example that does the same thing:

from typing import Generator

def foo(i: int =0) -> Generator[int, None, None]:
    while True:
        i += 1
        yield i

It highlights Generator[int, None, None] and tells me got no return.


Solution

  • mypy accepts your sample input without issue. This is an issue with PyCharm from what it seems.

    Scaning through the bug tracker for JetBrains, I found an issue that deals with what you're experiencing, see Return type hint messes up with 'Generator' type.