| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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.
| Back | FazBrowse Home | New Git URL |
The problem
Excluding the body of a case clause doesn't propagate to the case line, so
the case line is still reported as missing (or as a partial branch under
--branch). This is inconsistent with if/else, where excluding the else:
body makes the else: line disappear from the report.
Reproducer from the issue, with exclude_lines = assert False$:
Root cause
An irrefutable case (a wildcard case _:, or a bare capture like
case other:, with no guard) is the match equivalent of an else clause.
Unlike else:, though, the case line is a real statement with its own arc,
so excluding only its body left the case line behind as a missing statement /
partial branch.
The fix
In PythonParser._raw_parse, while walking the AST, if a case is irrefutable
and its entire body is excluded, the case line is added to the excluded set
too. This mirrors the existing handling that carries exclusions from a
decorator/signature onto a function or class body.
The wildcard detection that AstArcAnalyzer._handle__Match already performed is
factored out into a small _case_is_irrefutable() helper that both places now
share.
Refutable cases such as case 2: are deliberately left alone: their case
line stays reported when the body is excluded, the same way an elif line does.
Tests
bare-capture case other:, and a case whose body is only partly excluded
(which must not be excluded).
wildcard is excluded while a refutable case 2: with an excluded body is
still reported as missing.
ruff format --check, ruff check, and mypy --strict are clean on the
changed files; pylint reports only a pre-existing unrelated TODO.
Fixes #1563.