diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-03-26 18:31:49 -0400 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-03-27 14:39:30 +0200 |
commit | b25859c4a56ccce61087f7a1270f40deaed68169 (patch) | |
tree | a0ead530f94459affec223db55b64e74c15a5120 | |
parent | 0e1ca11ac65cbe5a65437518fca1e25f1ad0e48e (diff) | |
download | pylint-git-b25859c4a56ccce61087f7a1270f40deaed68169.tar.gz |
Fix false positive for `superfluous-parens` for `return (a or b) in iterable` (#5964)
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | doc/whatsnew/2.13.rst | 5 | ||||
-rw-r--r-- | pylint/checkers/format.py | 23 | ||||
-rw-r--r-- | tests/functional/s/superfluous_parens.py | 5 |
4 files changed, 23 insertions, 14 deletions
@@ -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? 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 |