Skip to content

Commit

Permalink
fix: '# pragma: no branch' in multiline if statements. #754 (#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
devdanzin committed May 2, 2024
1 parent 34d3eb7 commit 277c8c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion coverage/parser.py
Expand Up @@ -115,7 +115,7 @@ def lines_matching(self, *regexes: str) -> set[TLineNo]:
matches = set()
for i, ltext in enumerate(self.lines, start=1):
if regex_c.search(ltext):
matches.add(i)
matches.add(self._multiline.get(i, i))
return matches

def _raw_parse(self) -> None:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_parser.py
Expand Up @@ -425,6 +425,24 @@ def f():
)
assert parser.statements == {1,7}

def test_multiline_if_no_branch(self) -> None:
# From https://github.com/nedbat/coveragepy/issues/754
parser = self.parse_text("""\
if (this_is_a_verylong_boolean_expression == True # pragma: no branch
and another_long_expression and here_another_expression):
do_something()
""",
)
parser2 = self.parse_text("""\
if this_is_a_verylong_boolean_expression == True and another_long_expression \\
and here_another_expression: # pragma: no branch
do_something()
""",
)
assert parser.statements == parser2.statements == {1, 3}
pragma_re = ".*pragma: no branch.*"
assert parser.lines_matching(pragma_re) == parser2.lines_matching(pragma_re)

def test_excluding_function(self) -> None:
parser = self.parse_text("""\
def fn(foo): # nocover
Expand Down

0 comments on commit 277c8c4

Please sign in to comment.