Skip to content

Commit

Permalink
feat: support PEP 735 (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri authored Oct 15, 2024
2 parents 0f706d1 + 8ba8e1b commit 41e9e09
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/validate_pyproject/extra_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
Expand All @@ -49,4 +56,27 @@ 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=each,
name=f"data.dependency_groups.{key}",
definition={
"description": cleandoc(IncludedDependencyGroupMustExist._DESC),
"see": IncludedDependencyGroupMustExist._URL,
},
rule="PEP 735",
)
# TODO: check for `include-group` cycles (can be conditional to graphlib)
return pyproject


EXTRA_VALIDATIONS = (validate_project_dynamic, validate_include_depenency)
31 changes: 30 additions & 1 deletion src/validate_pyproject/pyproject_toml.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",

"$id": "https://packaging.python.org/en/latest/specifications/declaring-build-dependencies/",
"title": "Data structure for ``pyproject.toml`` files",
"$$description": [
Expand Down Expand Up @@ -60,6 +59,36 @@

"tool": {
"type": "object"
},
"dependency-groups": {
"type": "object",
"description": "Dependency groups following PEP 735",
"additionalProperties": false,
"patternProperties": {
"^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9])$": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string",
"description": "Python package specifiers following PEP 508",
"format": "pep508"
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"include-group": {
"description": "Another dependency group to include in this one",
"type": "string",
"pattern": "^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9._-]*[a-zA-Z0-9])$"
}
}
}
]
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions tests/examples/simple/depgroups.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[dependency-groups]
test = ["one", "two"]
other = ["one", {include-group = "test"}]
1 change: 1 addition & 0 deletions tests/invalid-examples/pep735/invalid-group.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dependency-groups` must not contain {'a b'} properties
2 changes: 2 additions & 0 deletions tests/invalid-examples/pep735/invalid-group.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dependency-groups]
"a b" = ["one"]
1 change: 1 addition & 0 deletions tests/invalid-examples/pep735/invalid-key.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`dependency-groups.mydep[1]` must be valid exactly by one definition
2 changes: 2 additions & 0 deletions tests/invalid-examples/pep735/invalid-key.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dependency-groups]
mydep = ["one", {other = "two"}]
1 change: 1 addition & 0 deletions tests/invalid-examples/pep735/not-pep508.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`dependency-groups.test[0]` must be valid exactly by one definition (0 matches found)
2 changes: 2 additions & 0 deletions tests/invalid-examples/pep735/not-pep508.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[dependency-groups]
test = [" "]

0 comments on commit 41e9e09

Please sign in to comment.