Skip to content

Commit

Permalink
Remove unnecessary Future usage (#3020)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
living180 committed May 28, 2023
1 parent accbc84 commit 55cc1ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
1 change: 1 addition & 0 deletions docs/changelog/3020.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove unnecessary usage of ``Future`` from ``tox.config.api.Loader.load()``.
19 changes: 6 additions & 13 deletions src/tox/config/loader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
17 changes: 7 additions & 10 deletions src/tox/config/loader/ini/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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())
Expand Down

0 comments on commit 55cc1ea

Please sign in to comment.