Skip to content

Commit 5a031b0

Browse files
committed
fix: correct the missing branch message for the last case of a match/case
1 parent f466ced commit 5a031b0

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

CHANGES.rst

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26+
- Fix: the last case of a match/case statement had an incorrect message if the
27+
branch was missed. It said the pattern never matched, when actually the
28+
branch is missed if the last case always matched.
29+
2630
- Fix: clicking a line number in the HTML report now positions more accurately.
2731

2832

coverage/parser.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,9 @@ def _handle__Match(self, node: ast.Match) -> set[ArcStart]:
11121112
exits |= self.add_body_arcs(case.body, from_start=from_start)
11131113
last_start = case_start
11141114
if not had_wildcard:
1115-
exits.add(from_start)
1115+
exits.add(
1116+
ArcStart(case_start, cause="the pattern on line {lineno} always matched"),
1117+
)
11161118
return exits
11171119

11181120
def _handle__NodeList(self, node: NodeList) -> set[ArcStart]:

tests/test_parser.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -902,21 +902,23 @@ def test_missing_arc_descriptions_bug460(self) -> None:
902902
assert parser.missing_arc_description(2, -3) == "line 3 didn't finish the lambda on line 3"
903903

904904
@pytest.mark.skipif(not env.PYBEHAVIOR.match_case, reason="Match-case is new in 3.10")
905-
def test_match_case_with_default(self) -> None:
906-
parser = self.parse_text("""\
907-
for command in ["huh", "go home", "go n"]:
908-
match command.split():
909-
case ["go", direction] if direction in "nesw":
910-
match = f"go: {direction}"
911-
case ["go", _]:
912-
match = "no go"
913-
print(match)
905+
def test_match_case(self) -> None:
906+
parser = self.parse_text("""\
907+
match command.split():
908+
case ["go", direction] if direction in "nesw": # 2
909+
match = f"go: {direction}"
910+
case ["go", _]: # 4
911+
match = "no go"
912+
print(match) # 6
914913
""")
915-
assert parser.missing_arc_description(3, 4) == (
916-
"line 3 didn't jump to line 4, because the pattern on line 3 never matched"
914+
assert parser.missing_arc_description(2, 3) == (
915+
"line 2 didn't jump to line 3, because the pattern on line 2 never matched"
917916
)
918-
assert parser.missing_arc_description(3, 5) == (
919-
"line 3 didn't jump to line 5, because the pattern on line 3 always matched"
917+
assert parser.missing_arc_description(2, 4) == (
918+
"line 2 didn't jump to line 4, because the pattern on line 2 always matched"
919+
)
920+
assert parser.missing_arc_description(4, 6) == (
921+
"line 4 didn't jump to line 6, because the pattern on line 4 always matched"
920922
)
921923

922924

0 commit comments

Comments
 (0)