Skip to content
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

Merged
merged 5 commits into from
Oct 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Semantic versioning in our case means:
where plain version should be used
- Added `RedundantEnumerateViolation` #1825
- Adds `RaiseFromItselfViolation` #2133
- Adds `ConsecutiveSlicesViolation` #2064
- Adds `KwargsUnpackingInClassDefinitionViolation` #1714

### Bugfixes
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/noqa/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,6 @@ def bare_raise_function():

class TestClass(object, **{}): # noqa: WPS470
"""Docs."""


secondary_slice = items[1:][:3] # noqa: WPS471
1 change: 1 addition & 0 deletions tests/test_checker/test_noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
'WPS468': 2,
'WPS469': 1,
'WPS470': 1,
'WPS471': 1,

'WPS500': 1,
'WPS501': 1,
Expand Down
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:]',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also test:

  1. a[1][2][3]
  2. a[a[0:1]][a[1:2]][a[2:3]]

'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, [])
30 changes: 30 additions & 0 deletions wemake_python_styleguide/violations/best_practices.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
RedundantEnumerateViolation
RaiseFromItselfViolation
KwargsUnpackingInClassDefinitionViolation
ConsecutiveSlicesViolation

Best practices
--------------
Expand Down Expand Up @@ -162,6 +163,7 @@
.. autoclass:: RedundantEnumerateViolation
.. autoclass:: RaiseFromItselfViolation
.. autoclass:: KwargsUnpackingInClassDefinitionViolation
.. autoclass:: ConsecutiveSlicesViolation

"""

Expand Down Expand Up @@ -2706,3 +2708,31 @@ class MyClass(**arguments):

error_template = 'Found kwarg unpacking in class definition'
code = 470


@final
class ConsecutiveSlicesViolation(ASTViolation):
"""
Forbid consecutive slices.

Reasoning:
Consecutive slices reduce readability of the code and obscure
intended meaning of the expression.

Solution:
Compress multiple consecutive slices into a single one.

Example::

# Correct:
my_list[1:3]

# Wrong:
my_list[1:][:2]

.. versionadded:: 0.16.0
lensvol marked this conversation as resolved.
Show resolved Hide resolved

"""

error_template = 'Found consecutive slices'
code = 471
33 changes: 33 additions & 0 deletions wemake_python_styleguide/visitors/ast/subscripts.py
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

Expand All @@ -17,12 +18,44 @@
class SubscriptVisitor(base.BaseNodeVisitor):
"""Checks subscripts used in the code."""

_marked_slices: Set[ast.Subscript] = set()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important to have this as class var? Maybe instance one?

Copy link
Collaborator Author

@lensvol lensvol Oct 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has to be always present on an instance so that the generic_visit method can function even if no consecutive slices were found. Or did I misunderstand your intention?


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
Expand Down