-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false positives for
superfluous-parens
(#4784)
* Split functional tests for ``superfluous-parents`` * Fix false positives for superfluous-parens This fixes the false positives identified in #2818, #3249, #3608 & #4346 All false positives reported fell under keywords before walrus operator or if-keyword within generators/comprehension. This closes #2818, closes #3429, closes #3608, closes #4346 * Move the superfluous functional tests to functional/s/super Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
7bb5043
commit 85b69c9
Showing
11 changed files
with
168 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Test the superfluous-parens warning.""" | ||
from __future__ import print_function | ||
# pylint: disable=unneeded-not, unnecessary-comprehension, missing-function-docstring, invalid-name, fixme | ||
A = 3 | ||
if (A == 5): # [superfluous-parens] | ||
pass | ||
if not (A == 5): # [superfluous-parens] | ||
pass | ||
if not (3 or 5): | ||
pass | ||
for (x) in (1, 2, 3): # [superfluous-parens] | ||
print(x) | ||
if (1) in (1, 2, 3): # [superfluous-parens] | ||
pass | ||
if (1, 2) in (1, 2, 3): | ||
pass | ||
DICT = {'a': 1, 'b': 2} | ||
del(DICT['b']) # [superfluous-parens] | ||
del DICT['a'] | ||
|
||
B = [x for x in ((3, 4))] # [superfluous-parens] | ||
C = [x for x in ((3, 4) if 1 > 0 else (5, 6))] | ||
D = [x for x in ((3, 4) if 1 > 0 else ((5, 6)))] # [superfluous-parens] | ||
E = [x for x in ((3, 4) if 1 > 0 else ((((5, 6)))))] # [superfluous-parens] | ||
|
||
# Test assertions | ||
F = "Version 1.0" | ||
G = "1.0" | ||
assert "Version " + G in F | ||
assert ("Version " + G) in F # [superfluous-parens] | ||
|
||
# Test assignment | ||
H = 2 + (5 * 3) | ||
NUMS_LIST = ['1', '2', '3'] | ||
NUMS_SET = {'1', '2', '3'} | ||
NUMS_DICT = {'1': 1, '2': 2, '3': 3} | ||
|
||
# Test functions | ||
def function_A(): | ||
return (x for x in ((3, 4))) # [superfluous-parens] | ||
|
||
# TODO: Test string combinations, see https://github.com/PyCQA/pylint/issues/4792 | ||
# Lines 45, 46 & 47 should raise the superfluous-parens message | ||
I = "TestString" | ||
J = ("Test " + "String") | ||
K = ("Test " + "String") in I | ||
assert "" + ("Version " + "String") in I |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
superfluous-parens:5:0::Unnecessary parens after 'if' keyword:HIGH | ||
superfluous-parens:7:0::Unnecessary parens after 'not' keyword:HIGH | ||
superfluous-parens:11:0::Unnecessary parens after 'for' keyword:HIGH | ||
superfluous-parens:13:0::Unnecessary parens after 'if' keyword:HIGH | ||
superfluous-parens:18:0::Unnecessary parens after 'del' keyword:HIGH | ||
superfluous-parens:21:0::Unnecessary parens after 'in' keyword:HIGH | ||
superfluous-parens:23:0::Unnecessary parens after 'else' keyword:HIGH | ||
superfluous-parens:24:0::Unnecessary parens after 'else' keyword:HIGH | ||
superfluous-parens:30:0::Unnecessary parens after 'assert' keyword:HIGH | ||
superfluous-parens:40:0::Unnecessary parens after 'in' keyword:HIGH |
51 changes: 51 additions & 0 deletions
51
tests/functional/s/super/superfluous_parens_walrus_py38.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
"""Test the superfluous-parens warning with python 3.8 functionality (walrus operator)""" | ||
# pylint: disable=missing-function-docstring, invalid-name, missing-class-docstring, import-error | ||
import numpy | ||
|
||
# Test parens in if statements | ||
if not (x := False): | ||
print(x) | ||
|
||
if not (foo := 5): | ||
pass | ||
|
||
A = 1 | ||
if odd := isinstance(A, int): | ||
pass | ||
|
||
if not ((x := 1)): # [superfluous-parens] | ||
pass | ||
|
||
if ((x := A)): # [superfluous-parens] | ||
pass | ||
|
||
if not ((x := A)): # [superfluous-parens] | ||
pass | ||
|
||
if not ((((x := 1)))): # [superfluous-parens] | ||
pass | ||
|
||
# Test assertions | ||
assert (ret := str(42)) | ||
|
||
# Teast 2D arrays with numpy (since they allow commas within indexing) | ||
ARRAY_2D = numpy.zeros((3, 3), dtype=bool) | ||
E = not (vals := ARRAY_2D[:, :].all()) | ||
F = not (vals := ARRAY_2D[2, 2].all()) | ||
G = not (vals := ARRAY_2D[::].all()) | ||
H = not (vals := ARRAY_2D[2:2].all()) | ||
|
||
# Test yield | ||
class TestYieldClass: | ||
@classmethod | ||
def function_A(cls): | ||
yield (var := 1 + 1) | ||
print(var) | ||
|
||
@classmethod | ||
def function_B(cls): | ||
yield str(1 + 1) | ||
|
||
@classmethod | ||
def function_C(cls): | ||
yield (1 + 1) # [superfluous-parens] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[testoptions] | ||
min_pyver=3.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
superfluous-parens:16:0::Unnecessary parens after 'not' keyword:HIGH | ||
superfluous-parens:19:0::Unnecessary parens after 'if' keyword:HIGH | ||
superfluous-parens:22:0::Unnecessary parens after 'not' keyword:HIGH | ||
superfluous-parens:25:0::Unnecessary parens after 'not' keyword:HIGH | ||
superfluous-parens:51:0::Unnecessary parens after 'yield' keyword:HIGH |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.