Skip to content

Commit 55cc1ea

Browse files
authored
Remove unnecessary Future usage (#3020)
The tox.config.api.Loader.load() method was manually creating a Future and passing it to the .build() context manager method, but no executor was involved and nothing was asynchronous. This commit moves the call to self.to() from the .load() method to the .build() method. This allows dropping the Future and changing .build() from a context manager to a regular method.
1 parent accbc84 commit 55cc1ea

File tree

3 files changed

+14
-23
lines changed

3 files changed

+14
-23
lines changed

docs/changelog/3020.misc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove unnecessary usage of ``Future`` from ``tox.config.api.Loader.load()``.

src/tox/config/loader/api.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
from abc import abstractmethod
44
from argparse import ArgumentTypeError
5-
from concurrent.futures import Future
6-
from contextlib import contextmanager
7-
from typing import TYPE_CHECKING, Any, Generator, List, Mapping, TypeVar
5+
from typing import TYPE_CHECKING, Any, List, Mapping, TypeVar
86

97
from tox.plugin import impl
108

@@ -122,22 +120,17 @@ def load(
122120
if key in self.overrides:
123121
return _STR_CONVERT.to(self.overrides[key].value, of_type, factory)
124122
raw = self.load_raw(key, conf, args.env_name)
125-
future: Future[V] = Future()
126-
with self.build(future, key, of_type, conf, raw, args) as prepared:
127-
converted = self.to(prepared, of_type, factory)
128-
future.set_result(converted)
129-
return converted
123+
return self.build(key, of_type, factory, conf, raw, args)
130124

131-
@contextmanager
132125
def build(
133126
self,
134-
future: Future[V], # noqa: U100
135127
key: str, # noqa: U100
136-
of_type: type[V], # noqa: U100
128+
of_type: type[V],
129+
factory: Factory[V],
137130
conf: Config | None, # noqa: U100
138131
raw: T,
139132
args: ConfigLoadArgs, # noqa: U100
140-
) -> Generator[T, None, None]:
133+
) -> V:
141134
"""
142135
Materialize the raw configuration value from the loader.
143136
@@ -148,7 +141,7 @@ def build(
148141
:param raw: the raw value
149142
:param args: env args
150143
"""
151-
yield raw
144+
return self.to(raw, of_type, factory)
152145

153146

154147
@impl

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import inspect
44
import re
5-
from concurrent.futures import Future
65
from configparser import ConfigParser, SectionProxy
7-
from contextlib import contextmanager
8-
from typing import TYPE_CHECKING, Generator, TypeVar
6+
from typing import TYPE_CHECKING, TypeVar
97

108
from tox.config.loader.api import ConfigLoadArgs, Loader, Override
9+
from tox.config.loader.convert import Factory
1110
from tox.config.loader.ini.factor import filter_for_env
1211
from tox.config.loader.ini.replace import replace
1312
from tox.config.loader.section import Section
@@ -57,16 +56,15 @@ def process_raw(conf: Config | None, env_name: str | None, value: str) -> str:
5756
collapsed = factor_filtered.replace("\r", "").replace("\\\n", "") # collapse explicit new-line escape
5857
return collapsed
5958

60-
@contextmanager
6159
def build(
6260
self,
63-
future: Future[V],
6461
key: str,
6562
of_type: type[V],
63+
factory: Factory[V],
6664
conf: Config | None,
6765
raw: str,
6866
args: ConfigLoadArgs,
69-
) -> Generator[str, None, None]:
67+
) -> V:
7068
delay_replace = inspect.isclass(of_type) and issubclass(of_type, SetEnv)
7169

7270
def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
@@ -83,12 +81,11 @@ def replacer(raw_: str, args_: ConfigLoadArgs) -> str:
8381
raise HandledError(msg) from exception
8482
return replaced
8583

86-
if not delay_replace:
87-
raw = replacer(raw, args)
88-
yield raw
84+
prepared = replacer(raw, args) if not delay_replace else raw
85+
converted = self.to(prepared, of_type, factory)
8986
if delay_replace:
90-
converted = future.result()
9187
converted.use_replacer(replacer, args) # type: ignore[attr-defined] # this can be only set_env that has it
88+
return converted
9289

9390
def found_keys(self) -> set[str]:
9491
return set(self._section_proxy.keys())

0 commit comments

Comments
 (0)