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

ENH: exclude TYPE_CHECKING from test coverage #497

Merged
merged 2 commits into from
Jan 21, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ coverage:
status:
project:
default:
target: 33%
target: 32%
threshold: 1%
base: auto
if_no_uploads: error
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ where = ["src"]
[tool.setuptools_scm]
write_to = "src/compwa_policy/version.py"

[tool.coverage.report]
exclude_also = [
"if TYPE_CHECKING:",
]

[tool.coverage.run]
branch = true
source = ["src"]
Expand Down Expand Up @@ -310,7 +315,7 @@ commands = [
[
"pytest",
"{posargs}",
"--cov-fail-under=33",
"--cov-fail-under=32",
"--cov-report=html",
"--cov-report=xml",
"--cov=compwa_policy",
Expand Down
30 changes: 27 additions & 3 deletions src/compwa_policy/check_dev_files/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import rtoml
from ini2toml.api import Translator

from compwa_policy.utilities import CONFIG_PATH
from compwa_policy.utilities.cfg import open_config
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.pyproject import ModifiablePyproject
from compwa_policy.utilities.pyproject import ModifiablePyproject, has_dependency
from compwa_policy.utilities.toml import to_toml_array

if TYPE_CHECKING:
from collections.abc import Iterable
from collections.abc import Iterable, MutableMapping

from tomlkit.items import Array

Expand All @@ -23,6 +23,7 @@
with Executor() as do, ModifiablePyproject.load() as pyproject:
do(_merge_coverage_into_pyproject, pyproject)
do(_merge_pytest_into_pyproject, pyproject)
do(_update_codecov_settings, pyproject)

Check warning on line 26 in src/compwa_policy/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/check_dev_files/pytest.py#L26

Added line #L26 was not covered by tests
do(_update_settings, pyproject)


Expand Down Expand Up @@ -97,3 +98,26 @@
else:
options.append(elements[i])
return options


def _update_codecov_settings(pyproject: ModifiablePyproject) -> None:
if not has_dependency(pyproject, ("coverage", "pytest-cov")):
return
updated = __update_settings(

Check warning on line 106 in src/compwa_policy/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/check_dev_files/pytest.py#L105-L106

Added lines #L105 - L106 were not covered by tests
config=pyproject.get_table("tool.coverage.run", create=True),
branch=True,
source=["src"],
)
updated |= __update_settings(
config=pyproject.get_table("tool.coverage.report", create=True),
exclude_also=to_toml_array(["if TYPE_CHECKING:"], multiline=True),
)
if updated:
msg = "Updated pytest coverage settings"
pyproject.changelog.append(msg)

Check warning on line 117 in src/compwa_policy/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/check_dev_files/pytest.py#L116-L117

Added lines #L116 - L117 were not covered by tests


def __update_settings(config: MutableMapping, **expected: Any) -> bool:
original_config = dict(config)
config.update(expected)
return dict(config) != original_config

Check warning on line 123 in src/compwa_policy/check_dev_files/pytest.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/check_dev_files/pytest.py#L121-L123

Added lines #L121 - L123 were not covered by tests
17 changes: 17 additions & 0 deletions src/compwa_policy/utilities/pyproject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
add_dependency,
create_sub_table,
remove_dependency,
split_dependency_definition,
)

if sys.version_info >= (3, 11):
Expand Down Expand Up @@ -276,6 +277,22 @@
return pyproject.get_package_name() is not None


def has_dependency(pyproject: Pyproject, package: str | tuple[str, ...]) -> bool:
toml_document: PyprojectTOML = pyproject._document # noqa: SLF001
dependencies = set(toml_document.get("project", {}).get("dependencies", []))

Check warning on line 282 in src/compwa_policy/utilities/pyproject/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/utilities/pyproject/__init__.py#L281-L282

Added lines #L281 - L282 were not covered by tests
for group in toml_document.get("dependency-groups", {}).values():
dependencies |= {x for x in group if isinstance(x, str)}

Check warning on line 284 in src/compwa_policy/utilities/pyproject/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/utilities/pyproject/__init__.py#L284

Added line #L284 was not covered by tests
if isinstance(package, str):
packages_to_search = {package}

Check warning on line 286 in src/compwa_policy/utilities/pyproject/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/utilities/pyproject/__init__.py#L286

Added line #L286 was not covered by tests
else:
packages_to_search = set(package)

Check warning on line 288 in src/compwa_policy/utilities/pyproject/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/utilities/pyproject/__init__.py#L288

Added line #L288 was not covered by tests
for dependency in dependencies:
dependency_name, *_ = split_dependency_definition(dependency)

Check warning on line 290 in src/compwa_policy/utilities/pyproject/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/utilities/pyproject/__init__.py#L290

Added line #L290 was not covered by tests
if dependency_name.lower() in packages_to_search:
return True
return False

Check warning on line 293 in src/compwa_policy/utilities/pyproject/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/compwa_policy/utilities/pyproject/__init__.py#L292-L293

Added lines #L292 - L293 were not covered by tests


def get_constraints_file(python_version: PythonVersion) -> Path | None:
path = CONFIG_PATH.pip_constraints / f"py{python_version}.txt"
if path.exists():
Expand Down