Skip to content

B909: visit if conditions and alternate branches - #606

Merged
cooperlees merged 2 commits into
PyCQA:mainfrom
Don-Yin:fix-b909-if-branches
Oct 24, 2026
Merged

cooperlees merged 2 commits into
PyCQA:mainfrom
Don-Yin:fix-b909-if-branches

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

B909 currently visits only the body of an if, so mutations in its condition or elif/else branches go unnoticed. For example:

items = [1, 2, 3]
for item in items:
    if item == 1:
        pass
    else:
        items.remove(item)

flake8 --isolated --select=B909 example.py reports nothing on main; with this change it reports the mutation on line 6.

This adds the missing visits and keeps alternate branches in separate mutation blocks, preserving the existing handling of unconditional break. The existing B909 fixture now covers elif, else, a mutating condition, and unconditional/conditional break controls.

Validation:

  • RED on main: the updated fixture is missing four expected diagnostics; GREEN with the fix.
  • Tox suites for Python 3.10–3.14 and pep8_naming pass. Python 3.15 was not available locally.
  • pre-commit run --all-files passes (isort, Black, flake8, rstcheck).
  • py313-mypy reports the same seven errors on pristine base bc1ec773c4bcaf121ca810cb78f5fcb3249d88c9 and this branch.

Measured B909 counts for six small snippets, including unchanged controls (inputs and diagnostic output):

B909 diagnostic counts before and after the fix

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused traversal fix has targeted regression coverage and no unresolved review findings.

No open issues 🎉

What changed in this PR

This PR fixes missed B909 diagnostics by extending the mutation checker to visit if conditions and alternate branches.

Changes:

  • Visit conditions and elif/else branches while keeping alternate mutation blocks separate.
  • Add regression cases for these paths and unconditional versus conditional break behavior.
File Description
tests/​eval_files/​b909.py Adds regression coverage for conditions, alternate branches, and break handling.
bugbear.py Extends B909’s if traversal while preserving separate mutation blocks.

Review effort: Balanced

@cooperlees cooperlees left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for this! Good enchancement

@copilot Can you add a CHANGE LOG entry for this?

@cooperlees

Copy link
Copy Markdown
Collaborator

@copilot Can you add a change log entry for this on to the PR in a new commit please.

cooperlees added a commit to Don-Yin/flake8-bugbear that referenced this pull request Oct 24, 2026
Don-Yin and others added 2 commits October 24, 2026 20:16
Visit the test and orelse nodes while keeping conditional mutation blocks
separate, preserving the existing handling of unconditional breaks.

Signed-off-by: Don Yin <Don_Yin@outlook.com>
@cooperlees
cooperlees force-pushed the fix-b909-if-branches branch from cd245d4 to 8a37d0b Compare October 24, 2026 20:16
@cooperlees
cooperlees merged commit 22d0af7 into PyCQA:main Oct 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants