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

Add disable_block_exclusion Option #1764

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions coverage/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def __init__(self) -> None:
# Defaults for [report]
self.exclude_list = DEFAULT_EXCLUDE[:]
self.exclude_also: list[str] = []
self.disable_block_exclusion: bool = False
self.fail_under = 0.0
self.format: str | None = None
self.ignore_errors = False
Expand Down Expand Up @@ -394,6 +395,7 @@ def copy(self) -> CoverageConfig:
# [report]
("exclude_list", "report:exclude_lines", "regexlist"),
("exclude_also", "report:exclude_also", "regexlist"),
("disable_block_exclusion", "report:disable_block_exclusion", "boolean"),
("fail_under", "report:fail_under", "float"),
("format", "report:format"),
("ignore_errors", "report:ignore_errors", "boolean"),
Expand Down
5 changes: 4 additions & 1 deletion coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def __init__(
text: str | None = None,
filename: str | None = None,
exclude: str | None = None,
disable_block_exclusion: bool | None = False,
) -> None:
"""
Source can be provided as `text`, the text itself, or `filename`, from
Expand All @@ -62,6 +63,8 @@ def __init__(

self.exclude = exclude

self.disable_block_exclusion = disable_block_exclusion

# The text lines of the parsed code.
self.lines: list[str] = self.text.split("\n")

Expand Down Expand Up @@ -217,7 +220,7 @@ def _raw_parse(self) -> None:
# Check whether to end an excluded suite.
if excluding and indent <= exclude_indent:
excluding = False
if excluding:
if excluding and not self.disable_block_exclusion:
self.raw_excluded.add(elineno)
first_on_line = False

Expand Down
6 changes: 3 additions & 3 deletions coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
import os.path
import types
import zipimport

from typing import Iterable, TYPE_CHECKING
from typing import TYPE_CHECKING, Iterable

from coverage import env
from coverage.exceptions import CoverageException, NoSource
from coverage.files import canonical_filename, relative_filename, zip_location
from coverage.misc import isolate_module, join_regex
from coverage.parser import PythonParser
from coverage.phystokens import source_token_lines, source_encoding
from coverage.phystokens import source_encoding, source_token_lines
from coverage.plugin import FileReporter
from coverage.types import TArc, TLineNo, TMorf, TSourceTokenLines

Expand Down Expand Up @@ -184,6 +183,7 @@ def parser(self) -> PythonParser:
self._parser = PythonParser(
filename=self.filename,
exclude=self.coverage._exclude_regex("exclude"),
disable_block_exclusion=self.coverage.config.disable_block_exclusion,
)
self._parser.parse_source()
return self._parser
Expand Down
11 changes: 11 additions & 0 deletions doc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,17 @@ See :ref:`cmd_combine_remapping` and :ref:`source_glob` for more information.
Settings common to many kinds of reporting.


.. _config_report_disable_block_exclusion:

[report] disable_block_exclusion
................................

(boolean, default False) The default behavior when excluding a line from coverage
is if the matched line introduces a block, the entire block is excluded from
reporting. Setting this to True will disable this behavior and only report the
line that was actually matched as excluded from coverage.


.. _config_report_exclude_also:

[report] exclude_also
Expand Down
8 changes: 5 additions & 3 deletions doc/excluding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ code, the "if debug" clause is excluded from reporting::

Any line with a comment of "pragma: no cover" is excluded. If that line
introduces a clause, for example, an if clause, or a function or class
definition, then the entire clause is also excluded. Here the __repr__
function is not reported as missing::
definition, then the entire clause is also excluded (see
:ref:`config_report_disable_block_exclusion` for disabling this feature).
Here the __repr__ function is not reported as missing::

class MyObject(object):
def __init__(self):
Expand Down Expand Up @@ -81,7 +82,8 @@ exclude them all at once without littering your code with exclusion pragmas.

If the matched line introduces a block, the entire block is excluded from
reporting. Matching a ``def`` line or decorator line will exclude an entire
function.
function (see :ref:`config_report_disable_block_exclusion` for disabling this
feature).

.. highlight:: ini

Expand Down