fix B031 false positive with if-else branches#557
Open
wali-reheman wants to merge 2 commits into
Open
Conversation
Only one branch of an if-else executes at runtime, so using the groupby variable in both branches shouldn't trigger B031. Previously, ast.walk() visited both branches and counted the variable twice, causing a false positive.
cooperlees
requested changes
Jun 15, 2026
cooperlees
left a comment
Collaborator
There was a problem hiding this comment.
Hi - many thanks for the potential bug fix. Can you add some example code into B031's tests to show it now working and the code so I can try understand the bug more please.
Also, please make sure pre-commit passes and let's see if copilot finds anything.
Thanks again.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the B031 check (check_for_b031) to avoid flagging itertools.groupby() iterators as “used multiple times” when the iterator is referenced in both branches of an if/else, since only one branch executes at runtime.
Changes:
- Added parent-tracking within the loop subtree to enable upward traversal from
ast.Namenodes. - Introduced helper logic to detect
if/elseconstructs where both branches reference the group iterator, and attempted to suppress B031 counting in that scenario. - Adjusted B031 usage counting to skip
group_nameoccurrences detected as part of such anif/else.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1225
to
+1236
| body_contains = any( | ||
| isinstance(n, ast.Name) and n.id == name | ||
| for n in ast.walk(if_node.body[0]) | ||
| ) | ||
| else_contains = ( | ||
| bool(if_node.orelse) | ||
| and any( | ||
| isinstance(n, ast.Name) and n.id == name | ||
| for n in ast.walk(if_node.orelse[0]) | ||
| ) | ||
| ) | ||
| return body_contains and else_contains |
Comment on lines
+1269
to
+1273
| # Skip if this name is in an if-else where the other | ||
| # branch also uses it (only one branch executes) | ||
| if is_in_if_branch_where_other_branch_has( | ||
| node, group_name | ||
| ): |
Comment on lines
+1209
to
+1214
| # Build parent map for this subtree so we can walk up | ||
| parent_map: dict[ast.AST, ast.AST] = {} | ||
|
|
||
| class ParentTracker(ast.NodeVisitor): | ||
| def __init__(self) -> None: | ||
| super().__init__() |
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.
when the groupby variable is used inside an if-else, only one branch executes at runtime, so using it in both branches shouldn't trigger B031.