Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reporting superfluous-parens after an is not operator #6451

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ Release date: TBA

Closes #4324

* Avoid reporting ``superfluous-parens`` on expressions using the ``is not`` operator.

Closes #5930

Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
* Added the ``super-without-brackets`` checker, raised when a super call is missing its brackets.

Closes #4008
Expand Down
10 changes: 9 additions & 1 deletion pylint/checkers/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,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 @@ -411,7 +419,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)