Skip to content

Commit 6754be9

Browse files
Merge pull request #848 from pelson/fix/normalize_override_env_name
Normalize the dist-name, as per PEP503, when looking for overrides
2 parents 4751f74 + 28c8d52 commit 6754be9

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ breaking
1515
and where hiding dirty states that are now explicitly dirty
1616
* depend on later importlib for the full selectable api
1717
* move setuptools integration code to private sub-package
18+
* use normalized dist names for the SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${DIST_NAME}
1819

1920
features
2021
--------

README.rst

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Note that running this Dockerfile requires docker with BuildKit enabled
262262
`[docs] <https://github.com/moby/buildkit/blob/v0.8.3/frontend/dockerfile/docs/syntax.md>`_.
263263

264264
To avoid BuildKit and mounting of the .git folder altogether, one can also pass the desired
265-
version as a build argument. Note that ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${UPPERCASED_DIST_NAME}``
265+
version as a build argument. Note that ``SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${NORMALIZED_DIST_NAME}``
266266
is preferred over ``SETUPTOOLS_SCM_PRETEND_VERSION``.
267267

268268

@@ -500,12 +500,15 @@ Environment variables
500500
:SETUPTOOLS_SCM_PRETEND_VERSION:
501501
when defined and not empty,
502502
its used as the primary source for the version number
503-
in which case it will be a unparsed string
503+
in which case it will be an unparsed string
504504

505-
:SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${UPPERCASED_DIST_NAME}:
505+
:SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${NORMALIZED_DIST_NAME}:
506506
when defined and not empty,
507507
its used as the primary source for the version number
508-
in which case it will be a unparsed string
508+
in which case it will be an unparsed string
509+
510+
the dist name normalization follows adapted PEP-503 semantics, with one or
511+
more of ".-_" being replaced by a single "_", and the name being upper-cased
509512

510513
it takes precedence over ``SETUPTOOLS_SCM_PRETEND_VERSION``
511514

src/setuptools_scm/_overrides.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import re
45
from typing import Any
56

67
from . import _config
@@ -18,7 +19,10 @@ def read_named_env(
1819
*, tool: str = "SETUPTOOLS_SCM", name: str, dist_name: str | None
1920
) -> str | None:
2021
if dist_name is not None:
21-
val = os.environ.get(f"{tool}_{name}_FOR_{dist_name.upper()}")
22+
# Normalize the dist name as per PEP 503.
23+
normalized_dist_name = re.sub(r"[-_.]+", "-", dist_name)
24+
env_var_dist_name = normalized_dist_name.replace("-", "_").upper()
25+
val = os.environ.get(f"{tool}_{name}_FOR_{env_var_dist_name}")
2226
if val is not None:
2327
return val
2428
return os.environ.get(f"{tool}_{name}")

testing/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def test_config_overrides(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> No
8484
[tool.setuptools_scm]
8585
root = "."
8686
[project]
87-
name = "test_a"
87+
name = "teSt-.a"
8888
"""
8989
),
9090
encoding="utf-8",

0 commit comments

Comments
 (0)