Wemake-python-styleguide: WPS122 should allow a no-op loop var

Created on 18 May 2020  路  8Comments  路  Source: wemake-services/wemake-python-styleguide

Bug report

What's wrong

Imagine I want to do something 3 times but I don't care about the attempt number. So I'd normally write something like this:

for _ in range(3):
    do_something()

But then the linter complains with WPS122 Found all unused variables definition: _ which is incorrect. While things like assignment allow to fix this with

-_something_unused = get_useless_result()
+get_useless_result()

it is impossible with for-loops not to define this loop variable at all.

How is that should be

This case shouldn't be a violation of the given rule.

System information

N/A

bug good first issue help wanted starter pr-merged

Most helpful comment

It is not released. Will be released in 0.15

All 8 comments

I agree, this is a valid use-case. Thanks a lot!

The same applies for comprehensions.

Hi, I would like to work on this issue.

I鈥檓 a newcomer to this project (and open source in general), so I was hoping for some help and clarification :).

Is it enough to just get rid of the visit_any_for function .

Or should I keep it and try to do something more complex where I detect if the for loop is trying to iterate through a range of numbers, and only then not raise an error?

Is there anything extra that has to be done for comprehensions? I couldn鈥檛 really find anything specific to that.

Thank you!

@nn3un thanks a lot!

Is it enough to just get rid of the visit_any_for function

Looks like it!

Is there anything extra that has to be done for comprehensions?

Yes, we should allow for _ in ... there too.

I鈥檓 a newcomer to this project (and open source in general), so I was hoping for some help and clarification

Feel very welcome! 馃憤

Was this actually fixed?
I'm still getting the same error (version 0.14.1).

$ cat test.py

def do_something():
    return

for _ in range(3):
    do_something()
$ flake8 test.py

test.py:1:1: D100 Missing docstring in public module
test.py:1:1: D103 Missing docstring in public function
test.py:2:5: WPS324 Found inconsistent `return` statement
test.py:5:1: WPS122 Found all unused variables definition:

Is this a regression or I'm doing something wrong?

It is not released. Will be released in 0.15

The line "Fixes WPS122 not raising for for and async for definitions" in the Bugfixes of "0.14.0" confuses so much when trying to understand why it doesn't work for "0.14.1".

@Pejnikov can you please move it to the correct section? Thanks! 馃憤

Was this page helpful?
0 / 5 - 0 ratings