Skip to content

Commit 277c8c4

Browse files
authored
fix: '# pragma: no branch' in multiline if statements. #754 (#1773)
1 parent 34d3eb7 commit 277c8c4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

coverage/parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def lines_matching(self, *regexes: str) -> set[TLineNo]:
115115
matches = set()
116116
for i, ltext in enumerate(self.lines, start=1):
117117
if regex_c.search(ltext):
118-
matches.add(i)
118+
matches.add(self._multiline.get(i, i))
119119
return matches
120120

121121
def _raw_parse(self) -> None:

tests/test_parser.py

+18
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,24 @@ def f():
425425
)
426426
assert parser.statements == {1,7}
427427

428+
def test_multiline_if_no_branch(self) -> None:
429+
# From https://github.com/nedbat/coveragepy/issues/754
430+
parser = self.parse_text("""\
431+
if (this_is_a_verylong_boolean_expression == True # pragma: no branch
432+
and another_long_expression and here_another_expression):
433+
do_something()
434+
""",
435+
)
436+
parser2 = self.parse_text("""\
437+
if this_is_a_verylong_boolean_expression == True and another_long_expression \\
438+
and here_another_expression: # pragma: no branch
439+
do_something()
440+
""",
441+
)
442+
assert parser.statements == parser2.statements == {1, 3}
443+
pragma_re = ".*pragma: no branch.*"
444+
assert parser.lines_matching(pragma_re) == parser2.lines_matching(pragma_re)
445+
428446
def test_excluding_function(self) -> None:
429447
parser = self.parse_text("""\
430448
def fn(foo): # nocover

0 commit comments

Comments
 (0)