Skip to content

Commit

Permalink
fix: properly measure strange use of wildcard alternatives in match/c…
Browse files Browse the repository at this point in the history
…ase. #1421
  • Loading branch information
nedbat committed Nov 3, 2022
1 parent ea24212 commit b34bd14
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,10 @@ def _handle__Match(self, node):
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")
Expand Down
13 changes: 13 additions & 0 deletions tests/test_arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,19 @@ def test_match_case_without_wildcard(self):
)
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."""
Expand Down

0 comments on commit b34bd14

Please sign in to comment.