PyCQA/flake8-bugbear

B031 does not take into account if-else statements

開放

#465 建立於 2024年3月24日

 (2 則留言) (0 個反應) (0 位負責人)Python (116 個分叉)github user discovery
bughelp wanted

倉庫指標

星標
 (1,116 顆星)
PR 合併指標
 (平均合併 4小時 17分鐘) (30 天內合併 3 個 PR)

描述

Bugbear reports false-positive "more that one" usage of the generator even if it occurs inside 2 different branches of the if-else statement.

This code

import itertools

for _, group in itertools.groupby([]):
    if True:
        print(group, True)
    else:
        print(group, False)

produces

b031.py:7:15: B031 Using the generator returned from `itertools.groupby()` more than once will do nothing on the second usage. Save the result to a list, if the result is needed multiple times.

but it's obvious that group cannot be used twice here.

flake8 --version:

7.0.0 (flake8-bugbear: 24.2.6, mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.2.0) CPython 3.11.2 on Linux

貢獻者指南