diff --git a/docs/changelog/3020.misc.rst b/docs/changelog/3020.misc.rst new file mode 100644 index 000000000..517125554 --- /dev/null +++ b/docs/changelog/3020.misc.rst @@ -0,0 +1 @@ +Remove unnecessary usage of ``Future`` from ``tox.config.api.Loader.load()``. diff --git a/src/tox/config/loader/api.py b/src/tox/config/loader/api.py index fcac1f839..a0786458c 100644 --- a/src/tox/config/loader/api.py +++ b/src/tox/config/loader/api.py @@ -2,9 +2,7 @@ from abc import abstractmethod from argparse import ArgumentTypeError -from concurrent.futures import Future -from contextlib import contextmanager -from typing import TYPE_CHECKING, Any, Generator, List, Mapping, TypeVar +from typing import TYPE_CHECKING, Any, List, Mapping, TypeVar from tox.plugin import impl @@ -122,22 +120,17 @@ def load( if key in self.overrides: return _STR_CONVERT.to(self.overrides[key].value, of_type, factory) raw = self.load_raw(key, conf, args.env_name) - future: Future[V] = Future() - with self.build(future, key, of_type, conf, raw, args) as prepared: - converted = self.to(prepared, of_type, factory) - future.set_result(converted) - return converted + return self.build(key, of_type, factory, conf, raw, args) - @contextmanager def build( self, - future: Future[V], # noqa: U100 key: str, # noqa: U100 - of_type: type[V], # noqa: U100 + of_type: type[V], + factory: Factory[V], conf: Config | None, # noqa: U100 raw: T, args: ConfigLoadArgs, # noqa: U100 - ) -> Generator[T, None, None]: + ) -> V: """ Materialize the raw configuration value from the loader. @@ -148,7 +141,7 @@ def build( :param raw: the raw value :param args: env args """ - yield raw + return self.to(raw, of_type, factory) @impl diff --git a/src/tox/config/loader/ini/__init__.py b/src/tox/config/loader/ini/__init__.py index 270dc13bb..1dfb8be09 100644 --- a/src/tox/config/loader/ini/__init__.py +++ b/src/tox/config/loader/ini/__init__.py @@ -2,12 +2,11 @@ import inspect import re -from concurrent.futures import Future from configparser import ConfigParser, SectionProxy -from contextlib import contextmanager -from typing import TYPE_CHECKING, Generator, TypeVar +from typing import TYPE_CHECKING, TypeVar from tox.config.loader.api import ConfigLoadArgs, Loader, Override +from tox.config.loader.convert import Factory from tox.config.loader.ini.factor import filter_for_env from tox.config.loader.ini.replace import replace from tox.config.loader.section import Section @@ -57,16 +56,15 @@ def process_raw(conf: Config | None, env_name: str | None, value: str) -> str: collapsed = factor_filtered.replace("\r", "").replace("\\\n", "") # collapse explicit new-line escape return collapsed - @contextmanager def build( self, - future: Future[V], key: str, of_type: type[V], + factory: Factory[V], conf: Config | None, raw: str, args: ConfigLoadArgs, - ) -> Generator[str, None, None]: + ) -> V: delay_replace = inspect.isclass(of_type) and issubclass(of_type, SetEnv) def replacer(raw_: str, args_: ConfigLoadArgs) -> str: @@ -83,12 +81,11 @@ def replacer(raw_: str, args_: ConfigLoadArgs) -> str: raise HandledError(msg) from exception return replaced - if not delay_replace: - raw = replacer(raw, args) - yield raw + prepared = replacer(raw, args) if not delay_replace else raw + converted = self.to(prepared, of_type, factory) if delay_replace: - converted = future.result() converted.use_replacer(replacer, args) # type: ignore[attr-defined] # this can be only set_env that has it + return converted def found_keys(self) -> set[str]: return set(self._section_proxy.keys())