Pylint,如何禁用有条件的规则

发布于 2025-01-11 20:55:15 字数 811 浏览 3 评论 0原文

我不想禁用整个项目的规则,而是针对已知的场景。就像:

class A:
   def __init__(self, creator: Callback[[], B])

大多数时候我使用

x = A(lambda: Bx())
y = A(lambda: By())

但它总是触发unnecessary-lambda,但我无法删除这个lambda。由于这种情况发生了很多,我只想在这种情况下禁用该规则..

类似:

disable=unnecessary-lambda when-class=A

或者作为注释...

# pylint: disable=unnecessary-lambda propagate=True
class A:
    def __init__(self, creator: Callback[[], B])

有某种方法可以做到这一点吗?

附: 案件的发生是这样的:

class Bx(B):
    factory = A(lambda: Bx())

I don't want to disable the rule for entire project, but for a known scenario. Like:

class A:
   def __init__(self, creator: Callback[[], B])

Most of time I use

x = A(lambda: Bx())
y = A(lambda: By())

But it always trigger unnecessary-lambda, but I can't remove this lambda. Since this case happens a lot, I just want to disable that rule under that condition..

Something like:

disable=unnecessary-lambda when-class=A

Or as annotation...

# pylint: disable=unnecessary-lambda propagate=True
class A:
    def __init__(self, creator: Callback[[], B])

There is some sort way to do that?

PS:
The case happens like that:

class Bx(B):
    factory = A(lambda: Bx())

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2025-01-18 20:55:15

就目前的代码而言,lambda 确实是不必要的。您可以使用 x = A(Bx) 代替 x = A(lambda: Bx())。这将消除警告。

如果这不能解决您的问题,我将建议您参考 https ://pylint.pycqa.org/en/latest/user_guide/message-control.html 提供了在给定范围内禁用 pylint 警告的各种方法。

The lambda really is unnecessary in the code as it stands. You can use x = A(Bx) instead of x = A(lambda: Bx()). This would get rid of the warning.

In case this doesn't solve your problem, I will refer you to https://pylint.pycqa.org/en/latest/user_guide/message-control.html which gives various ways to disable a pylint warning in e.g. a given scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文