Skip to content

Commit

Permalink
Avoid reporting superfluous-parens after an is not operator (#6451)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmartin authored and Pierre-Sassoulas committed May 2, 2022
1 parent 7022d23 commit feca907
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
============================
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``

Expand Down
10 changes: 9 additions & 1 deletion pylint/checkers/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:]):
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/s/superfluous_parens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit feca907

Please sign in to comment.