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
时,如 文档 示例,IDE 突出显示 True
并告诉我 Expected Generator[Tuple[int], None, bool], got bool
.我该如何解决这个问题?
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
它突出显示 Generator[int, None, None]
并告诉我 没有返回
.
It highlights Generator[int, None, None]
and tells me got no return
.
推荐答案
mypy
可以毫无问题地接受您的示例输入.从表面上看,这是 PyCharm 的一个问题.
mypy
accepts your sample input without issue. This is an issue with PyCharm from what it seems.
扫描 JetBrains 的错误跟踪器,我发现了一个与您遇到的问题有关的问题,请参阅 返回类型提示与生成器"类型混淆.
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.
这篇关于PyCharm 中的打字问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!