Skip to content

Commit

Permalink
feat: add validation schema for depdency-groups section
Browse files Browse the repository at this point in the history
  • Loading branch information
finswimmer committed Feb 9, 2025
1 parent c41b037 commit c501df5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/poetry/core/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,14 @@ def validate(
]
result["errors"] += tool_poetry_validation_errors

dependency_groups = toml_data.get("dependency-groups")
if dependency_groups is not None:
dependency_groups_validation_errors = [
e.replace("data", "dependency-groups")
for e in validate_object(dependency_groups, "dependency-groups-schema")
]
result["errors"] += dependency_groups_validation_errors

# Check for required fields if package mode.
# In non-package mode, there are no required fields.
package_mode = tool_poetry.get("package-mode", True)
Expand Down
18 changes: 18 additions & 0 deletions src/poetry/core/json/schemas/dependency-groups-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"name": "dependency-groups",
"type": "object",
"additionalProperties": {
"title": "Dependency group requirements",
"type": "array",
"items": {
"type": "string"
},
"examples": [
[
"attrs",
"requests ~= 2.28"
]
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[dependency-groups]
testing = [
"pytest",
["pytest-cov", {version = ">=4.0"}],
]

[tool.poetry]
name = "my-package"
version = "1.2.3"
description = "Some description."
authors = ["Awesome Hacker <[email protected]>"]
license = "MIT"

[tool.poetry.dependencies]

[tool.poetry.extras]
15 changes: 15 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,21 @@ def test_create_poetry_with_invalid_dev_dependencies(caplog: LogCaptureFixture)
assert any("dev" in r.groups for r in poetry.package.all_requires)


@pytest.mark.parametrize("with_groups", [True, False])
def test_create_poetry_with_invalid_dependency_groups(with_groups: bool) -> None:
with pytest.raises(RuntimeError) as e:
_ = Factory().create_poetry(
fixtures_dir / "project_with_invalid_dependency_groups",
with_groups=with_groups,
)

expected = """\
The Poetry configuration is invalid:
- dependency-groups.testing[1] must be string
"""
assert str(e.value) == expected


def test_create_poetry_with_groups_and_legacy_dev(caplog: LogCaptureFixture) -> None:
assert not caplog.records

Expand Down

0 comments on commit c501df5

Please sign in to comment.