question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid consecutive slices

See original GitHub issue

Rule request

Thesis

We should forbid consecutive slices.

Reasoning

Consider this code:

>>> a = [1, 2, 3, 4]
>>> a[1:][:2]
[2, 3]

What it does?

  1. a[1:] is [2, 3, 4]
  2. [2, 3, 4][:2] is [2, 3]

It is much more readable and effective to use

>>> a[1:3]
[2, 3]

Related https://twitter.com/m_ou_se/status/1403426221367627777

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
Edald123commented, Jul 8, 2021

Hi! I actually worked on a solution with Gibran.

So far we have implemented a solution in wemake_python_styleguide/visitors/ast/subscripts.py. In the class SubscriptVisitor we added:

def _check_consecutive(self, node: ast.Subscript) -> None:
        """Check if subscript node has a slice and a subscript"""
        if isinstance(node.slice, ast.Slice) and isinstance(node.value, ast.Subscript):
            self.add_violation(consistency.ForbidConsecutiveSlicesViolation(node))

Our implementation is passing some tests but others not. The tests we created are for:

no_consecutive = """
a = [1, 2, 3, 4]
b = a[1:]
"""

no_consecutive_visit = """
a = [1, 2, 3, 4]
b = a[1:][2]
"""

no_slicing = """
a = [1, 2, 3, 4]
b = a[1]
"""

no_slicing_double = """
a = [1, [5, 6, 7], 3, 4]
b = a[1][3]
"""

no_slicing_triple = """
a = [1, [5, 6, 7, [8, 9, 10]], 3, 4]
b = a[1][3][1]
"""

consecutive_double = """
a = [1, 2, 3, 4]
b = a[1:][2:]
"""

consecutive_triple = """
a = [1, 2, 3, 4, 5, 6]
b = a[1:][2:][:2]
"""

consecutive_plus = """
a = [1, [5, 6, 7, 8, 9, 10], 3, 4]
b = a[1][2:][:4]
"""

no_consecutive_for_slices = """
a = [1, [5, 6, 7, [8, [1, 2, 3, 4], 10]], 3, 4]
for i in a[1][3][1]:
    print(i)
"""

consecutive_for = """
a = [1, [5, 6, 7, [8, [1, 2, 3, 4], 10]], 3, 4]
for i in a[1][:4][1:]:
    print(i)
"""

The tests that are not supposed to raise errors are: no_consecutive, no_consecutive_visit, no_slicing, no_slicing_double, no_slicing_triple, no_consecutive_for_slices. These are working fine.

The tests that are supposed to raise an error (violation) are: consecutive_double, consecutive_triple, consecutive_plus, consecutive_for. Of these tests only consecutive_double is passing since we are expecting the raise of 1 violation and it is raising 1 The other three are failing: consecutive_triple, consecutive_for, and consecutive_plus.

The problem is that these 3 tests are raising 2 errors instead of 1 so they fail: AssertionError: assert 1 == 2 This failure is occurring in tests/test_visitors/conftest.py, in the assert_errors function, in the line assert len(errors) == len(real_errors) Therefore, it seems like the code is working for forbidding 2 consecutive slices, but when there are more it isn’t. We are working on solving this.

0reactions
sobolevncommented, Oct 11, 2021

Oups, sorry! I’ve totally missed it (because it was not linked to this issue). This PR is merged: https://github.com/wemake-services/wemake-python-styleguide/pull/2220

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issuehunt
We should forbid consecutive slices. Reasoning. <!-- Why do you think this is a good idea? Please, include some strong points and considerations....
Read more >
How to disallow consecutive five digits and more using regex?
I have created a sandbox demo. I want to disallow five consecutive numbers/digits. Currently it is disallowing any number. javascript · regex ...
Read more >
Version 0.16 Milestone · GitHub
Forbid consecutive slices good first issue Entrypoint to the project help wanted Extra attention is needed level:starter Good for newcomers rule request ...
Read more >
Guillotine cutting is asymptotically optimal for packing ...
The packing of consecutive squares has become a benchmark problem for general ... for example in scheduling, where rotation is forbidden.
Read more >
Command Line Options — x265 documentation - Read the Docs
Disable periodic progress reports from the CLI ... Type Slice type of the frame. ... Encode each incoming frame as multiple parallel slices...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found