Skip to content
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

Fix handling of multi-line function declarations #1705

Merged
merged 5 commits into from
Dec 2, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

- Fix: function definitions with multi-line signatures can now be excluded by
matching any of the lines, closing `issue 684`_. Thanks, `Jan Rusak and
others <pull 1705_>`_.

- Added new :ref:`debug options <cmd_run_debug>`:

- ``pytest`` writes the pytest test name into the debug output.

- ``dataop2`` writes the full data being added to CoverageData objects.

.. _issue 684: https://github.com/nedbat/coveragepy/issues/684
.. _pull 1705: https://github.com/nedbat/coveragepy/pull/1705


.. scriv-start-here

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ J. M. F. Tsang
JT Olds
Jacqueline Lee
Jakub Wilk
Jan Rusak
Janakarajan Natarajan
Jerin Peter George
Jessamyn Smith
Expand Down
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).
if nesting == 0 and prev_toktype == token.OP and prev_ttext == ":":
statement_multilines = set(range(first_line, elineno + 1))
if statement_multilines & set(self.raw_excluded):
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
2 changes: 1 addition & 1 deletion tests/coveragetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def check_coverage(
# Get the analysis results, and check that they are right.
analysis = cov._analyze(mod)
statements = sorted(analysis.statements)
if lines is not None:
if lines is not None and len(lines) != 0:
if isinstance(lines[0], int):
# lines is just a list of numbers, it must match the statements
# found in the code.
Expand Down
110 changes: 110 additions & 0 deletions tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,100 @@ def fn(foo): #pragma: NO COVER
""",
[6,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'])

self.check_coverage("""\
a = 0
def very_long_function_to_exclude_name(
very_long_argument1,
very_long_argument2
):
pass
assert a == 0
""",
[1,7], "", excludes=['function_to_exclude'])

self.check_coverage("""\
def my_func(
super_long_input_argument_0=0,
super_long_input_argument_1=1,
super_long_input_argument_2=2):
pass

def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
pass
""",
[], "", excludes=['my_func'])

self.check_coverage("""\
def my_func(
super_long_input_argument_0=0,
super_long_input_argument_1=1,
super_long_input_argument_2=2):
pass

def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
pass
""",
[1,5], "5", excludes=['my_func_2'])

self.check_coverage("""\
def my_func (
super_long_input_argument_0=0,
super_long_input_argument_1=1,
super_long_input_argument_2=2):
pass

def my_func_2 (super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
pass
""",
[1,5], "5", excludes=['my_func_2'])

self.check_coverage("""\
def my_func (
super_long_input_argument_0=0,
super_long_input_argument_1=1,
super_long_input_argument_2=2):
pass

def my_func_2 (super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
pass
""",
[], "5", excludes=['my_func'])

self.check_coverage("""\
def my_func \
(
super_long_input_argument_0=0,
super_long_input_argument_1=1
):
pass

def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
pass
""",
[1,5], "5", excludes=['my_func_2'])

self.check_coverage("""\
def my_func \
(
super_long_input_argument_0=0,
super_long_input_argument_1=1
):
pass

def my_func_2(super_long_input_argument_0=0, super_long_input_argument_1=1, super_long_input_argument_2=2):
pass
""",
[], "5", excludes=['my_func'])

def test_excluding_method(self) -> None:
self.check_coverage("""\
class Fooey:
Expand All @@ -1560,6 +1654,22 @@ def foo(self): #pragma: NO COVER
""",
[1,2,3,8,9], "", excludes=['#pragma: NO COVER'])

self.check_coverage("""\
class Fooey:
def __init__(self):
self.a = 1

def very_long_method_to_exclude_name(
very_long_argument1,
very_long_argument2
):
pass

x = Fooey()
assert x.a == 1
""",
[1,2,3,11,12], "", excludes=['method_to_exclude'])

def test_excluding_class(self) -> None:
self.check_coverage("""\
class Fooey: #pragma: NO COVER
Expand Down