Skip to content

Commit

Permalink
Fix false positive for superfluous-parens for `return (a or b) in i…
Browse files Browse the repository at this point in the history
…terable` (#5964)

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
jacobtylerwalls and Pierre-Sassoulas committed Mar 27, 2022
1 parent 0e1ca11 commit b25859c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ What's New in Pylint 2.13.2?
============================
Release date: TBA

* Fix false positive for ``superfluous-parens`` for patterns like
"return (a or b) in iterable".

Closes #5803


What's New in Pylint 2.13.1?
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,8 @@ Other Changes
the variable being raised.

Closes #2793

* Fix false positive for ``superfluous-parens`` for patterns like
"return (a or b) in iterable".

Closes #5803
23 changes: 10 additions & 13 deletions pylint/checkers/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ def new_line(self, tokens, line_end, line_start):
def process_module(self, _node: nodes.Module) -> None:
pass

# pylint: disable-next=too-many-return-statements
def _check_keyword_parentheses(
self, tokens: List[tokenize.TokenInfo], start: int
) -> None:
Expand Down Expand Up @@ -382,19 +383,15 @@ def _check_keyword_parentheses(
# The empty tuple () is always accepted.
if i == start + 2:
return
if keyword_token == "not":
if not found_and_or:
self.add_message(
"superfluous-parens", line=line_num, args=keyword_token
)
elif keyword_token in {"return", "yield"}:
self.add_message(
"superfluous-parens", line=line_num, args=keyword_token
)
elif not found_and_or and keyword_token != "in":
self.add_message(
"superfluous-parens", line=line_num, args=keyword_token
)
if found_and_or:
return
if keyword_token == "in":
# This special case was added in https://github.com/PyCQA/pylint/pull/4948
# but it could be removed in the future. Avoid churn for now.
return
self.add_message(
"superfluous-parens", line=line_num, args=keyword_token
)
return
elif depth == 1:
# This is a tuple, which is always acceptable.
Expand Down
5 changes: 4 additions & 1 deletion tests/functional/s/superfluous_parens.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ def function_A():
def function_B(var):
return (var.startswith(('A', 'B', 'C')) or var == 'D')

def function_C(first, second):
return (first or second) in (0, 1)

# TODO: Test string combinations, see https://github.com/PyCQA/pylint/issues/4792
# Lines 45, 46 & 47 should raise the superfluous-parens message
# The lines with "+" should raise the superfluous-parens message
J = "TestString"
K = ("Test " + "String")
L = ("Test " + "String") in I
Expand Down

0 comments on commit b25859c

Please sign in to comment.