summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.rst5
-rw-r--r--coverage/parser.py5
-rw-r--r--tests/test_arcs.py13
3 files changed, 21 insertions, 2 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index f3be6387..9453f1ae 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -20,7 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------
-Nothing yet.
+- Fixed a mis-measurement of a strange use of wildcard alternatives in
+ match/case statements, closing `issue 1421`_.
+
+.. _issue 1421: https://github.com/nedbat/coveragepy/issues/1421
.. _changes_6-6-0b1:
diff --git a/coverage/parser.py b/coverage/parser.py
index 8b2a9ac5..c4fef9ce 100644
--- a/coverage/parser.py
+++ b/coverage/parser.py
@@ -1041,7 +1041,10 @@ class AstArcAnalyzer:
had_wildcard = False
for case in node.cases:
case_start = self.line_for_node(case.pattern)
- if isinstance(case.pattern, ast.MatchAs):
+ pattern = case.pattern
+ while isinstance(pattern, ast.MatchOr):
+ pattern = pattern.patterns[-1]
+ if isinstance(pattern, ast.MatchAs):
had_wildcard = True
self.add_arc(last_start, case_start, "the pattern on line {lineno} always matched")
from_start = ArcStart(case_start, cause="the pattern on line {lineno} never matched")
diff --git a/tests/test_arcs.py b/tests/test_arcs.py
index d907e8c7..1f2e50d7 100644
--- a/tests/test_arcs.py
+++ b/tests/test_arcs.py
@@ -1362,6 +1362,19 @@ class MatchCaseTest(CoverageTest):
)
assert self.stdout() == "None\nno go\ngo: n\n"
+ def test_absurd_wildcard(self):
+ # https://github.com/nedbat/coveragepy/issues/1421
+ self.check_coverage("""\
+ def absurd(x):
+ match x:
+ case (3 | 99 | (999 | _)):
+ print("default")
+ absurd(5)
+ """,
+ arcz=".1 15 5. .2 23 34 4.",
+ )
+ assert self.stdout() == "default\n"
+
class OptimizedIfTest(CoverageTest):
"""Tests of if statements being optimized away."""