Skip to content

Commit f83d2f3

Browse files
add basic documentation for overrides and split up overrides env var parsing
1 parent aba7788 commit f83d2f3

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

docs/overrides.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
# overrides
1+
# Overrides
2+
3+
## pretend versions
4+
5+
setuptools_scm provides a mechanism to override the version number build time.
6+
7+
the environment variable `SETUPTOOLS_SCM_PRETEND_VERSION` is used
8+
as the override source for the version number unparsed string.
9+
10+
to be specific about the package this applies for, one can use `SETUPTOOLS_SCM_PRETEND_VERSION_FOR_${NORMALIZED_DIST_NAME}`
11+
where the dist name normalization follows adapted PEP 503 semantics.
12+
13+
## config overrides
14+
15+
setuptools_scm parses the environment variable `SETUPTOOLS_SCM_OVERRIDES_FOR_${NORMALIZED_DIST_NAME}`
16+
as a toml inline map to override the configuration data from `pyproject.toml`.

src/setuptools_scm/_overrides.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import re
55
from typing import Any
6+
from typing import cast
7+
from typing import TypedDict
68

79
from . import _config
810
from . import _log
@@ -18,6 +20,7 @@
1820
def read_named_env(
1921
*, tool: str = "SETUPTOOLS_SCM", name: str, dist_name: str | None
2022
) -> str | None:
23+
""" """
2124
if dist_name is not None:
2225
# Normalize the dist name as per PEP 503.
2326
normalized_dist_name = re.sub(r"[-_.]+", "-", dist_name)
@@ -48,13 +51,23 @@ def _read_pretended_version_for(
4851
return None
4952

5053

54+
class _CheatTomlData(TypedDict):
55+
cheat: dict[str, Any]
56+
57+
58+
def load_toml_or_inline_map(data: str | None) -> dict[str, Any]:
59+
"""
60+
load toml data - with a special hack if only a inline map is given
61+
"""
62+
if not data:
63+
return {}
64+
elif data[0] == "{":
65+
data = "cheat=" + data
66+
loaded: _CheatTomlData = cast(_CheatTomlData, lazy_toml_load(data))
67+
return loaded["cheat"]
68+
return lazy_toml_load(data)
69+
70+
5171
def read_toml_overrides(dist_name: str | None) -> dict[str, Any]:
5272
data = read_named_env(name="OVERRIDES", dist_name=dist_name)
53-
if data:
54-
if data[0] == "{":
55-
data = "cheat=" + data
56-
loaded = lazy_toml_load(data)
57-
return loaded["cheat"] # type: ignore[no-any-return]
58-
return lazy_toml_load(data)
59-
else:
60-
return {}
73+
return load_toml_or_inline_map(data)

0 commit comments

Comments
 (0)