Skip to content

Commit

Permalink
FIX: use itertools.pairwise() correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Oct 28, 2024
1 parent 096351f commit 37fd403
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/compwa_policy/utilities/pyproject/setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,16 @@ def _add_to_optional_dependencies(
)
return True
if isinstance(optional_key, abc.Sequence):
if len(optional_key) < 2: # noqa: PLR2004
msg = "Need at least two keys to define nested optional dependencies"
if len(optional_key) == 0:
msg = "Need at least one key to define nested optional dependencies"
raise ValueError(msg)
this_package = get_package_name(pyproject, raise_on_missing=True)
updated = False
updated &= add_dependency(pyproject, package, optional_key=optional_key[0])
for previous, key in itertools.pairwise(optional_key):
if previous is None:
updated &= add_dependency(pyproject, package, key)
else:
updated &= add_dependency(pyproject, f"{this_package}[{previous}]", key)
updated &= add_dependency(
pyproject, f"{this_package}[{previous}]", optional_key=key
)
return updated
msg = f"Unsupported type for optional_key: {type(optional_key)}"
raise NotImplementedError(msg)
Expand Down
13 changes: 12 additions & 1 deletion tests/utilities/pyproject/test_setters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def test_add_dependency_nested():
""")
pyproject = load_pyproject_toml(src, modifiable=True)
add_dependency(pyproject, "ruff", optional_key=["lint", "style", "dev"])

new_content = tomlkit.dumps(pyproject)
expected = dedent("""
[project]
Expand All @@ -65,6 +64,18 @@ def test_add_dependency_nested():
""")
assert new_content == expected

pyproject = load_pyproject_toml(src, modifiable=True)
add_dependency(pyproject, "ruff", optional_key=["lint"])
new_content = tomlkit.dumps(pyproject)
expected = dedent("""
[project]
name = "my-package"
[project.optional-dependencies]
lint = ["ruff"]
""")
assert new_content == expected


def test_add_dependency_optional():
src = dedent("""
Expand Down

0 comments on commit 37fd403

Please sign in to comment.