Repository: pdm-project/pdm
Pull request #3045 (2024-07-24 04:28:43)

fix: correct the logic of unselecting groups
Signed-off-by: Frost Ming <me@frostming.com>

## Pull Request Checklist

- [ ] A news fragment is added in `news/` describing what is new.
- [ ] Test cases added for changed code.

## Describe what you have changed in this PR.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pdm-project/pdm/pull/3045?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 85.60%. Comparing base [(`8d7a79b`)](https://app.codecov.io/gh/pdm-project/pdm/commit/8d7a79bef81b7e357e230ea6bf8e2a89f9f37ede?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) to head [(`133dc88`)](https://app.codecov.io/gh/pdm-project/pdm/commit/133dc881e5e99f8e30c293917b2902ace9c99879?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##             main    #3045   +/-   ##
=======================================
  Coverage   85.60%   85.60%           
=======================================
  Files         109      109           
  Lines       10912    10912           
  Branches     2385     2386    +1     
=======================================
  Hits         9341     9341           
  Misses       1086     1086           
  Partials      485      485           
```

| [Flag](https://app.codecov.io/gh/pdm-project/pdm/pull/3045/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/pdm-project/pdm/pull/3045/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | `85.41% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#carryforward-flags-in-the-pull-request-comment) to find out more.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pdm-project/pdm/pull/3045?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).


Patch:
diff --git a/news/3045.bugfix.md b/news/3045.bugfix.md
new file mode 100644
index 00000000..f87f9d93
--- /dev/null
+++ b/news/3045.bugfix.md
@@ -0,0 +1,2 @@
+Fix the group selection logic, to make `--without GROUP` work as expected.
+```
diff --git a/src/pdm/cli/filters.py b/src/pdm/cli/filters.py
index 9cf47244..91abfcd2 100644
--- a/src/pdm/cli/filters.py
+++ b/src/pdm/cli/filters.py
@@ -89,7 +89,9 @@ class GroupSelection:
         if ":all" in groups:
             groups_set.discard(":all")
             groups_set.update(optional_groups)
-            groups_set -= set(self.excluded_groups)
+        if default:
+            groups_set.add("default")
+        groups_set -= set(self.excluded_groups)
 
         invalid_groups = groups_set - set(project.iter_groups())
         if invalid_groups:
@@ -100,9 +102,7 @@ class GroupSelection:
             groups_set -= invalid_groups
         # Sorts the result in ascending order instead of in random order
         # to make this function pure
-        result = sorted(groups_set)
-        if default:
-            result.insert(0, "default")
+        result = sorted(groups_set, key=lambda x: (x != "default", x))
         return result
 
     def validate(self) -> None:


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 064b68a4..7461d71d 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -469,15 +469,15 @@ def test_dependency_group_selection(project, args, golden):
 @pytest.mark.parametrize(
     "args,golden",
     [
-        ({"groups": [":all"], "excluded_groups": ["web"]}, ["default", "auth", "test", "doc"]),
-        ({"groups": [":all"], "excluded_groups": ["web", "auth"]}, ["default", "test", "doc"]),
-        ({"groups": [":all"], "excluded_groups": ["default", "test"]}, ["default", "web", "auth", "doc"]),
+        ({"groups": [":all"], "excluded_groups": ["web"]}, ["default", "auth", "doc", "test"]),
+        ({"groups": [":all"], "excluded_groups": ["web", "auth"]}, ["default", "doc", "test"]),
+        ({"groups": [":all"], "excluded_groups": ["default", "test"]}, ["auth", "doc", "web"]),
     ],
 )
 def test_exclude_optional_groups_from_all(project, args, golden):
     setup_dependencies(project)
     selection = GroupSelection(project, **args)
-    assert sorted(golden) == sorted(selection)
+    assert golden == list(selection)
 
 
 def test_prod_should_not_be_with_dev(project):