-
-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forbid consecutive slices. #2220
Changes from all commits
7885f8d
11c8687
160c3b4
4305434
4cc5c3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -250,6 +250,7 @@ | |
'WPS468': 2, | ||
'WPS469': 1, | ||
'WPS470': 1, | ||
'WPS471': 1, | ||
|
||
'WPS500': 1, | ||
'WPS501': 1, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import pytest | ||
|
||
from wemake_python_styleguide.violations.best_practices import ( | ||
ConsecutiveSlicesViolation, | ||
) | ||
from wemake_python_styleguide.visitors.ast.subscripts import SubscriptVisitor | ||
|
||
usage_template = 'constant[{0}]' | ||
|
||
|
||
@pytest.mark.parametrize('expression', [ | ||
'a[1:][:3]', | ||
'a[1:3][3:]', | ||
'a[:][:]', | ||
'a[1:3][3:][2:]', | ||
'a[:c[d]][:5]["hello"]', | ||
]) | ||
def test_forbidden_consecutive_slices( | ||
assert_errors, | ||
parse_ast_tree, | ||
expression, | ||
default_options, | ||
): | ||
"""Testing that consecutive slices are forbidden.""" | ||
tree = parse_ast_tree(usage_template.format(expression)) | ||
|
||
visitor = SubscriptVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [ConsecutiveSlicesViolation]) | ||
|
||
|
||
@pytest.mark.parametrize('expression', [ | ||
'a[:][:a[3:][4:]]', | ||
'a[:][:1]["hello"][:42][2:]', | ||
]) | ||
def test_forbidden_multiple_consecutive_slices( | ||
assert_errors, | ||
parse_ast_tree, | ||
expression, | ||
default_options, | ||
): | ||
"""Testing that consecutive slices are forbidden.""" | ||
tree = parse_ast_tree(usage_template.format(expression)) | ||
|
||
visitor = SubscriptVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [ | ||
ConsecutiveSlicesViolation, | ||
ConsecutiveSlicesViolation, | ||
]) | ||
|
||
|
||
@pytest.mark.parametrize('expression', [ | ||
'a', | ||
'a[3:7]', | ||
'a[4][:5]', | ||
'a["hello"][4:]', | ||
'a[1:]["tram"][17:]', | ||
'a[a[:1]][a[1:2]][a[2:3]]', | ||
'a[1][2][3]', | ||
]) | ||
def test_nonconsecutive_slices( | ||
assert_errors, | ||
parse_ast_tree, | ||
expression, | ||
default_options, | ||
): | ||
"""Testing that non-consecutive slices are allowed.""" | ||
tree = parse_ast_tree(usage_template.format(expression)) | ||
|
||
visitor = SubscriptVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import ast | ||
from typing import Set | ||
|
||
from typing_extensions import final | ||
|
||
|
@@ -17,12 +18,44 @@ | |
class SubscriptVisitor(base.BaseNodeVisitor): | ||
"""Checks subscripts used in the code.""" | ||
|
||
_marked_slices: Set[ast.Subscript] = set() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it important to have this as class var? Maybe instance one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has to be always present on an instance so that the |
||
|
||
def visit_Subscript(self, node: ast.Subscript) -> None: | ||
"""Visits subscript.""" | ||
self._check_redundant_subscript(node) | ||
|
||
if node in self._marked_slices: | ||
# Violation for that specific slice was already triggered earlier | ||
self._marked_slices.remove(node) | ||
else: | ||
self._check_consecutive_slices(node) | ||
|
||
self._check_slice_assignment(node) | ||
self.generic_visit(node) | ||
|
||
def _check_consecutive_slices(self, node: ast.Subscript): | ||
if not isinstance(node.slice, ast.Slice): | ||
return | ||
|
||
if not isinstance(node.value, ast.Subscript): | ||
return | ||
|
||
if not isinstance(node.value.slice, ast.Slice): | ||
return | ||
|
||
self.add_violation(best_practices.ConsecutiveSlicesViolation(node)) | ||
|
||
# We do not want to trigger duplicate violations for each subsequent | ||
# subscript in the same chain, so we proactively mark them so | ||
# that visitor can skip them in the future. | ||
current_sub = node.value | ||
while isinstance(current_sub.value, ast.Subscript): | ||
if not isinstance(current_sub.value.slice, ast.Slice): | ||
break | ||
|
||
self._marked_slices.add(current_sub) | ||
current_sub = current_sub.value | ||
|
||
def _check_redundant_subscript(self, node: ast.Subscript) -> None: | ||
if not isinstance(node.slice, ast.Slice): | ||
return | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also test:
a[1][2][3]
a[a[0:1]][a[1:2]][a[2:3]]