Skip to content

Commit 7cce069

Browse files
authored
WPS349 highlighting (#3437)
1 parent 7b83f78 commit 7cce069

3 files changed

Lines changed: 80 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Due to PEP-695, it's now allowed to use `[]` in the decorator only for `python3.
4040
def some_function(): ...
4141
```
4242

43+
- Improves `WPS349` highlighting, #3437
44+
45+
4346
## 1.1.0
4447

4548
### Command line utility

tests/test_visitors/test_ast/test_subscripts/test_redundant_subscripts.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@
1212
'expression',
1313
[
1414
'0:7',
15-
'0:7:1',
1615
'None:7',
1716
'3:None',
1817
'3:None:2',
1918
'3:7:None',
2019
'3:7:1',
2120
],
2221
)
23-
def test_redundant_subscript(
22+
def test_one_redundant_subscript(
2423
assert_errors,
2524
parse_ast_tree,
2625
expression,
@@ -35,6 +34,70 @@ def test_redundant_subscript(
3534
assert_errors(visitor, [RedundantSubscriptViolation])
3635

3736

37+
@pytest.mark.parametrize(
38+
'expression',
39+
[
40+
'0:7:1',
41+
'0:7:None',
42+
'0:None',
43+
'None:7:None',
44+
'None:None',
45+
'3:None:1',
46+
':None:None',
47+
],
48+
)
49+
def test_two_redundant_subscript(
50+
assert_errors,
51+
parse_ast_tree,
52+
expression,
53+
default_options,
54+
):
55+
"""Testing that redundant subscripts are forbidden."""
56+
tree = parse_ast_tree(usage_template.format(expression))
57+
58+
visitor = SubscriptVisitor(default_options, tree=tree)
59+
visitor.run()
60+
61+
assert_errors(
62+
visitor,
63+
[
64+
RedundantSubscriptViolation,
65+
RedundantSubscriptViolation,
66+
],
67+
)
68+
69+
70+
@pytest.mark.parametrize(
71+
'expression',
72+
[
73+
'0:None:1',
74+
'None:None:1',
75+
'None:None:None',
76+
'0:None:None',
77+
],
78+
)
79+
def test_three_redundant_subscript(
80+
assert_errors,
81+
parse_ast_tree,
82+
expression,
83+
default_options,
84+
):
85+
"""Testing that redundant subscripts are forbidden."""
86+
tree = parse_ast_tree(usage_template.format(expression))
87+
88+
visitor = SubscriptVisitor(default_options, tree=tree)
89+
visitor.run()
90+
91+
assert_errors(
92+
visitor,
93+
[
94+
RedundantSubscriptViolation,
95+
RedundantSubscriptViolation,
96+
RedundantSubscriptViolation,
97+
],
98+
)
99+
100+
38101
@pytest.mark.parametrize(
39102
'expression',
40103
[

wemake_python_styleguide/visitors/ast/subscripts.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,30 @@ def _check_redundant_subscript(self, node: ast.Subscript) -> None:
5757
if not isinstance(node.slice, ast.Slice):
5858
return
5959

60+
indexes: list[ast.expr | None] = []
6061
lower_ok = node.slice.lower is None or (
6162
not self._is_zero(node.slice.lower)
6263
and not self._is_none(node.slice.lower)
6364
)
64-
6565
upper_ok = node.slice.upper is None or not self._is_none(
6666
node.slice.upper,
6767
)
68-
6968
step_ok = node.slice.step is None or (
7069
not self._is_one(node.slice.step)
7170
and not self._is_none(node.slice.step)
7271
)
7372

74-
if not (lower_ok and upper_ok and step_ok):
75-
self.add_violation(
76-
consistency.RedundantSubscriptViolation(
77-
node,
78-
),
79-
)
73+
if not lower_ok:
74+
indexes.append(node.slice.lower)
75+
76+
if not upper_ok:
77+
indexes.append(node.slice.upper)
78+
79+
if not step_ok:
80+
indexes.append(node.slice.step)
81+
82+
for index in indexes:
83+
self.add_violation(consistency.RedundantSubscriptViolation(index))
8084

8185
def _check_slice_assignment(self, node: ast.Subscript) -> None:
8286
if not isinstance(node.ctx, ast.Store):

0 commit comments

Comments
 (0)