实例化Coroutine Generator时,Pylint标记分配是从返回的。

发布于 2025-02-08 14:29:35 字数 999 浏览 0 评论 0原文

我收到Pylint错误,我不明白。我定义了一个生成器(我用作coroutine),即即使分配分配返回生成器而不是返回值:

E1111: Assigning result of a function call, where the function has no return (assignment-from-no-return)

最小降低示例:

def test():
    print((yield))

a = test()
print(a)
next(a)
try:
    a.send("hello, world")
except StopIteration:
    pass

行 :行:行, pylint 抱怨的地方,即使也是如此。 a = test()是标记错误的位置。由于test()是一个发电机的实例化,而不是函数调用,因此我不明白错误。我尝试将(收益)更改为(none none)(收益true)无用。添加返回true给我一个no-ember错误。

执行:

$ python3 /tmp/test.py
<generator object test at 0x7f9de7801b30>
hello, world

是的,我可以禁用它,但是我更喜欢在禁用它之前完全理解它。

如果我将其转换为传统发电机,它不会抱怨:

def test():
    yield True

a = test()
print(a)
next(a)

I am getting a pylint error I don't understand. I defined a generator (which I am using as a coroutine), and where I instantiate the generator, pylint complains, even though the assignment returns a generator and not a return value:

E1111: Assigning result of a function call, where the function has no return (assignment-from-no-return)

Minimum reducible example:

def test():
    print((yield))

a = test()
print(a)
next(a)
try:
    a.send("hello, world")
except StopIteration:
    pass

The line a = test() is where the error is flagged. Since test() is a generator instantiation, not a function call, I don't understand the error. I tried changing (yield) to (yield None) or (yield True) to no avail. Adding a return True gives me a no-member error.

Execution:

$ python3 /tmp/test.py
<generator object test at 0x7f9de7801b30>
hello, world

Yes, I can disable it, but I prefer to fully understand it before I disable it.

If I convert it to a traditional generator, it does not complain:

def test():
    yield True

a = test()
print(a)
next(a)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文