Repository: wemake-services/wemake-python-styleguide

Issue:
False positive ImplicitEnumerateViolation on range with step
# Bug report

<!--
Hi, thanks for submitting a bug. We appreciate that.

But, we will need some information about what's wrong to help you.
-->

## What's wrong
A simple function that splits a list into chunks 
```python
def chunks(lst, amount):
    for index in range(0, len(lst), amount):
        yield lst[index:index + amount]
```
will raise `ImplicitEnumerateViolation`. 

Documentation suggests using `enumerate(lst)` instead of `range(0, len(list))` but there is no `step`
<!-- Describe what is not working. Please, attach a traceback. -->

## How it should be
`ImplicitEnumerateViolation` should not raise if `step > 0` in range arguments.
<!-- Describe how it should work. -->

<!--

  Love wemake-python-styleguide? ❤️
  Do you want your bugs to be solved faster?

  Please consider supporting our collective:
👉  https://opencollective.com/wemake-python-styleguide/donate

-->
False positive ImplicitEnumerateViolation on range with step
# Bug report

<!--
Hi, thanks for submitting a bug. We appreciate that.

But, we will need some information about what's wrong to help you.
-->

## What's wrong
A simple function that splits a list into chunks 
```python
def chunks(lst, amount):
    for index in range(0, len(lst), amount):
        yield lst[index:index + amount]
```
will raise `ImplicitEnumerateViolation`. 

Documentation suggests using `enumerate(lst)` instead of `range(0, len(list))` but there is no `step`
<!-- Describe what is not working. Please, attach a traceback. -->

## How it should be
`ImplicitEnumerateViolation` should not raise if `step > 0` in range arguments.
<!-- Describe how it should work. -->

<!--

  Love wemake-python-styleguide? ❤️
  Do you want your bugs to be solved faster?

  Please consider supporting our collective:
👉  https://opencollective.com/wemake-python-styleguide/donate

-->


Discussion:
@AlwxSin fix is welcome!
@sobolevn yeah. I'll try to fix it in a week or two if no one else wouldn't take it.
@AlwxSin fix is welcome!
@sobolevn yeah. I'll try to fix it in a week or two if no one else wouldn't take it.

Resolving patch:
diff --git a/wemake_python_styleguide/visitors/ast/functions.py b/wemake_python_styleguide/visitors/ast/functions.py
--- a/wemake_python_styleguide/visitors/ast/functions.py
+++ b/wemake_python_styleguide/visitors/ast/functions.py
@@ -240,22 +240,38 @@ def _check_type_compare(self, node: ast.Call) -> None:
             self.add_violation(TypeCompareViolation(node))
 
     def _check_range_len(self, node: ast.Call) -> None:
-        function_name = functions.given_function_called(node, {'range'})
-        if not function_name:
+        if not functions.given_function_called(node, {'range'}):
             return
 
-        is_one_argument_range = (
-            len(node.args) == 1 and
+        args_len = len(node.args)
+
+        is_one_arg_range = (
+            args_len == 1 and
             isinstance(node.args[0], ast.Call) and
             functions.given_function_called(node.args[0], {'len'})
         )
-        is_two_arguments_range = (
+        is_two_args_range = (
+            self._is_multiple_args_range_with_len(node) and
+            args_len == 2
+        )
+        # for three args add violation
+        # only if `step` arg do not equals 1 or -1
+        step_arg = args_len == 3 and operators.unwrap_unary_node(node.args[2])
+        is_three_args_range = (
+            self._is_multiple_args_range_with_len(node) and
+            args_len == 3 and
+            isinstance(step_arg, ast.Num) and
+            abs(step_arg.n) == 1
+        )
+        if any([is_one_arg_range, is_two_args_range, is_three_args_range]):
+            self.add_violation(ImplicitEnumerateViolation(node))
+
+    def _is_multiple_args_range_with_len(self, node: ast.Call) -> bool:
+        return bool(
             len(node.args) in {2, 3} and
             isinstance(node.args[1], ast.Call) and
-            functions.given_function_called(node.args[1], {'len'})
+            functions.given_function_called(node.args[1], {'len'}),
         )
-        if is_one_argument_range or is_two_arguments_range:
-            self.add_violation(ImplicitEnumerateViolation(node))
 
 
 @final


Test patch:
diff --git a/tests/test_visitors/test_ast/test_functions/test_call_context/test_range_len.py b/tests/test_visitors/test_ast/test_functions/test_call_context/test_range_len.py
--- a/tests/test_visitors/test_ast/test_functions/test_call_context/test_range_len.py
+++ b/tests/test_visitors/test_ast/test_functions/test_call_context/test_range_len.py
@@ -17,6 +17,8 @@
     'len([])',
     'len(some)',
     'len(range(10))',
+    'range(0, len(some), 2)',
+    'range(0, len(some), -2)',
 ])
 def test_correct_range_len(
     assert_errors,