You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Panic: reuse-of-groupby-generator (B031) + match list(group)Â #26624
The reuse-of-groupby-generator rule results in a panic when running on a match statement in a specific constellation. Minimal repro:
from itertools import groupby
for key, group in groupby([]):
match list(group):
case _:
pass
Ruff invocation:
uvx --python 3.14.6 ruff check --isolated --select B031
panic: Panicked at crates/ruff_linter/src/rules/flake8_bugbear/rules/reuse_of_groupby_generator.rs:99:30 when checking `/home/joergherbel/tmp/B031_ruff_crash/repro.py`: `called `Option::unwrap()` on a `None` value`
--> repro.py:1:1
info: This indicates a bug in Ruff.
info: If you could open an issue at https://github.com/astral-sh/ruff/issues/new?title=%5Bpanic%5D, we'd be very appreciative!
info: run with `RUST_BACKTRACE=1` environment variable to show the full backtrace information
Found 1 error.
error: Panic during linting indicates a bug in Ruff. If you could open an issue at:
https://github.com/astral-sh/ruff/issues/new?title=%5BLinter%20panic%5D
...with the relevant file contents, the `pyproject.toml` settings, and the stack trace above, we'd be very appreciative!
I don't have a pyproject.toml in the cwd. Note that this workaround passes:
from itertools import groupby
for key, group in groupby([]):
group_list = list(group)
match group_list:
case _:
pass
Looking into this, the panic is in the Stmt::Match arm of the visitor. This pushes an empty counter frame and then visits the subject, so increment_usage_count unwraps a None on the still-empty inner vec.
Visiting subject before pushing the frame should fix it, rather than pre-pushing a 0 the way the If arm does. The subject is evaluated only once and unconditionally, so it isn't part of any branch. Counting it inside the frame would make match list(group) with a case that also uses group come out as max(1, 1) instead of 2.
@ntBre does this sound good? Happy to open a PR with that plus a regression test if so.
Activity
ntBre commentedon Jul 8, 2026
Thank you for the report!
SuryanshSS1011 commentedon Jul 16, 2026
Looking into this, the panic is in the
Stmt::Matcharm of the visitor. This pushes an empty counter frame and then visits the subject, soincrement_usage_countunwraps aNoneon the still-empty inner vec.Visiting
subjectbefore pushing the frame should fix it, rather than pre-pushing a0the way theIfarm does. Thesubjectis evaluated only once and unconditionally, so it isn't part of any branch. Counting it inside the frame would makematch list(group)with a case that also usesgroupcome out as max(1, 1) instead of 2.@ntBre does this sound good? Happy to open a PR with that plus a regression test if so.
[`flake8-bugbear`] Fix panic when the group is used in a `match` subjâĶ
[`flake8-bugbear`] Fix panic on `match` subjects (`B031`) (#27781)