Repository: pypa/setuptools_scm

Issue:
setuptools_scm should support custom per-project SETUPTOOLS_SCM_PRETEND_VERSION var
When there's two project (one is dependency of another), both use `setuptools_scm` and `SETUPTOOLS_SCM_PRETEND_VERSION` env var is set, both of them use it for their version if installed from source.

I think maybe providing some prefixing/suffixing mechanism would improve this.

Comments:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 5f58599..dbce446 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -10,6 +10,7 @@ v4.2.0
 * enhance documentation
 * consider SOURCE_DATE_EPOCH for versioning
 * add a version_tuple to write_to templates
+* fix #321: add suppport for the ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${DISTRIBUTION_NAME}`` env var to target the pretend key
 * fix #142: clearly list supported scm
 
 
diff --git a/README.rst b/README.rst
index d402b7e..a826b1f 100644
--- a/README.rst
+++ b/README.rst
@@ -442,6 +442,15 @@ Environment variables
     its used as the primary source for the version number
     in which case it will be a unparsed string
 
+
+:SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${UPPERCASED_DIST_NAME}:
+    when defined and not empty,
+    its used as the primary source for the version number
+    in which case it will be a unparsed string
+
+    it takes precedence over ``SETUPTOOLS_SCM_PRETEND_VERSION``
+
+
 :SETUPTOOLS_SCM_DEBUG:
     when defined and not empty,
     a lot of debug information will be printed as part of ``setuptools_scm``
diff --git a/src/setuptools_scm/__init__.py b/src/setuptools_scm/__init__.py
index beb7168..47b9e44 100644
--- a/src/setuptools_scm/__init__.py
+++ b/src/setuptools_scm/__init__.py
@@ -11,11 +11,12 @@ from .config import (
     DEFAULT_LOCAL_SCHEME,
     DEFAULT_TAG_REGEX,
 )
-from .utils import function_has_arg, string_types
+from .utils import function_has_arg, string_types, trace
 from .version import format_version, meta
 from .discover import iter_matching_entrypoints
 
 PRETEND_KEY = "SETUPTOOLS_SCM_PRETEND_VERSION"
+PRETEND_KEY_NAMED = PRETEND_KEY + "_FOR_{name}"
 
 TEMPLATES = {
     ".py": """\
@@ -97,7 +98,18 @@ def dump_version(root, version, write_to, template=None):
 
 
 def _do_parse(config):
-    pretended = os.environ.get(PRETEND_KEY)
+
+    trace("dist name:", config.dist_name)
+    if config.dist_name is not None:
+        pretended = os.environ.get(
+            PRETEND_KEY_NAMED.format(name=config.dist_name.upper())
+        )
+    else:
+        pretended = None
+
+    if pretended is None:
+        pretended = os.environ.get(PRETEND_KEY)
+
     if pretended:
         # we use meta here since the pretended version
         # must adhere to the pep to begin with
@@ -144,6 +156,7 @@ def get_version(
     fallback_root=".",
     parse=None,
     git_describe_command=None,
+    dist_name=None,
 ):
     """
     If supplied, relative_to should be a file from which root may
diff --git a/src/setuptools_scm/config.py b/src/setuptools_scm/config.py
index e7f4d72..f0d9243 100644
--- a/src/setuptools_scm/config.py
+++ b/src/setuptools_scm/config.py
@@ -54,6 +54,7 @@ class Configuration(object):
         fallback_root=".",
         parse=None,
         git_describe_command=None,
+        dist_name=None,
     ):
         # TODO:
         self._relative_to = relative_to
@@ -70,6 +71,7 @@ class Configuration(object):
         self.parse = parse
         self.tag_regex = tag_regex
         self.git_describe_command = git_describe_command
+        self.dist_name = dist_name
 
     @property
     def fallback_root(self):
diff --git a/src/setuptools_scm/integration.py b/src/setuptools_scm/integration.py
index c623db7..ffd4521 100644
--- a/src/setuptools_scm/integration.py
+++ b/src/setuptools_scm/integration.py
@@ -1,7 +1,7 @@
 from pkg_resources import iter_entry_points
 
 from .version import _warn_if_setuptools_outdated
-from .utils import do, trace_exception
+from .utils import do, trace_exception, trace
 from . import _get_version, Configuration
 
 
@@ -13,7 +13,12 @@ def version_keyword(dist, keyword, value):
         value = {}
     if getattr(value, "__call__", None):
         value = value()
-    config = Configuration(**value)
+    assert (
+        "dist_name" not in value
+    ), "dist_name may not be specified in the setup keyword "
+    trace("dist name", dist, dist.name)
+    dist_name = dist.name if dist.name != 0 else None
+    config = Configuration(dist_name=dist_name, **value)
     dist.metadata.version = _get_version(config)
 
 
@@ -32,7 +37,7 @@ def find_files(path=""):
 
 def _args_from_toml(name="pyproject.toml"):
     # todo: more sensible config initialization
-    # move this elper back to config and unify it with the code from get_config
+    # move this helper back to config and unify it with the code from get_config
 
     with open(name) as strm:
         defn = __import__("toml").load(strm)


Test patch:
diff --git a/testing/test_integration.py b/testing/test_integration.py
index 68c3bfe..4564897 100644
--- a/testing/test_integration.py
+++ b/testing/test_integration.py
@@ -3,6 +3,7 @@ import sys
 import pytest
 
 from setuptools_scm.utils import do
+from setuptools_scm import PRETEND_KEY, PRETEND_KEY_NAMED
 
 
 @pytest.fixture
@@ -40,3 +41,23 @@ def test_pyproject_support_with_git(tmpdir, monkeypatch, wd):
     pkg.join("setup.py").write("__import__('setuptools').setup()")
     res = do((sys.executable, "setup.py", "--version"), pkg)
     assert res == "0.1.dev0"
+
+
+def test_pretend_version(tmpdir, monkeypatch, wd):
+    monkeypatch.setenv(PRETEND_KEY, "1.0.0")
+
+    assert wd.get_version() == "1.0.0"
+    assert wd.get_version(dist_name="ignored") == "1.0.0"
+
+
+def test_pretend_version_named(tmpdir, monkeypatch, wd):
+    monkeypatch.setenv(PRETEND_KEY_NAMED.format(name="test".upper()), "1.0.0")
+    monkeypatch.setenv(PRETEND_KEY_NAMED.format(name="test2".upper()), "2.0.0")
+    assert wd.get_version(dist_name="test") == "1.0.0"
+    assert wd.get_version(dist_name="test2") == "2.0.0"
+
+
+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"