Repository: pdm-project/pdm
Pull request #2182 (2023-08-14 03:33:22)

Fix comparable_version()
## Pull Request Checklist

- [x] A news fragment is added in `news/` describing what is new.
- [x] Test cases added for changed code.

## Describe what you have changed in this PR.

This will complete the attribute editing procedure.
It makes `comarable_version("1.2.3+local1") == Version("1.2.3")` which is not achievable now.

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/pdm-project/pdm/pull/2182?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) Report
Patch coverage: **`100.00%`** and no project coverage change.
> Comparison is base [(`79a56a7`)](https://app.codecov.io/gh/pdm-project/pdm/commit/79a56a78ec586244a3b3535e3fdaa19d5ac84619?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) 84.27% compared to head [(`6b65221`)](https://app.codecov.io/gh/pdm-project/pdm/pull/2182?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) 84.27%.

:exclamation: Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please [install the Github App Integration](https://github.com/apps/codecov) for your organization. [Read more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?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    #2182   +/-   ##
=======================================
  Coverage   84.27%   84.27%           
=======================================
  Files         100      100           
  Lines        9786     9787    +1     
  Branches     2161     2161           
=======================================
+ Hits         8247     8248    +1     
  Misses       1065     1065           
  Partials      474      474           
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `84.05% <100.00%> (+<0.01%)` | :arrow_up: |

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.

| [Files Changed](https://app.codecov.io/gh/pdm-project/pdm/pull/2182?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Coverage Δ | |
|---|---|---|
| [src/pdm/utils.py](https://app.codecov.io/gh/pdm-project/pdm/pull/2182?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#diff-c3JjL3BkbS91dGlscy5weQ==) | `84.76% <100.00%> (+0.05%)` | :arrow_up: |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pdm-project/pdm/pull/2182?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).

drunkwcodes: Note: I am currently forking `pdm` to cater to the needs of the Mojo language, and the resulting project will be named `mojo-muse`. I appreciate your work in developing this well-established architecture and software.

Please feel free to provide any guidance or insights if necessary. Thank you very much for your support and assistance!
drunkwcodes: @frostming The file is added. Please review again, thanks. 

Patch:
diff --git a/news/2182.bugfix.md b/news/2182.bugfix.md
new file mode 100644
index 00000000..7ef16f94
--- /dev/null
+++ b/news/2182.bugfix.md
@@ -0,0 +1,1 @@
+Makes `comarable_version("1.2.3+local1") == Version("1.2.3")`.
\ No newline at end of file
diff --git a/src/pdm/utils.py b/src/pdm/utils.py
index 72e4a60f..f903a461 100644
--- a/src/pdm/utils.py
+++ b/src/pdm/utils.py
@@ -19,7 +19,7 @@ import warnings
 from pathlib import Path
 from typing import TYPE_CHECKING
 
-from packaging.version import Version
+from packaging.version import Version, _cmpkey
 
 from pdm.compat import importlib_metadata
 
@@ -344,6 +344,17 @@ def comparable_version(version: str) -> Version:
     if parsed.local is not None:
         # strip the local part
         parsed._version = parsed._version._replace(local=None)
+
+        # To make comparable_version("1.2.3+local1") == Version("1.2.3")
+        parsed._key = _cmpkey(
+            parsed._version.epoch,
+            parsed._version.release,
+            parsed._version.pre,
+            parsed._version.post,
+            parsed._version.dev,
+            parsed._version.local,
+        )
+
     return parsed
 
 


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 218a3205..86976e26 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -3,6 +3,7 @@ import sys
 
 import pytest
 import tomlkit
+from packaging.version import Version
 
 from pdm import utils
 from pdm.cli import utils as cli_utils
@@ -145,3 +146,8 @@ def test_deprecation_warning():
 
     with pytest.raises(FutureWarning):
         utils.deprecation_warning("Test warning", raise_since="0.0")
+
+
+def test_comparable_version():
+    assert utils.comparable_version("1.2.3") == Version("1.2.3")
+    assert utils.comparable_version("1.2.3a1+local1") == Version("1.2.3a1")