diff options
author | Tim Martin <tim@asymptotic.co.uk> | 2022-04-27 19:37:41 +0100 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-05-02 16:38:19 +0200 |
commit | feca907976aea32abc9910c1b6800ff58dd65cc4 (patch) | |
tree | 4aed3afe7f853baae8281f38352afa4b2ea99695 | |
parent | 7022d23376ebf6c3bded2d87400c529113bea5ee (diff) | |
download | pylint-git-feca907976aea32abc9910c1b6800ff58dd65cc4.tar.gz |
Avoid reporting superfluous-parens after an ``is not`` operator (#6451)
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/whatsnew/2.13.rst | 4 | ||||
-rw-r--r-- | pylint/checkers/format.py | 10 | ||||
-rw-r--r-- | tests/functional/s/superfluous_parens.py | 7 |
4 files changed, 24 insertions, 1 deletions
@@ -25,6 +25,10 @@ Release date: TBA Closes #6414 +* Avoid reporting ``superfluous-parens`` on expressions using the ``is not`` operator. + + Closes #5930 + What's New in Pylint 2.13.7? ============================ diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index bd002d021..c28b94023 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -611,6 +611,10 @@ Other Changes Closes #5931 +* Avoid reporting ``superfluous-parens`` on expressions using the ``is not`` operator. + + Closes #5930 + * Fix a crash when linting a file that passes an integer ``mode=`` to ``open`` diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py index 70c56a621..4bf174df2 100644 --- a/pylint/checkers/format.py +++ b/pylint/checkers/format.py @@ -339,6 +339,14 @@ class FormatChecker(BaseTokenChecker): self._bracket_stack.pop() if tokens[start + 1].string != "(": return + if ( + tokens[start].string == "not" + and start > 0 + and tokens[start - 1].string == "is" + ): + # If this is part of an `is not` expression, we have a binary operator + # so the parentheses are not necessarily redundant. + return found_and_or = False contains_walrus_operator = False walrus_operator_depth = 0 @@ -412,7 +420,7 @@ class FormatChecker(BaseTokenChecker): elif token[1] == "for": return # A generator expression can have an 'else' token in it. - # We check the rest of the tokens to see if any problems incur after + # We check the rest of the tokens to see if any problems occur after # the 'else'. elif token[1] == "else": if "(" in (i.string for i in tokens[i:]): diff --git a/tests/functional/s/superfluous_parens.py b/tests/functional/s/superfluous_parens.py index ee835f6d0..5c91c99ee 100644 --- a/tests/functional/s/superfluous_parens.py +++ b/tests/functional/s/superfluous_parens.py @@ -68,3 +68,10 @@ class ClassA: def __iter__(self): return ((k, getattr(self, k)) for k in self.keys) + +if (A == 2) is not (B == 2): + pass + +M = A is not (A <= H) +M = True is not (M == K) +M = True is not (True is not False) |