Skip to content

Commit

Permalink
fix: add checking if multiline was before a suite #684
Browse files Browse the repository at this point in the history
  • Loading branch information
kajakaj authored and nedbat committed Dec 2, 2023
1 parent 4b7e5a0 commit 6c0d09d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def _raw_parse(self) -> None:
empty = True
first_on_line = True
nesting = 0
prev_ttext = None

assert self.text is not None
tokgen = generate_tokens(self.text)
Expand Down Expand Up @@ -189,6 +190,12 @@ def _raw_parse(self) -> None:
# so record a multi-line range.
for l in range(first_line, elineno+1): # type: ignore[unreachable]
self._multiline[l] = first_line
# Check if multi-line was before a suite (trigger by the colon token).
statement_multilines = set(range(first_line, elineno + 1))
if (statement_multilines & set(self.raw_excluded) and prev_toktype == token.OP
and prev_ttext == ":" and nesting == 0):
exclude_indent = indent
excluding = True
first_line = None
first_on_line = True

Expand All @@ -206,6 +213,7 @@ def _raw_parse(self) -> None:
first_on_line = False

prev_toktype = toktype
prev_ttext = ttext

# Find the starts of the executable statements.
if not empty:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,14 @@ def test_excluding_for_suite(self) -> None:
assert a == 1
""",
[1,7], "", excludes=['#pragma: NO COVER'])
self.check_coverage("""\
a = 0
def very_long_function_to_exclude_name(very_long_argument1,
very_long_argument2):
pass
assert a == 0
""",
[1,5], "", excludes=['function_to_exclude'])

def test_excluding_for_else(self) -> None:
self.check_coverage("""\
Expand Down

0 comments on commit 6c0d09d

Please sign in to comment.