From a28deaf0092579b6ad26edc64deff2b461f4a1f7 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 12 Oct 2024 02:02:39 -0400 Subject: [PATCH] fix: also validate existing dependency groups Signed-off-by: Henry Schreiner --- src/validate_pyproject/extra_validations.py | 33 ++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/validate_pyproject/extra_validations.py b/src/validate_pyproject/extra_validations.py index c4ffe65..8ea1efb 100644 --- a/src/validate_pyproject/extra_validations.py +++ b/src/validate_pyproject/extra_validations.py @@ -24,6 +24,13 @@ class RedefiningStaticFieldAsDynamic(ValidationError): ) +class IncludedDependencyGroupMustExist(ValidationError): + _DESC = """An included dependency group must exist and must not be cyclic. + """ + __doc__ = _DESC + _URL = "https://peps.python.org/pep-0735/" + + def validate_project_dynamic(pyproject: T) -> T: project_table = pyproject.get("project", {}) dynamic = project_table.get("dynamic", []) @@ -49,4 +56,28 @@ def validate_project_dynamic(pyproject: T) -> T: return pyproject -EXTRA_VALIDATIONS = (validate_project_dynamic,) +def validate_include_depenency(pyproject: T) -> T: + dependency_groups = pyproject.get("dependency-groups", {}) + for key, value in dependency_groups.items(): + for each in value: + if ( + isinstance(each, dict) + and (include_group := each.get("include-group")) + and include_group not in dependency_groups + ): + raise IncludedDependencyGroupMustExist( + message=f"The included dependency group {include_group} doesn't exist", + value={ + "field": include_group, + }, + name=f"data.dependency_groups.{key}", + definition={ + "description": cleandoc(IncludedDependencyGroupMustExist._DESC), + "see": IncludedDependencyGroupMustExist._URL, + }, + rule="PEP 621", + ) + return pyproject + + +EXTRA_VALIDATIONS = (validate_project_dynamic, validate_include_depenency)