Skip to content

Commit e155c6d

Browse files
committed
[BUGFIX] TOML config interpreting set_env=file|...
1 parent 65d404e commit e155c6d

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/tox/config/loader/toml/__init__.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from __future__ import annotations
22

3+
import inspect
34
import logging
45
from pathlib import Path
56
from typing import TYPE_CHECKING, Dict, Iterator, List, Mapping, TypeVar, cast
67

78
from tox.config.loader.api import ConfigLoadArgs, Loader, Override
9+
from tox.config.set_env import SetEnv
810
from tox.config.types import Command, EnvList
11+
from tox.report import HandledError
912

1013
from ._api import TomlTypes
1114
from ._replace import Unroll
@@ -55,15 +58,34 @@ def load_raw_from_root(self, path: str) -> TomlTypes:
5558

5659
def build( # noqa: PLR0913
5760
self,
58-
key: str, # noqa: ARG002
61+
key: str,
5962
of_type: type[_T],
6063
factory: Factory[_T],
6164
conf: Config | None,
6265
raw: TomlTypes,
6366
args: ConfigLoadArgs,
6467
) -> _T:
68+
delay_replace = inspect.isclass(of_type) and issubclass(of_type, SetEnv)
69+
70+
def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
71+
if conf is None:
72+
replaced = raw_ # no replacement supported in the core section
73+
else:
74+
reference_replacer = Unroll(conf, self, args)
75+
try:
76+
replaced = str(reference_replacer(raw_)) # do replacements
77+
except Exception as exception:
78+
if isinstance(exception, HandledError):
79+
raise
80+
msg = f"replace failed in {args_.env_name}.{key} with {exception!r}"
81+
raise HandledError(msg) from exception
82+
return replaced
83+
6584
exploded = Unroll(conf=conf, loader=self, args=args)(raw)
66-
return self.to(exploded, of_type, factory)
85+
refactoried = self.to(exploded, of_type, factory)
86+
if delay_replace:
87+
refactoried.use_replacer(replacer, args=args) # type: ignore[attr-defined] # issubclass(to_type, SetEnv)
88+
return refactoried
6789

6890
def found_keys(self) -> set[str]:
6991
return set(self.content.keys()) - self._unused_exclude

0 commit comments

Comments
 (0)