Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: excluding non-existing groups for pdm remove #3420

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/3404.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Excluding non-existing groups for `pdm remove`.
2 changes: 1 addition & 1 deletion src/pdm/cli/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def do_remove(
if sync:
do_sync(
project,
selection=GroupSelection(project, default=False, groups=[group]),
selection=GroupSelection(project, default=False, groups=[group], exclude_non_existing=True),
clean=True,
no_editable=no_editable,
no_self=no_self,
Expand Down
6 changes: 6 additions & 0 deletions src/pdm/cli/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ def __init__(
groups: Sequence[str] = (),
group: str | None = None,
excluded_groups: Sequence[str] = (),
exclude_non_existing: bool = False,
):
self.project = project
self.groups = groups
self.group = group
self.default = default
self.dev = dev
self.excluded_groups = excluded_groups
self.exclude_non_existing = exclude_non_existing

@classmethod
def from_options(cls, project: Project, options: argparse.Namespace) -> GroupSelection:
Expand Down Expand Up @@ -101,6 +103,10 @@ def _translated_groups(self) -> list[str]:
err=True,
)
groups_set -= invalid_groups

if self.exclude_non_existing and self.project.lockfile.groups is not None:
groups_set &= {normalize_name(g) for g in self.project.lockfile.groups}

# Sorts the result in ascending order instead of in random order
# to make this function pure
result = sorted(groups_set, key=lambda x: (x != "default", x))
Expand Down
9 changes: 9 additions & 0 deletions tests/cli/test_remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,12 @@ def test_remove_group_not_in_lockfile(project, pdm, mocker):
pdm(["remove", "--group", "tz", "pytz"], obj=project, strict=True)
assert "optional-dependencies" not in project.pyproject.metadata
locker.assert_not_called()


@pytest.mark.usefixtures("working_set")
def test_remove_exclude_non_existing_dev_group_in_lockfile(project, pdm):
pdm(["add", "requests"], obj=project, strict=True)
project.add_dependencies(["pytz"], to_group="tz", dev=True)
assert project.lockfile.groups == ["default"]
result = pdm(["remove", "requests"], obj=project)
assert result.exit_code == 0