Skip to content

Support '# pragma: no branch' in multi-line if statements (fix #754) #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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