summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rw-r--r--doc/whatsnew/2.13.rst5
-rw-r--r--pylint/checkers/format.py23
-rw-r--r--tests/functional/s/superfluous_parens.py5
4 files changed, 23 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index d713e5d30..956d26634 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -36,6 +36,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?
diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst
index b0bfd1321..2d425d7a4 100644
--- a/doc/whatsnew/2.13.rst
+++ b/doc/whatsnew/2.13.rst
@@ -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
diff --git a/pylint/checkers/format.py b/pylint/checkers/format.py
index fae6085ca..70c56a621 100644
--- a/pylint/checkers/format.py
+++ b/pylint/checkers/format.py
@@ -320,6 +320,7 @@ class FormatChecker(BaseTokenChecker):
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:
@@ -382,19 +383,15 @@ class FormatChecker(BaseTokenChecker):
# 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.
diff --git a/tests/functional/s/superfluous_parens.py b/tests/functional/s/superfluous_parens.py
index 22c4c3ab4..ee835f6d0 100644
--- a/tests/functional/s/superfluous_parens.py
+++ b/tests/functional/s/superfluous_parens.py
@@ -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