PY3.R1723

No else break

Used in order to highlight an unnecessary block of code following an if containing a break statement. As such, it will warn when it encounters an else following a chain of if s, all of them containing a break statement.

Noncompliant Code:

Copy
def next_seven_elements(iterator):
    for i, item in enumerate(iterator):
        if i == 7:
            break
        else:
            yield item

Compliant Code:

Copy
def next_seven_elements(iterator):
    for i, item in enumerate(iterator):
        if i == 7:
            break
        yield item

The content on this page is adapted from the Pylint User Guide, Copyright ©2003-2022, Logilab, PyCQA and contributors. All rights reserved. https://pylint.pycqa.org/en/latest/index.html#, and is used under the Python Software Foundation License Version 2. Examples, recipes, and other code in the Pylint documentation are additionally licensed under the Zero Clause BSD License.