Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Due to PEP-695, it's now allowed to use `[]` in the decorator only for `python3.
def some_function(): ...
```

- Improves `WPS349` highlighting, #3437


## 1.1.0

### Command line utility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
'expression',
[
'0:7',
'0:7:1',
'None:7',
'3:None',
'3:None:2',
'3:7:None',
'3:7:1',
],
)
def test_redundant_subscript(
def test_one_redundant_subscript(
assert_errors,
parse_ast_tree,
expression,
Expand All @@ -35,6 +34,70 @@ def test_redundant_subscript(
assert_errors(visitor, [RedundantSubscriptViolation])


@pytest.mark.parametrize(
'expression',
[
'0:7:1',
'0:7:None',
'0:None',
'None:7:None',
'None:None',
'3:None:1',
':None:None',
],
)
def test_two_redundant_subscript(
assert_errors,
parse_ast_tree,
expression,
default_options,
):
"""Testing that redundant subscripts are forbidden."""
tree = parse_ast_tree(usage_template.format(expression))

visitor = SubscriptVisitor(default_options, tree=tree)
visitor.run()

assert_errors(
visitor,
[
RedundantSubscriptViolation,
RedundantSubscriptViolation,
],
)


@pytest.mark.parametrize(
'expression',
[
'0:None:1',
'None:None:1',
'None:None:None',
'0:None:None',
],
)
def test_three_redundant_subscript(
assert_errors,
parse_ast_tree,
expression,
default_options,
):
"""Testing that redundant subscripts are forbidden."""
tree = parse_ast_tree(usage_template.format(expression))

visitor = SubscriptVisitor(default_options, tree=tree)
visitor.run()

assert_errors(
visitor,
[
RedundantSubscriptViolation,
RedundantSubscriptViolation,
RedundantSubscriptViolation,
],
)


@pytest.mark.parametrize(
'expression',
[
Expand Down
20 changes: 12 additions & 8 deletions wemake_python_styleguide/visitors/ast/subscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,30 @@ def _check_redundant_subscript(self, node: ast.Subscript) -> None:
if not isinstance(node.slice, ast.Slice):
return

indexes: list[ast.expr | None] = []
lower_ok = node.slice.lower is None or (
not self._is_zero(node.slice.lower)
and not self._is_none(node.slice.lower)
)

upper_ok = node.slice.upper is None or not self._is_none(
node.slice.upper,
)

step_ok = node.slice.step is None or (
not self._is_one(node.slice.step)
and not self._is_none(node.slice.step)
)

if not (lower_ok and upper_ok and step_ok):
self.add_violation(
consistency.RedundantSubscriptViolation(
node,
),
)
if not lower_ok:
indexes.append(node.slice.lower)

if not upper_ok:
indexes.append(node.slice.upper)

if not step_ok:
indexes.append(node.slice.step)

for index in indexes:
self.add_violation(consistency.RedundantSubscriptViolation(index))

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