I was going to ask if there is a pylint-style code analyzer capable of suggesting the use of the :=
operator in places were it might improve the code. However, it looks like such test has been added to the pylint
two years ago -> github PR (merged).
Anyway I never saw such suggestion, not even for this example like in the linked PR:
x = 2
if x:
print(x)
# -----
# if (x := 2):
# print(x)
# -----
This feature is available since Python 3.8. (I'm using recent Python and pylint versions.) I though I have to enable it somehow, but the help says:
--py-version <py_version> Minimum Python version to use for version dependent checks. Will default to the version used to run pylint.
What is wrong? Why there is no consider-using-assignment-expr
from pylint
?
elif
branches (to avoid nesting and extensive computations) orwhile
(when it's used likefor
but on something that is not iterable).pylint
behavior (namely - why doesn't the linter fail a rule check on given code), which can be answered as is based on facts and code history.+=
), not assignment expressions. Much as I dislike it,consider-using-assignment-expr
is enabled by default in the codestyle extension, as long as you enable the extension itself.consider-using-augmented-assignment
is disabled by default even if you enable the extension. Strangely, this appears to have nothing to do with the fact that changinga = a+b
toa += b
can change the meaning of the line. Nobody brought that fact up in any of the discussion I can see surrounding the change.