Forbid consecutive slices
See original GitHub issueRule request
Thesis
We should forbid consecutive slices.
Reasoning
Consider this code:
>>> a = [1, 2, 3, 4]
>>> a[1:][:2]
[2, 3]
What it does?
a[1:]
is[2, 3, 4]
[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:
- Created 2 years ago
- Comments:18 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Our implementation is passing some tests but others not. The tests we created are for:
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 lineassert 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.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