From feca907976aea32abc9910c1b6800ff58dd65cc4 Mon Sep 17 00:00:00 2001 From: Tim Martin Date: Wed, 27 Apr 2022 19:37:41 +0100 Subject: [PATCH] Avoid reporting superfluous-parens after an ``is not`` operator (#6451) --- ChangeLog | 4 ++++ doc/whatsnew/2.13.rst | 4 ++++ pylint/checkers/format.py | 10 +++++++++- tests/functional/s/superfluous_parens.py | 7 +++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index ee6e4b3f95..6fcce762ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 bd002d0216..c28b94023a 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 70c56a621a..4bf174df2d 100644 --- a/pylint/checkers/format.py +++ b/pylint/checkers/format.py @@ -339,6 +339,14 @@ def _check_keyword_parentheses( 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 @@ def _check_keyword_parentheses( 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 ee835f6d08..5c91c99ee0 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)