Repository navigation
Exclude the case line when an irrefutable case body is excluded - #2269
Merged
nedbat merged 3 commits intoAug 30, 2026
Merged
Conversation
When the whole body of an irrefutable `case` (like `case _:` or a bare capture) is excluded, exclude the `case` line too, the same way an excluded `else:` body makes the `else:` line disappear. Previously the `case` line was left behind and reported as a missing statement (or a partial branch under `--branch`). Refutable cases such as `case 2:` are unaffected and still reported when their body is excluded, matching how an `elif` line behaves. Closes coveragepy#1563.
Member
|
This is now released as part of coverage 7.16.1. |
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.
The problem
Excluding the body of a
caseclause doesn't propagate to thecaseline, sothe
caseline is still reported as missing (or as a partial branch under--branch). This is inconsistent withif/else, where excluding theelse:body makes the
else:line disappear from the report.Reproducer from the issue, with
exclude_lines = assert False$:Root cause
An irrefutable
case(a wildcardcase _:, or a bare capture likecase other:, with no guard) is thematchequivalent of anelseclause.Unlike
else:, though, thecaseline is a real statement with its own arc,so excluding only its body left the
caseline behind as a missing statement /partial branch.
The fix
In
PythonParser._raw_parse, while walking the AST, if acaseis irrefutableand its entire body is excluded, the
caseline is added to the excluded settoo. This mirrors the existing handling that carries exclusions from a
decorator/signature onto a function or class body.
The wildcard detection that
AstArcAnalyzer._handle__Matchalready performed isfactored out into a small
_case_is_irrefutable()helper that both places nowshare.
Refutable cases such as
case 2:are deliberately left alone: theircaseline stays reported when the body is excluded, the same way an
elifline does.Tests
tests/test_parser.py: three unit tests covering an irrefutablecase _:, abare-capture
case other:, and a case whose body is only partly excluded(which must not be excluded).
tests/test_coverage.py: an end-to-end test in branch mode checking that thewildcard is excluded while a refutable
case 2:with an excluded body isstill reported as missing.
ruff format --check,ruff check, andmypy --strictare clean on thechanged files;
pylintreports only a pre-existing unrelated TODO.Fixes #1563.