From 6064f5581d8cc9ff6bc3562850ca536a1e3d2880 Mon Sep 17 00:00:00 2001 From: Ben Rowland Date: Thu, 18 Apr 2024 11:39:27 +0100 Subject: [PATCH] Cache env config This commit adds a new cache to the main config object, specifically for env config. When env config is retrieved via Config.get_env(), it is not in general possible to determine whether the env is a packaging environment or not. Because retrieving standard section config needs to know which "base" section to inherit from, and this is different for test envs and packaging envs, we require a separate way to retrieve previously created env config. At creation time we do reliably know whether this is a packaging or run env, and we want that to persist (if it changes, we call clear_env() and recreate it with the new type). --- src/tox/config/main.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/tox/config/main.py b/src/tox/config/main.py index a409aaa0a..1a7193d38 100644 --- a/src/tox/config/main.py +++ b/src/tox/config/main.py @@ -41,6 +41,7 @@ def __init__( # noqa: PLR0913 self._src = config_source self._key_to_conf_set: dict[tuple[str, str, str], ConfigSet] = OrderedDict() + self._key_to_env_conf: dict[str, EnvConfigSet] = {} self._core_set: CoreConfigSet | None = None self.memory_seed_loaders: defaultdict[str, list[MemoryLoader]] = defaultdict(list) @@ -159,18 +160,21 @@ def get_env( :param loaders: loaders to use for this configuration (only used for creation) :return: the tox environments config """ - section, base_test, base_pkg = self._src.get_tox_env_section(item) - return self.get_section_config( - section, - base=base_pkg if package else base_test, - of_type=EnvConfigSet, - for_env=item, - loaders=loaders, - ) + if item not in self._key_to_env_conf: + section, base_test, base_pkg = self._src.get_tox_env_section(item) + self._key_to_env_conf[item] = self.get_section_config( + section, + base=base_pkg if package else base_test, + of_type=EnvConfigSet, + for_env=item, + loaders=loaders, + ) + return self._key_to_env_conf[item] def clear_env(self, name: str) -> None: section, _, __ = self._src.get_tox_env_section(name) self._key_to_conf_set = {k: v for k, v in self._key_to_conf_set.items() if k[0] == section.key and k[1] == name} + self._key_to_env_conf.pop(name) ___all__ = [