Repository: pypa/setuptools_scm
Pull request #544 (2021-03-18 20:47:02)

enhance docs and old python erroring


Patch:
diff --git a/.gitignore b/.gitignore
index 66b1ad7..b66b65a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -44,6 +44,7 @@ htmlcov/
 nosetests.xml
 coverage.xml
 *,cover
+.hypothesis/
 
 # Sphinx documentation
 docs/_build/
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 643feb3..5ed8598 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -12,11 +12,11 @@ repos:
     -   id: check-yaml
     -   id: debug-statements
 -   repo: https://github.com/PyCQA/flake8
-    rev: 3.9.1
+    rev: 3.9.0
     hooks:
     -   id: flake8
 -   repo: https://github.com/asottile/pyupgrade
-    rev: v2.12.0
+    rev: v2.11.0
     hooks:
     - id: pyupgrade
       args: [--py36-plus]
diff --git a/README.rst b/README.rst
index ade4d86..62ad85c 100644
--- a/README.rst
+++ b/README.rst
@@ -73,8 +73,12 @@ to be supplied to ``get_version()``. For example:
     write_to = "pkg/version.py"
 
 
-``setup.py`` usage
-------------------
+``setup.py`` usage (deprecated)
+-------------------------------
+
+.. warning::
+
+   ``setup_requires`` has been deprecated in favor of ``pyproject.toml``
 
 The following settings are considered legacy behavior and
 superseded by the ``pyproject.toml`` usage, but for maximal
@@ -129,8 +133,8 @@ You can confirm the version number locally via ``setup.py``:
    not defined in ``setup.cfg``.
 
 
-``setup.cfg`` usage
--------------------
+``setup.cfg`` usage (deprecated)
+------------------------------------
 
 as ``setup_requires`` is deprecated in favour of ``pyproject.toml``
 usage in ``setup.cfg`` is considered deprecated,
@@ -147,7 +151,7 @@ than the project's root, you can use:
     from setuptools_scm import get_version
     version = get_version(root='..', relative_to=__file__)
 
-See `setup.py Usage`_ above for how to use this within ``setup.py``.
+See `setup.py Usage (deprecated)`_ above for how to use this within ``setup.py``.
 
 
 Retrieving package version at runtime
@@ -169,7 +173,7 @@ or the `importlib_metadata`_ backport:
         pass
 
 Alternatively, you can use ``pkg_resources`` which is included in
-``setuptools``:
+``setuptools`` (but has a significant runtime cost):
 
 .. code:: python
 
@@ -196,9 +200,10 @@ instead use ``importlib.metadata`` after editable/real installation:
 
 .. code:: python
 
+    # contents of docs/conf.py
     from importlib.metadata import version
-
-    release = version("package-name")
+    release = version('myproject')
+    # for example take major/minor
     version = '.'.join(release.split('.')[:2])
 
 The underlying reason is, that services like *Read the Docs* sometimes change
diff --git a/setup.py b/setup.py
index ed7425a..f3169d0 100644
--- a/setup.py
+++ b/setup.py
@@ -15,17 +15,26 @@ import setuptools
 
 
 def scm_config():
+
+    if sys.version_info < (3, 6):
+        raise RuntimeError(
+            "support for python < 3.6 has been removed in setuptools_scm>=6.0.0"
+        )
+
     here = os.path.dirname(os.path.abspath(__file__))
     src = os.path.join(here, "src")
     egg_info = os.path.join(src, "setuptools_scm.egg-info")
     has_entrypoints = os.path.isdir(egg_info)
     import pkg_resources
 
+    pkg_resources.require("setuptools>=45")
+
     sys.path.insert(0, src)
     pkg_resources.working_set.add_entry(src)
     # FIXME: remove debug
     print(src)
     print(pkg_resources.working_set)
+
     from setuptools_scm.hacks import parse_pkginfo
     from setuptools_scm.git import parse as parse_git
     from setuptools_scm.version import guess_next_dev_version, get_local_node_and_date
diff --git a/src/setuptools_scm/__init__.py b/src/setuptools_scm/__init__.py
index 97061f2..bc5797d 100644
--- a/src/setuptools_scm/__init__.py
+++ b/src/setuptools_scm/__init__.py
@@ -5,11 +5,6 @@
 import os
 import warnings
 
-try:
-    from packaging.version import parse
-except ImportError:
-    from pkg_resources import parse_version as parse
-
 from .config import (
     Configuration,
     DEFAULT_VERSION_SCHEME,
@@ -88,12 +83,15 @@ def dump_version(root, version, write_to, template=None):
             )
         )
 
-    parsed_version = parse(version)
-    version_fields = parsed_version.release
-    if parsed_version.dev is not None:
-        version_fields += (f"dev{parsed_version.dev}",)
-    if parsed_version.local is not None:
-        version_fields += (parsed_version.local,)
+    # 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, version_tuple=tuple(version_fields)))


Test patch:
diff --git a/testing/conftest.py b/testing/conftest.py
index 2eff745..49cb1e0 100644
--- a/testing/conftest.py
+++ b/testing/conftest.py
@@ -49,7 +49,7 @@ class Wd:
 
     def _reason(self, given_reason):
         if given_reason is None:
-            return f"number-{next(self.__counter)}"
+            return "number-{c}".format(c=next(self.__counter))
         else:
             return given_reason
 
diff --git a/testing/test_basic_api.py b/testing/test_basic_api.py
index 5aaf8fa..5e35a9e 100644
--- a/testing/test_basic_api.py
+++ b/testing/test_basic_api.py
@@ -112,18 +112,6 @@ def test_dump_version(tmpdir):
     assert "version = '1.0.dev42'" in lines
     assert "version_tuple = (1, 0, 'dev42')" in lines
 
-    dump_version(sp, "1.0.1+g4ac9d2c", "second.py")
-    content = tmpdir.join("second.py").read()
-    lines = content.splitlines()
-    assert "version = '1.0.1+g4ac9d2c'" in lines
-    assert "version_tuple = (1, 0, 1, 'g4ac9d2c')" in lines
-
-    dump_version(sp, "1.2.3.dev18+gb366d8b.d20210415", "third.py")
-    content = tmpdir.join("third.py").read()
-    lines = content.splitlines()
-    assert "version = '1.2.3.dev18+gb366d8b.d20210415'" in lines
-    assert "version_tuple = (1, 2, 3, 'dev18', 'gb366d8b.d20210415')" in lines
-
     import ast
 
     ast.parse(content)
diff --git a/testing/test_integration.py b/testing/test_integration.py
index 1c87108..9b8f445 100644
--- a/testing/test_integration.py
+++ b/testing/test_integration.py
@@ -1,4 +1,5 @@
 import sys
+import os
 
 import pytest
 
@@ -68,3 +69,16 @@ def test_pretend_version_name_takes_precedence(tmpdir, monkeypatch, wd):
     monkeypatch.setenv(PRETEND_KEY_NAMED.format(name="test".upper()), "1.0.0")
     monkeypatch.setenv(PRETEND_KEY, "2.0.0")
     assert wd.get_version(dist_name="test") == "1.0.0"
+
+
+def test_own_setup_fails_on_old_python(monkeypatch):
+    monkeypatch.setattr("sys.version_info", (3, 5))
+    monkeypatch.syspath_prepend(os.path.dirname(os.path.dirname(__file__)))
+
+    import setup
+
+    with pytest.raises(
+        RuntimeError,
+        match="support for python < 3.6 has been removed in setuptools_scm>=6.0.0",
+    ):
+        setup.scm_config()