Skip to content

Commit

Permalink
fix: ensure dev-dependencies are added to the correct group (#3393)
Browse files Browse the repository at this point in the history
* fix: ensure dev-dependencies are added to the correct group

Before this change, the `group` variable containing the group the user requested was being overridden with the last group processed from the `tool.pdm.dev-dependencies` table, making PDM add dev-dependencies to the wrong group.

* fix: use deepcopy for `dependency-groups` to prevent mutating the original table

This change prevents groups defined in `tool.pdm.dev-dependencies` from being copied to the `dependency-groups` table.

* test: add test for adding dev dependency with existing editables group

This test should prevent #3392 from happening again.

* doc: added news fragment for #3392
  • Loading branch information
dalbitresb12 authored Feb 13, 2025
1 parent c582a94 commit 000ef5f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/3392.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure dev-dependencies are added to the correct group when the `tool.pdm.dev-dependencies` table has groups.
7 changes: 4 additions & 3 deletions src/pdm/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import shutil
import sys
from copy import deepcopy
from functools import cached_property, reduce
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Iterable, Mapping, Sequence, cast
Expand Down Expand Up @@ -669,9 +670,9 @@ def update_dev_dependencies(deps: list[str]) -> None:
metadata, settings = self.pyproject.metadata, self.pyproject.settings
if group == "default":
return metadata.get("dependencies", tomlkit.array()), lambda x: metadata.__setitem__("dependencies", x)
dev_dependencies = self.pyproject._data.get("dependency-groups", {})
for group, items in self.pyproject.settings.get("dev-dependencies", {}).items():
dev_dependencies.setdefault(group, []).extend(items)
dev_dependencies = deepcopy(self.pyproject._data.get("dependency-groups", {}))
for dev_group, items in self.pyproject.settings.get("dev-dependencies", {}).items():
dev_dependencies.setdefault(dev_group, []).extend(items)
deps_setter = [
(
metadata.get("optional-dependencies", {}),
Expand Down
19 changes: 19 additions & 0 deletions tests/cli/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ def test_add_editable_normal_dev_dependency(project, pdm):
assert pep735_group == ["urllib3>=1.22", "idna>=2.7"]


@pytest.mark.usefixtures("working_set", "vcs")
def test_add_dev_dependency_with_existing_editables_group(project, pdm):
project.environment.python_requires = PySpecSet(">=3.6")
url = "git+https://github.com/test-root/demo.git#egg=demo"
pdm(["add", "-dG", "editables", "-e", url], obj=project, strict=True)
pdm(["add", "-d", "urllib3"], obj=project, strict=True)
pdm(["add", "-dG", "named", "idna"], obj=project, strict=True)
assert "editables" in project.pyproject.settings["dev-dependencies"]
assert "dev" in project.pyproject.dependency_groups
assert "named" in project.pyproject.dependency_groups
editables_group = project.pyproject.settings["dev-dependencies"]["editables"]
pep735_group = project.pyproject.dependency_groups["dev"]
pep735_named_group = project.pyproject.dependency_groups["named"]
assert editables_group == ["-e git+https://github.com/test-root/demo.git#egg=demo"]
assert "editables" not in project.pyproject.dependency_groups
assert pep735_group == ["urllib3>=1.22"]
assert pep735_named_group == ["idna>=2.7"]


@pytest.mark.usefixtures("working_set")
def test_add_remote_package_url(project, dev_option, pdm):
project.environment.python_requires = PySpecSet(">=3.6")
Expand Down

0 comments on commit 000ef5f

Please sign in to comment.