pylint
pylint copied to clipboard
Make ``superfluous-parens`` consider string combinations
Current problem
Although #4784 fixes some problems with superfluous-parens
it still does not consider the following cases:
Z = "TestString"
X = ("Test " + "String") # Bad
Y = ("Test " + "String") in Z # Bad
assert "" + ("Version " + "String") in Z # Bad
This is because the current checker focuses on unnecessary parens after keywords (if
, else
, etc.) and only if the (
immediately follows the keyword. See
if tokens[start + 1].string != "(":
return
on line 373
in format.py
and the fact that we only run when Keywords are present.
Ideally, the checker would also consider string combinations and seeing if parens are necessary there.
Desired solution
Raise superfluous-parens
message on the test cases described above.
Additional context
No response
Not sure if this is exactly the same as the thing mentioned above, so I am leaving it here first instead of creating a new issue.
The following code still produces superfluous-parens
false-positive.
if not (a := dummy_func())
return True
Here's the error message:
C0325: Unnecessary parens after 'not' keyword (superfluous-parens)
Versions:
pylint 2.9.6
astroid 2.6.6
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)]
Syntax Reasoning
I can't use
if a := not dummy_func():
return True
instead of
if not (a := dummy_func()):
return True
because the first one only works if dummy_func()
is returning bool
. However, dummy_func
is either returning None
or some object, so I have to use the second one.
This should be fixed in 2.10 (that is not released yet) @RaenonX
This should be fixed in 2.10 (that is not released yet) @RaenonX
OK, will wait for that. Thanks!
Still seeing this
I'll work on this issue