Pylint,如何禁用有条件的规则
我不想禁用整个项目的规则,而是针对已知的场景。就像:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就目前的代码而言,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 ofx = 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.