PyCharm 中的输入问题

Typing problems in PyCharm

我有以下功能:

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))

我正在使用 PyCharm 来编辑我的代码(很喜欢)。它告诉我类型 Generator[Tuple[int], None, None] 是预期的,而是 got no return ?当我将其更改为 Generator[Tuple[int], None, bool] 并添加一行 return True 时,如 the documentation 示例中所示,IDE 突出显示 True 并告诉我 Expected Generator[Tuple[int], None, bool], got bool。我该如何解决这个问题?


这是一个做同样事情的更简单的例子:

from typing import Generator

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

它突出显示 Generator[int, None, None] 并告诉我 got no return

mypy 毫无问题地接受您的样本输入。从表面上看,这是 PyCharm 的问题。

浏览 JetBrains 的错误跟踪器,我发现了一个与您遇到的问题相关的问题,请参阅 Return type hint messes up with 'Generator' type