Repository: pypa/setuptools_scm
Pull request #1005 (2024-01-18 18:10:58)

Add a "only-version" version scheme
Fixes https://github.com/pypa/setuptools_scm/issues/1002 by adding a "only-version" version scheme.

thejcannon: Done and done.
RonnyPfannschmidt: thanks, would you like to see this squashed or should i merge as is?

thejcannon: Oh I'm definitely camp squash-merge. "Rename" isn't much of an inspiring commit message :joy: 

Patch:
diff --git a/docs/extending.md b/docs/extending.md
index 957c762..fa71841 100644
--- a/docs/extending.md
+++ b/docs/extending.md
@@ -84,6 +84,11 @@ representing the version.
 `no-guess-dev`
 : Does no next version guessing, just adds `.post1.devN`
 
+`only-version`
+:   Only use the version from the tag, as given.
+
+    !!! warning "This means version is no longer pseudo unique per commit"
+
 
 ### `setuptools_scm.local_scheme`
 Configures how the local part of a version is rendered given a
diff --git a/pyproject.toml b/pyproject.toml
index a93544d..0df1499 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -98,6 +98,7 @@ PKG-INFO = "setuptools_scm.fallbacks:parse_pkginfo"
 "calver-by-date" = "setuptools_scm.version:calver_by_date"
 "guess-next-dev" = "setuptools_scm.version:guess_next_dev_version"
 "no-guess-dev" = "setuptools_scm.version:no_guess_dev_version"
+"only-version" = "setuptools_scm.version:only_version"
 "post-release" = "setuptools_scm.version:postrelease_version"
 "python-simplified-semver" = "setuptools_scm.version:simplified_semver_version"
 "release-branch-semver" = "setuptools_scm.version:release_branch_semver_version"
diff --git a/src/setuptools_scm/version.py b/src/setuptools_scm/version.py
index f43e14b..8bf0305 100644
--- a/src/setuptools_scm/version.py
+++ b/src/setuptools_scm/version.py
@@ -299,6 +299,10 @@ def release_branch_semver(version: ScmVersion) -> str:
     return release_branch_semver_version(version)
 
 
+def only_version(version: ScmVersion) -> str:
+    return version.format_with("{tag}")
+
+
 def no_guess_dev_version(version: ScmVersion) -> str:
     if version.exact:
         return version.format_with("{tag}")


Test patch:
diff --git a/testing/test_version.py b/testing/test_version.py
index ea4c7d9..5aace36 100644
--- a/testing/test_version.py
+++ b/testing/test_version.py
@@ -15,6 +15,7 @@ from setuptools_scm.version import guess_next_date_ver
 from setuptools_scm.version import guess_next_version
 from setuptools_scm.version import meta
 from setuptools_scm.version import no_guess_dev_version
+from setuptools_scm.version import only_version
 from setuptools_scm.version import release_branch_semver_version
 from setuptools_scm.version import ScmVersion
 from setuptools_scm.version import simplified_semver_version
@@ -170,6 +171,33 @@ def test_bump_dev_version_nonzero_raises() -> None:
         guess_next_version(m("1.0.dev1"))
 
 
+@pytest.mark.parametrize(
+    "version",
+    [
+        "1.dev0",
+        "1.0.dev456",
+        "1.0a1",
+        "1.0a2.dev456",
+        "1.0a12.dev456",
+        "1.0a12",
+        "1.0b1.dev456",
+        "1.0b2",
+        "1.0b2.post345.dev456",
+        "1.0b2.post345",
+        "1.0rc1.dev456",
+        "1.0rc1",
+        "1.0",
+        "1.0.post456.dev34",
+        "1.0.post456",
+        "1.0.15",
+        "1.1.dev1",
+    ],
+)
+def test_only_version(version: str) -> None:
+    assert version == only_version(meta(version, config=c))
+    assert version == only_version(meta(version, distance=2, config=c))
+
+
 @pytest.mark.parametrize(
     ("tag", "expected"),
     [