Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary Future usage #3020

Merged
merged 1 commit into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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