0

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(lamda: Bx())
y = A(lamda: 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())
DIG
  • 1
  • 1

1 Answers1

1

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.

Andrew McClement
  • 1,171
  • 5
  • 14
  • 1
    Well, it isn't a problem if the `x = A(Bx)` won't be a static instance inside `Bx`. At that point `Bx` does not exists and that is why the lambda is required. As a workaround, I can instantiate `Bx.x = A(Bx)` after declaring `Bx`. That solve the problem. But.. I will break the pattern in code. Sometimes it is just `A(lambda: Bx())`, but there are others cases where we use `A(lambda: Bz(True))`.. Mixing pattern does not looks good. But also writing `# pylint: disable=...` in each useless lambda for class A, will be a pain – DIG Mar 07 '22 at 01:42
  • I see, when wanting to do this inside the class, obviously it isn't fully resolved. Perhaps disable the warning in the configuration file to get essentially a global disable? Sorry, I don't have a good solution. – Andrew McClement Mar 07 '22 at 11:16