Skip to content

Commit

Permalink
Exclude if TYPE_CHECKING: blocks from coverage
Browse files Browse the repository at this point in the history
This is extracted from
#188 as a separate PR.

This commit also adds includes for per-project overriding or extending
of the coverage excludes:

* If a project has a `.cookiecutter/includes/coverage/exclude_also` file
  that will _replace_ the cookiecutter's default `exclude_also`'s

* If a project has a `.cookiecutter/includes/coverage/exclude_also/tail` file
  that will _extend_ the cookiecutter's default `exclude_also`'s

Also fix a bug in `local_extensions.py`'s `include_exists(path)` function:
don't crash if there's a _folder_ (rather than a file) at `path` (the
code was catching the wrong exception class, looks like just a mistake).
  • Loading branch information
seanh committed Jan 10, 2025
1 parent de2f20d commit 731fa0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion _shared/local_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def include_exists(self, context, path):
try:
with self._open(context, path):
return True
except (FileNotFoundError, NotADirectoryError):
except (FileNotFoundError, IsADirectoryError):
return False

@pass_context
Expand Down
11 changes: 11 additions & 0 deletions _shared/project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ show_missing = true
precision = 2
fail_under = 100.00
skip_covered = true
exclude_also = [
{% if include_exists("coverage/exclude_also") %}
{{- include("coverage/exclude_also", indent=4) -}}
{% else %}
# `if TYPE_CHECKING:` blocks are only executed while running mypy.
"if TYPE_CHECKING:",
{% if include_exists("coverage/exclude_also/tail") %}
{{- include("coverage/exclude_also/tail", indent=4) -}}
{% endif %}
{% endif %}
]

[tool.isort]
multi_line_output = 3
Expand Down

0 comments on commit 731fa0e

Please sign in to comment.