pylint icon indicating copy to clipboard operation
pylint copied to clipboard

Make ``superfluous-parens`` consider string combinations

Open DanielNoord opened this issue 2 years ago • 4 comments

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

DanielNoord avatar Aug 03 '21 12:08 DanielNoord

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.

RaenonX avatar Aug 05 '21 21:08 RaenonX

This should be fixed in 2.10 (that is not released yet) @RaenonX

Pierre-Sassoulas avatar Aug 06 '21 05:08 Pierre-Sassoulas

This should be fixed in 2.10 (that is not released yet) @RaenonX

OK, will wait for that. Thanks!

RaenonX avatar Aug 06 '21 05:08 RaenonX

Still seeing this

emmeowzing avatar Sep 09 '22 17:09 emmeowzing

I'll work on this issue

clavedeluna avatar Nov 11 '22 15:11 clavedeluna