Skip to content

Commit

Permalink
fix: case _ as value shouldn't be considered a missing branch. #1860
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Sep 25, 2024
1 parent a57f17a commit 71f0f4c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ upgrading your version of coverage.py.
Unreleased
----------

- Fix: a final wildcard match/case clause assigning to a name (``case _ as
value``) was incorrectly marked as a missing branch. This is now fixed,
closing `issue 1860`_.

- Fewer things are considered branches now. Lambdas, comprehensions, and
generator expressions are no longer marked as missing branches if they don't
complete execution.
Expand All @@ -47,6 +51,7 @@ Unreleased
.. _pull 1843: https://github.com/nedbat/coveragepy/pull/1843
.. _issue 1846: https://github.com/nedbat/coveragepy/issues/1846
.. _pull 1849: https://github.com/nedbat/coveragepy/pull/1849
.. _issue 1860: https://github.com/nedbat/coveragepy/issues/1860


.. scriv-start-here
Expand Down
2 changes: 2 additions & 0 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,8 @@ def _handle__Match(self, node: ast.Match) -> set[ArcStart]:
pattern = case.pattern # pylint: disable=undefined-loop-variable
while isinstance(pattern, ast.MatchOr):
pattern = pattern.patterns[-1]
while isinstance(pattern, ast.MatchAs) and pattern.pattern is not None:
pattern = pattern.pattern
had_wildcard = (
isinstance(pattern, ast.MatchAs)
and pattern.pattern is None
Expand Down
17 changes: 17 additions & 0 deletions tests/test_arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,23 @@ def test_match_case_with_default(self) -> None:
)
assert self.stdout() == "default\nno go\ngo: n\n"

def test_match_case_with_named_default(self) -> None:
self.check_coverage("""\
for command in ["huh", "go home", "go n"]:
match command.split():
case ["go", direction] if direction in "nesw":
match = f"go: {direction}"
case ["go", _]:
match = "no go"
case _ as value:
match = "default"
print(match)
""",
branchz="12 1-1 34 35 56 57",
branchz_missing="",
)
assert self.stdout() == "default\nno go\ngo: n\n"

def test_match_case_with_wildcard(self) -> None:
self.check_coverage("""\
for command in ["huh", "go home", "go n"]:
Expand Down

0 comments on commit 71f0f4c

Please sign in to comment.