Skip to content

B031 false positive in loop with continue/break/return #10337

@MikkelSchubert

Description

@MikkelSchubert
from __future__ import annotations
from itertools import groupby

def ruff_b031_false_positive(values: list[int]) -> None:
    for key, group in groupby(values):
        if key:
            list(group)
            continue # or break or return

        list(group)
$ ruff check --isolated --select B031 testcase.py
testcase.py:10:14: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage
Found 1 error.

There have been fixes for when the generator is used in different branches of an if statements, see for example #4050, but the warning still triggers if you use continue, break, or return. Tested with Ruff version 0.3.2.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinglinterRelated to the linter

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions