Repository: pypa/setuptools_scm

Issue:
enable version-tuples/lazy parsed versions in write_to contents
follow up to https://github.com/pytest-dev/pytest/pull/7605

a way to have easy static version tuples or a available layz parsed version to do version comparisation correctly is nice to have,
if only the string is there, users of a library are tempted into potentially hazardous solutions to compare versions too quick and too error prone

Discussion:

Patch:
diff --git a/src/setuptools_scm/__init__.py b/src/setuptools_scm/__init__.py
index 6b22b28..beb7168 100644
--- a/src/setuptools_scm/__init__.py
+++ b/src/setuptools_scm/__init__.py
@@ -23,6 +23,7 @@ TEMPLATES = {
 # file generated by setuptools_scm
 # don't change, don't track in version control
 version = {version!r}
+version_tuple = {version_tuple!r}
 """,
     ".txt": "{version}",
 }
@@ -80,8 +81,19 @@ def dump_version(root, version, write_to, template=None):
                 os.path.splitext(target)[1], target
             )
         )
+
+    # version_tuple: each field is converted to an int if possible or kept as string
+    fields = tuple(version.split("."))
+    version_fields = []
+    for field in fields:
+        try:
+            v = int(field)
+        except ValueError:
+            v = field
+        version_fields.append(v)
+
     with open(target, "w") as fp:
-        fp.write(template.format(version=version))
+        fp.write(template.format(version=version, version_tuple=tuple(version_fields)))
 
 
 def _do_parse(config):


Test patch:
diff --git a/testing/test_basic_api.py b/testing/test_basic_api.py
index c5104f5..5111542 100644
--- a/testing/test_basic_api.py
+++ b/testing/test_basic_api.py
@@ -101,9 +101,13 @@ def test_dump_version(tmpdir):
 
     dump_version(sp, "1.0", "first.txt")
     assert tmpdir.join("first.txt").read() == "1.0"
-    dump_version(sp, "1.0", "first.py")
+
+    dump_version(sp, "1.0.dev42", "first.py")
     content = tmpdir.join("first.py").read()
-    assert repr("1.0") in content
+    lines = content.splitlines()
+    assert "version = '1.0.dev42'" in lines
+    assert "version_tuple = (1, 0, 'dev42')" in lines
+
     import ast
 
     ast.parse(content)