DIG
DIG

Reputation: 1

Pylint, how to disable rule with condition

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

Upvotes: 0

Views: 1101

Answers (1)

Andrew McClement
Andrew McClement

Reputation: 1397

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.

Upvotes: 1

Related Questions