From 277c8c43c9ee59c941ec8fd7da8ea2a49049d1e0 Mon Sep 17 00:00:00 2001 From: devdanzin <74280297+devdanzin@users.noreply.github.com> Date: Thu, 2 May 2024 18:43:37 -0300 Subject: [PATCH] fix: '# pragma: no branch' in multiline if statements. #754 (#1773) --- coverage/parser.py | 2 +- tests/test_parser.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/coverage/parser.py b/coverage/parser.py index 8a64f6247..7842b36c9 100644 --- a/coverage/parser.py +++ b/coverage/parser.py @@ -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: diff --git a/tests/test_parser.py b/tests/test_parser.py index 69de60411..eaaf4ae0a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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