pylint
pylint copied to clipboard
Consider adding rule to suggest using the operator library
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.
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 ?
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.
It make sense then, thank you :)
Thanks Pierre 😄
To help any potential contributor, let's settle on a name now. What about consider-using-operator?
We could also piggy back on unnecessary-lambda-assignment / unnecessary-direct-lambda-call with unnecessary-lambda-for-operator ?
@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!
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.
Let's use consider-using-operator then.
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?
Yes, could you open another issue @NeilGirdhar please ?