pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Consider adding rule to suggest using the operator library

Open NeilGirdhar opened this issue 3 years ago • 11 comments
trafficstars

Given code like:

    mm = jax.tree_map(lambda x, y: x + y, f, f)

Consider having a pylint rule that suggests replacing it with:

    mm = jax.tree_map(operator.add, f, f)

Motivation: the Google style guide.

NeilGirdhar avatar May 03 '22 23:05 NeilGirdhar

It seems it's very specific to jax and could be something for a pylint-jax external plugin instead of being in pylint main repository, what do you think ?

Pierre-Sassoulas avatar May 04 '22 09:05 Pierre-Sassoulas

It seems it's very specific to jax

It's not specific to Jax at all. That was just an example I gave. I'm sure you can find many, many code snippets that use lambdas like this.

NeilGirdhar avatar May 04 '22 09:05 NeilGirdhar

It make sense then, thank you :)

Pierre-Sassoulas avatar May 04 '22 09:05 Pierre-Sassoulas

Thanks Pierre 😄

NeilGirdhar avatar May 04 '22 09:05 NeilGirdhar

To help any potential contributor, let's settle on a name now. What about consider-using-operator?

DanielNoord avatar May 04 '22 09:05 DanielNoord

We could also piggy back on unnecessary-lambda-assignment / unnecessary-direct-lambda-call with unnecessary-lambda-for-operator ?

Pierre-Sassoulas avatar May 04 '22 09:05 Pierre-Sassoulas

@NeilGirdhar Is there an actual performance benefit? Or is this just style? If it is just style I think we should go for consider, otherwise unnecessary does indeed make sense!

DanielNoord avatar May 04 '22 09:05 DanielNoord

Is there an actual performance benefit? Or is this just style?

It's motivated by style (I haven't tested the performance). I got the idea from the Googe style guide, which I linked at the top. I usually write the lambda out of laziness, but I agree with the style guide that operator.add is easier to read.

NeilGirdhar avatar May 04 '22 09:05 NeilGirdhar

Let's use consider-using-operator then.

Pierre-Sassoulas avatar Jul 18 '22 07:07 Pierre-Sassoulas

A related suggestion I just learned about is contextlib.suppress, which replaces

try:
    Y # ...
except X:
    pass

with

with contextlib.suppress(X):
    Y # ...

Should this be a separate issue?

NeilGirdhar avatar Jul 18 '22 09:07 NeilGirdhar

Yes, could you open another issue @NeilGirdhar please ?

Pierre-Sassoulas avatar Jul 18 '22 10:07 Pierre-Sassoulas