Skip to content

Commit b34bd14

Browse files
committed
fix: properly measure strange use of wildcard alternatives in match/case. #1421
1 parent ea24212 commit b34bd14

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGES.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23-
Nothing yet.
23+
- Fixed a mis-measurement of a strange use of wildcard alternatives in
24+
match/case statements, closing `issue 1421`_.
25+
26+
.. _issue 1421: https://github.com/nedbat/coveragepy/issues/1421
2427

2528

2629
.. _changes_6-6-0b1:

coverage/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,10 @@ def _handle__Match(self, node):
10411041
had_wildcard = False
10421042
for case in node.cases:
10431043
case_start = self.line_for_node(case.pattern)
1044-
if isinstance(case.pattern, ast.MatchAs):
1044+
pattern = case.pattern
1045+
while isinstance(pattern, ast.MatchOr):
1046+
pattern = pattern.patterns[-1]
1047+
if isinstance(pattern, ast.MatchAs):
10451048
had_wildcard = True
10461049
self.add_arc(last_start, case_start, "the pattern on line {lineno} always matched")
10471050
from_start = ArcStart(case_start, cause="the pattern on line {lineno} never matched")

tests/test_arcs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,19 @@ def test_match_case_without_wildcard(self):
13621362
)
13631363
assert self.stdout() == "None\nno go\ngo: n\n"
13641364

1365+
def test_absurd_wildcard(self):
1366+
# https://github.com/nedbat/coveragepy/issues/1421
1367+
self.check_coverage("""\
1368+
def absurd(x):
1369+
match x:
1370+
case (3 | 99 | (999 | _)):
1371+
print("default")
1372+
absurd(5)
1373+
""",
1374+
arcz=".1 15 5. .2 23 34 4.",
1375+
)
1376+
assert self.stdout() == "default\n"
1377+
13651378

13661379
class OptimizedIfTest(CoverageTest):
13671380
"""Tests of if statements being optimized away."""

0 commit comments

Comments
 (0)