Repository: pypa/setuptools_scm

Issue:
setuptools_scm unable to compute distance?
I have a tarball that was built with a previous version of setuptools_scm. The other day it started failing because the version number it was producing was ``1.None.0``. Nothing in the code base had changed, so I believe it to be a setuptools_scm regression.

This tarball is private, but the ``setup.py`` looks like:

```python
#!/usr/bin/env python
import setuptools


setuptools.setup(
    name="pypi-theme",
    version="15.0",

    packages=[
        "pypi_theme",
    ],

    include_package_data=True,

    use_scm_version={
        "local_scheme": "dirty-tag",
        "version_scheme": lambda v: "1.{.distance}.0".format(v),
    },

    install_requires=[
        "Pyramid",
    ],

    setup_requires=["setuptools_scm"],
)
```

I'm guessing that ``v.distance`` is returning ``None`` now when it previously didn't, but I don't know why.

Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 50f0cde..054bfe5 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,3 +1,9 @@
+v1.15.5
+=======
+
+* fix #167 by correctly respecting preformatted version metadata
+  from PKG-INFO/EGG-INFO
+
 v1.15.4
 =======
 
diff --git a/setuptools_scm/hacks.py b/setuptools_scm/hacks.py
index 2d298be..add89a8 100644
--- a/setuptools_scm/hacks.py
+++ b/setuptools_scm/hacks.py
@@ -10,7 +10,7 @@ def parse_pkginfo(root):
     data = data_from_mime(pkginfo)
     version = data.get('Version')
     if version != 'UNKNOWN':
-        return meta(version)
+        return meta(version, preformatted=True)
 
 
 def parse_pip_egg_info(root):
diff --git a/setuptools_scm/version.py b/setuptools_scm/version.py
index 15c4495..dce8c75 100644
--- a/setuptools_scm/version.py
+++ b/setuptools_scm/version.py
@@ -55,7 +55,10 @@ def tags_to_versions(tags):
 class ScmVersion(object):
     def __init__(self, tag_version,
                  distance=None, node=None, dirty=False,
+                 preformatted=False,
                  **kw):
+        if kw:
+            trace("unknown args", kw)
         self.tag = tag_version
         if dirty and distance is None:
             distance = 0
@@ -64,6 +67,7 @@ class ScmVersion(object):
         self.time = datetime.datetime.now()
         self.extra = kw
         self.dirty = dirty
+        self.preformatted = preformatted
 
     @property
     def exact(self):
@@ -84,13 +88,19 @@ class ScmVersion(object):
         return self.format_with(dirty_format if self.dirty else clean_format)
 
 
-def meta(tag, distance=None, dirty=False, node=None, **kw):
+def _parse_tag(tag, preformatted):
+    if preformatted:
+        return tag
     if SetuptoolsVersion is None or not isinstance(tag, SetuptoolsVersion):
         tag = tag_to_version(tag)
-    trace('version', tag)
+    return tag
+
 
+def meta(tag, distance=None, dirty=False, node=None, preformatted=False, **kw):
+    tag = _parse_tag(tag, preformatted)
+    trace('version', tag)
     assert tag is not None, 'cant parse version %s' % tag
-    return ScmVersion(tag, distance, node, dirty, **kw)
+    return ScmVersion(tag, distance, node, dirty, preformatted, **kw)
 
 
 def guess_next_version(tag_version, distance):
@@ -147,6 +157,8 @@ def postrelease_version(version):
 def format_version(version, **config):
     trace('scm version', version)
     trace('config', config)
+    if version.preformatted:
+        return version.tag
     version_scheme = callable_or_entrypoint(
         'setuptools_scm.version_scheme', config['version_scheme'])
     local_scheme = callable_or_entrypoint(


Test patch:
diff --git a/testing/conftest.py b/testing/conftest.py
index 49a9d14..29e129c 100644
--- a/testing/conftest.py
+++ b/testing/conftest.py
@@ -52,14 +52,18 @@ class Wd(object):
         self(self.add_command)
         self.commit(reason=reason)
 
-    @property
-    def version(self):
+    def get_version(self, **kw):
         __tracebackhide__ = True
         from setuptools_scm import get_version
-        version = get_version(root=str(self.cwd))
+        version = get_version(root=str(self.cwd), **kw)
         print(version)
         return version
 
+    @property
+    def version(self):
+        __tracebackhide__ = True
+        return self.get_version()
+
 
 @pytest.yield_fixture(autouse=True)
 def debug_mode():
diff --git a/testing/test_basic_api.py b/testing/test_basic_api.py
index 5f9e1d6..4192f71 100644
--- a/testing/test_basic_api.py
+++ b/testing/test_basic_api.py
@@ -30,6 +30,9 @@ def test_version_from_pkginfo(wd):
     wd.write('PKG-INFO', 'Version: 0.1')
     assert wd.version == '0.1'
 
+    # replicate issue 167
+    assert wd.get_version(version_scheme="1.{0.distance}.0".format) == '0.1'
+
 
 def assert_root(monkeypatch, expected_root):
     """