Wemake-python-styleguide: WPS500 False-positive with `return` and not `break`

Created on 31 Jul 2020  路  2Comments  路  Source: wemake-services/wemake-python-styleguide

This rule wants

needle = None
for smth in it:
    if smth is worthy:
        needle = smth
        break
else:
    raise LookupError

But inside of the lookup-style functions it's reasonable to just return early instead of maintaining an extra var:

def func(it):
    for smth in it:
        if smth is worthy:
            return smth
    else:
        raise LookupError

This case does not trigger raise when return interrupts the loop, just like break. I think it should not be reported.

bug

All 2 comments

In case of using return, you can omit else:

def func(it):
    for smth in it:
        if smth is worthy:
            return smth
    raise LookupError

I hardly can imagine a case when else for for is needed without break. According to the documentation, break is the only flow control statement that "terminates the loop without executing the else clause鈥檚 suite". For every other case, else can be just inlined.

If you don't mind, I close the issue.

  • Please, feel free to re-open the issue if the proposed solution doesn't work for you.
  • Please, make a PR if you have an idea of how to make the error message or documentation better.
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sobolevn picture sobolevn  路  5Comments

sobolevn picture sobolevn  路  3Comments

serhii73 picture serhii73  路  3Comments

sobolevn picture sobolevn  路  4Comments

jtpavlock picture jtpavlock  路  4Comments