Skip to content

Commit

Permalink
feat(config): detect invalid pyproject.toml options
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed Mar 6, 2024
1 parent bd78e3e commit eafe7fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 13 additions & 1 deletion deptry/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from typing import TYPE_CHECKING, Any

from deptry.exceptions import InvalidPyprojectTOMLOptionsError
from deptry.utils import load_pyproject_toml

if TYPE_CHECKING:
Expand All @@ -11,6 +12,13 @@
import click


def _get_invalid_pyproject_toml_keys(ctx: click.Context, deptry_toml_config_keys: set[str]) -> list[str]:
"""Returns the list of options set in `pyproject.toml` that do not exist as CLI parameters."""
existing_cli_params = {param.name for param in ctx.command.params}

return sorted(deptry_toml_config_keys.difference(existing_cli_params))


def read_configuration_from_pyproject_toml(ctx: click.Context, _param: click.Parameter, value: Path) -> Path | None:
"""
Callback that, given a click context, overrides the default values with configuration options set in a
Expand All @@ -28,11 +36,15 @@ def read_configuration_from_pyproject_toml(ctx: click.Context, _param: click.Par
return value

try:
deptry_toml_config = pyproject_data["tool"]["deptry"]
deptry_toml_config: dict[str, Any] = pyproject_data["tool"]["deptry"]
except KeyError:
logging.debug("No configuration for deptry was found in pyproject.toml.")
return value

invalid_pyproject_toml_keys = _get_invalid_pyproject_toml_keys(ctx, set(deptry_toml_config))
if invalid_pyproject_toml_keys:
raise InvalidPyprojectTOMLOptionsError(invalid_pyproject_toml_keys)

click_default_map: dict[str, Any] = {}

if ctx.default_map:
Expand Down
9 changes: 9 additions & 0 deletions deptry/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import TYPE_CHECKING

from click import UsageError

if TYPE_CHECKING:
from pathlib import Path

Expand Down Expand Up @@ -29,3 +31,10 @@ def __init__(self, version: tuple[int, int]) -> None:
super().__init__(
f"Python version {version[0]}.{version[1]} is not supported. Only versions >= 3.8 are supported."
)


class InvalidPyprojectTOMLOptionsError(UsageError):
def __init__(self, invalid_options: list[str]) -> None:
super().__init__(
f"'[tool.deptry]' section in 'pyproject.toml' contains invalid configuration options: {invalid_options}."
)

0 comments on commit eafe7fd

Please sign in to comment.