From b8640b110153581e01d55aaa83904a0773056ec8 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Wed, 17 May 2023 16:39:03 +0200 Subject: [PATCH 1/2] Normalize the dist-name, as per PEP503, when treating the dist name in environment variables for overrides. Closes #829. --- src/setuptools_scm/_overrides.py | 6 +++++- testing/test_config.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/setuptools_scm/_overrides.py b/src/setuptools_scm/_overrides.py index 62793779..05d4b884 100644 --- a/src/setuptools_scm/_overrides.py +++ b/src/setuptools_scm/_overrides.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import re from typing import Any from . import _config @@ -18,7 +19,10 @@ def read_named_env( *, tool: str = "SETUPTOOLS_SCM", name: str, dist_name: str | None ) -> str | None: if dist_name is not None: - val = os.environ.get(f"{tool}_{name}_FOR_{dist_name.upper()}") + # Normalize the dist name as per PEP 503. + normalized_dist_name = re.sub(r"[-_.]+", "-", dist_name) + env_var_dist_name = normalized_dist_name.replace("-", "_").upper() + val = os.environ.get(f"{tool}_{name}_FOR_{env_var_dist_name}") if val is not None: return val return os.environ.get(f"{tool}_{name}") diff --git a/testing/test_config.py b/testing/test_config.py index 211a853c..7246a79b 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -84,7 +84,7 @@ def test_config_overrides(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> No [tool.setuptools_scm] root = "." [project] - name = "test_a" + name = "teSt-.a" """ ), encoding="utf-8", From 28c8d52582c9b6eaf982917a758204af862f1d79 Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 19 May 2023 10:30:35 +0200 Subject: [PATCH 2/2] Provide more documentation on the SETUPTOOLS_SCM_PRETEND_VERSION_FOR_ variable --- CHANGELOG.rst | 1 + README.rst | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 1b2d8065..51d74cf8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -15,6 +15,7 @@ breaking and where hiding dirty states that are now explicitly dirty * depend on later importlib for the full selectable api * move setuptools integration code to private sub-package +* use normalized dist names for the SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${DIST_NAME} features -------- diff --git a/README.rst b/README.rst index bbfd90e2..a48309c1 100644 --- a/README.rst +++ b/README.rst @@ -262,7 +262,7 @@ Note that running this Dockerfile requires docker with BuildKit enabled `[docs] `_. To avoid BuildKit and mounting of the .git folder altogether, one can also pass the desired -version as a build argument. Note that ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${UPPERCASED_DIST_NAME}`` +version as a build argument. Note that ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${NORMALIZED_DIST_NAME}`` is preferred over ``SETUPTOOLS_SCM_PRETEND_VERSION``. @@ -500,12 +500,15 @@ Environment variables :SETUPTOOLS_SCM_PRETEND_VERSION: when defined and not empty, its used as the primary source for the version number - in which case it will be a unparsed string + in which case it will be an unparsed string -:SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${UPPERCASED_DIST_NAME}: +:SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${NORMALIZED_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 + in which case it will be an unparsed string + + the dist name normalization follows adapted PEP-503 semantics, with one or + more of ".-_" being replaced by a single "_", and the name being upper-cased it takes precedence over ``SETUPTOOLS_SCM_PRETEND_VERSION``