B031: don't count mutually exclusive branch usages - #572
Open
Sonike wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Updates B031 to avoid false positives across mutually exclusive conditional branches.
Changes:
- Adds branch-aware generator usage tracking.
- Adds regression and preserved-warning fixtures.
- Updates the unreleased changelog.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
bugbear.py |
Implements branch-aware B031 analysis. |
tests/eval_files/b031.py |
Adds conditional-branch fixtures. |
README.rst |
Documents the B031 change. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+1407
to
+1420
| if isinstance(node, ast.For): | ||
| num_usages = self._check_b031_group_usage( | ||
| node.target, group_name, num_usages, repeated | ||
| ) | ||
| num_usages = self._check_b031_group_usage( | ||
| node.iter, group_name, num_usages, repeated | ||
| ) | ||
| # Any body reference may run once per nested loop iteration. | ||
| num_usages = self._check_b031_group_usages( | ||
| node.body, group_name, num_usages, True | ||
| ) | ||
| return self._check_b031_group_usages( | ||
| node.orelse, group_name, num_usages, repeated | ||
| ) |
Author
There was a problem hiding this comment.
Good catch. I added failing fixtures for conditional branches inside both while and async for, then updated the traversal to treat those loop bodies as repeated. The full Python 3.13/3.14 test runs and all pre-commit checks pass in 98fec80.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #465.
Summary
groupbygenerator uses along each mutually exclusiveif/elif/elsepath instead of summing all branchesValidation
tox -e py313— 80 passed, 1 skipped; 98% coveragepre-commit run --all-files— isort, Black, flake8, and rstcheck passedgit diff --checkScope
This PR is limited to mutually exclusive conditional branches. The annotation-target case mentioned later in the issue was already fixed separately in #558.