diff options
| author | Miroslav Šedivý <6774676+eumiro@users.noreply.github.com> | 2021-04-06 08:02:41 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-06 07:02:41 +0100 |
| commit | 00aaba2ab7ea71c13076f3ed72d9d7e6aefa72b7 (patch) | |
| tree | d01d0ce5a21418c8ba365edc3cdaee83cca6fcdb /src | |
| parent | 3aec6725d11f4017ec7164261c470d371c7cfbee (diff) | |
| download | tox-git-00aaba2ab7ea71c13076f3ed72d9d7e6aefa72b7.tar.gz | |
Refactor: Simplify and clean up the code (#1997)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/tox/config/cli/parse.py | 5 | ||||
| -rw-r--r-- | src/tox/config/cli/parser.py | 2 | ||||
| -rw-r--r-- | src/tox/config/loader/ini/factor.py | 10 | ||||
| -rw-r--r-- | src/tox/config/loader/str_convert.py | 20 | ||||
| -rw-r--r-- | src/tox/config/of_type.py | 17 | ||||
| -rw-r--r-- | src/tox/config/set_env.py | 18 | ||||
| -rw-r--r-- | src/tox/provision.py | 7 | ||||
| -rw-r--r-- | src/tox/pytest.py | 2 | ||||
| -rw-r--r-- | src/tox/session/cmd/run/parallel.py | 4 | ||||
| -rw-r--r-- | src/tox/tox_env/package.py | 2 | ||||
| -rw-r--r-- | src/tox/tox_env/python/pip/req_file.py | 2 | ||||
| -rw-r--r-- | src/tox/tox_env/python/virtual_env/package/api.py | 6 |
12 files changed, 37 insertions, 58 deletions
diff --git a/src/tox/config/cli/parse.py b/src/tox/config/cli/parse.py index 63bd6826..73a332d9 100644 --- a/src/tox/config/cli/parse.py +++ b/src/tox/config/cli/parse.py @@ -16,10 +16,11 @@ def get_options(*args: str) -> Tuple[Parsed, Handlers, Optional[Sequence[str]], pos_args: Optional[Tuple[str, ...]] = None try: # remove positional arguments passed to parser if specified, they are pulled directly from sys.argv pos_arg_at = args.index("--") - pos_args = tuple(args[pos_arg_at + 1 :]) - args = args[:pos_arg_at] except ValueError: pass + else: + pos_args = tuple(args[pos_arg_at + 1 :]) + args = args[:pos_arg_at] guess_verbosity, log_handler, source = _get_base(args) parsed, cmd_handlers = _get_all(args) diff --git a/src/tox/config/cli/parser.py b/src/tox/config/cli/parser.py index ac27cac9..df8d6163 100644 --- a/src/tox/config/cli/parser.py +++ b/src/tox/config/cli/parser.py @@ -182,7 +182,7 @@ class ToxParser(ArgumentParserWithEnvAndConfig): def add_argument(self, *args: str, of_type: Optional[Type[Any]] = None, **kwargs: Any) -> Action: result = super().add_argument(*args, **kwargs) - if self.of_cmd is None and (result.dest not in ("help",)): + if self.of_cmd is None and result.dest != "help": self._arguments.append((args, of_type, kwargs)) if hasattr(self, "_cmd") and self._cmd is not None and hasattr(self._cmd, "choices"): for parser in {id(v): v for k, v in self._cmd.choices.items()}.values(): diff --git a/src/tox/config/loader/ini/factor.py b/src/tox/config/loader/ini/factor.py index d1e956e4..281dbf4d 100644 --- a/src/tox/config/loader/ini/factor.py +++ b/src/tox/config/loader/ini/factor.py @@ -1,16 +1,14 @@ """ Expand tox factor expressions to tox environment list. """ -import itertools import re +from itertools import chain, groupby, product from typing import Iterator, List, Optional, Tuple def filter_for_env(value: str, name: Optional[str]) -> str: current = ( - set(itertools.chain.from_iterable([(i for i, _ in a) for a in find_factor_groups(name)])) - if name is not None - else set() + set(chain.from_iterable([(i for i, _ in a) for a in find_factor_groups(name)])) if name is not None else set() ) overall = [] for factors, content in expand_factors(value): @@ -68,12 +66,12 @@ def find_factor_groups(value: str) -> Iterator[List[Tuple[str, bool]]]: def expand_env_with_negation(value: str) -> Iterator[str]: """transform '{py,!pi}-{a,b},c' to ['py-a', 'py-b', '!pi-a', '!pi-b', 'c']""" - for key, group in itertools.groupby(re.split(r"((?:{[^}]+})+)|,", value), key=bool): + for key, group in groupby(re.split(r"((?:{[^}]+})+)|,", value), key=bool): if key: group_str = "".join(group).strip() elements = re.split(r"{([^}]+)}", group_str) parts = [re.sub(r"\s+", "", elem).split(",") for elem in elements] - for variant in itertools.product(*parts): + for variant in product(*parts): variant_str = "".join(variant) yield variant_str diff --git a/src/tox/config/loader/str_convert.py b/src/tox/config/loader/str_convert.py index 8524afaf..a973d7e4 100644 --- a/src/tox/config/loader/str_convert.py +++ b/src/tox/config/loader/str_convert.py @@ -37,16 +37,12 @@ class StrConvert(Convert[str]): @staticmethod def to_dict(value: str, of_type: Tuple[Type[Any], Type[Any]]) -> Iterator[Tuple[str, str]]: # noqa: U100 for row in value.split("\n"): - row = row.strip() - if row: - try: - at = row.index("=") - except ValueError as exc: - raise TypeError(f"dictionary lines must be of form key=value, found {row}") from exc + if row.strip(): + key, sep, value = row.partition("=") + if sep: + yield key.strip(), value.strip() else: - key = row[:at].strip() - value = row[at + 1 :].strip() - yield key, value + raise TypeError(f"dictionary lines must be of form key=value, found {row!r}") @staticmethod def to_command(value: str) -> Command: @@ -57,11 +53,7 @@ class StrConvert(Convert[str]): args: List[str] = [] for arg in splitter: # on Windows quoted arguments will remain quoted, strip it - if ( - len(arg) > 1 - and (arg.startswith('"') and arg.endswith('"')) - or (arg.startswith("'") and arg.endswith("'")) - ): + if len(arg) > 1 and arg[0] == arg[-1] and arg.startswith(("'", '"')): arg = arg[1:-1] args.append(arg) else: diff --git a/src/tox/config/of_type.py b/src/tox/config/of_type.py index 82c184c9..b555e887 100644 --- a/src/tox/config/of_type.py +++ b/src/tox/config/of_type.py @@ -2,6 +2,7 @@ Group together configuration values that belong together (such as base tox configuration, tox environment configs) """ from abc import ABC, abstractmethod +from itertools import product from typing import TYPE_CHECKING, Any, Callable, Generic, Iterable, List, Mapping, Optional, Type, TypeVar, Union, cast from tox.config.loader.api import Loader @@ -92,16 +93,12 @@ class ConfigDynamicDefinition(ConfigDefinition[T]): chain: List[str], ) -> T: if self._cache is _PLACE_HOLDER: - found = False - for key in self.keys: - for loader in loaders: - try: - value = loader.load(key, self.of_type, self.kwargs, conf, self.env_name, chain) - found = True - except KeyError: - continue - break - if found: + for key, loader in product(self.keys, loaders): + try: + value = loader.load(key, self.of_type, self.kwargs, conf, self.env_name, chain) + except KeyError: + continue + else: break else: value = self.default(conf, self.env_name) if callable(self.default) else self.default diff --git a/src/tox/config/set_env.py b/src/tox/config/set_env.py index 8a4d794a..ce836516 100644 --- a/src/tox/config/set_env.py +++ b/src/tox/config/set_env.py @@ -6,12 +6,11 @@ Replacer = Callable[[str, List[str]], str] class SetEnv: def __init__(self, raw: str) -> None: self.replacer: Replacer = lambda s, c: s - lines = raw.splitlines() self._later: List[str] = [] self._raw: Dict[str, str] = {} from .loader.ini.replace import find_replace_part - for line in lines: + for line in raw.splitlines(): if line.strip(): try: key, value = self._extract_key_value(line) @@ -29,12 +28,11 @@ class SetEnv: @staticmethod def _extract_key_value(line: str) -> Tuple[str, str]: - try: - at = line.index("=") - except ValueError: + key, sep, value = line.partition("=") + if sep: + return key.strip(), value.strip() + else: raise ValueError(f"invalid line {line!r} in set_env") - key, value = line[:at], line[at + 1 :] - return key.strip(), value.strip() def load(self, item: str, chain: Optional[List[str]] = None) -> str: if chain is None: @@ -57,11 +55,7 @@ class SetEnv: while self._later: line = self._later.pop(0) expanded_line = self.replacer(line, []) - sub_raw = {} - for sub_line in expanded_line.splitlines(): - if sub_line: - key, value = self._extract_key_value(sub_line) - sub_raw[key] = value + sub_raw = dict(self._extract_key_value(sub_line) for sub_line in expanded_line.splitlines() if sub_line) self._raw.update(sub_raw) yield from sub_raw.keys() diff --git a/src/tox/provision.py b/src/tox/provision.py index 94d23fca..ff2ac5fd 100644 --- a/src/tox/provision.py +++ b/src/tox/provision.py @@ -81,10 +81,11 @@ def provision(state: State) -> Union[int, bool]: package_name = canonicalize_name(package.name) try: dist = distribution(package_name) # type: ignore[no-untyped-call] - if not package.specifier.contains(dist.version, prereleases=True): - missing.append((package, dist.version)) except PackageNotFoundError: missing.append((package, "N/A")) + else: + if not package.specifier.contains(dist.version, prereleases=True): + missing.append((package, dist.version)) if not missing: return False deps = ", ".join(f"{p} ({ver})" for p, ver in missing) @@ -107,7 +108,7 @@ def run_provision(deps: List[Requirement], state: State) -> int: # noqa tox_env = cast(PythonRun, state.tox_env(provision_tox_env)) env_python = tox_env.env_python() logging.info("will run in a automatically provisioned python environment under %s", env_python) - recreate = state.options.no_recreate_provision is False if state.options.recreate else False + recreate = state.options.recreate and not state.options.no_recreate_provision try: tox_env.setup(recreate=recreate) except Skip as exception: diff --git a/src/tox/pytest.py b/src/tox/pytest.py index f7c84502..8ce123f6 100644 --- a/src/tox/pytest.py +++ b/src/tox/pytest.py @@ -461,7 +461,7 @@ class IndexServer: self.path = path self.host, self.port = "localhost", _find_free_port() - self._passwd = "".join(random.choice(string.ascii_letters) for _ in range(8)) + self._passwd = "".join(random.choices(string.ascii_letters, k=8)) def _exe(name: str) -> str: return str(Path(scripts_dir) / f"{name}{'.exe' if IS_WIN else ''}") diff --git a/src/tox/session/cmd/run/parallel.py b/src/tox/session/cmd/run/parallel.py index a41c6d9d..cf0e53d4 100644 --- a/src/tox/session/cmd/run/parallel.py +++ b/src/tox/session/cmd/run/parallel.py @@ -36,9 +36,9 @@ def parse_num_processes(str_value: str) -> Optional[int]: try: value = int(str_value) except ValueError as exc: - raise ArgumentTypeError(f"value must be a positive number, is {str_value}") from exc + raise ArgumentTypeError(f"value must be a positive number, is {str_value!r}") from exc if value < 0: - raise ArgumentTypeError(f"value must be positive, is {value}") + raise ArgumentTypeError(f"value must be positive, is {value!r}") return value diff --git a/src/tox/tox_env/package.py b/src/tox/tox_env/package.py index 3d1b9ea0..daf7dbb8 100644 --- a/src/tox/tox_env/package.py +++ b/src/tox/tox_env/package.py @@ -31,7 +31,7 @@ class PackageToxEnv(ToxEnv, ABC): self, conf: EnvConfigSet, core: CoreConfigSet, options: "Parsed", journal: EnvJournal, log_handler: ToxHandler ) -> None: super().__init__(conf, core, options, journal, log_handler) - self.recreate_package = options.no_recreate_pkg is False if options.recreate else False + self.recreate_package = options.recreate and not options.no_recreate_pkg self._envs: Set[str] = set() self._lock = Lock() diff --git a/src/tox/tox_env/python/pip/req_file.py b/src/tox/tox_env/python/pip/req_file.py index 5f10cc9d..62d05740 100644 --- a/src/tox/tox_env/python/pip/req_file.py +++ b/src/tox/tox_env/python/pip/req_file.py @@ -19,7 +19,7 @@ def is_url(name: str) -> bool: def get_url_scheme(url: str) -> Optional[str]: - return None if ":" not in url else url.split(":", 1)[0].lower() + return None if ":" not in url else url.partition(":")[0].lower() NO_ARG = { diff --git a/src/tox/tox_env/python/virtual_env/package/api.py b/src/tox/tox_env/python/virtual_env/package/api.py index 8e444244..e2b1095d 100644 --- a/src/tox/tox_env/python/virtual_env/package/api.py +++ b/src/tox/tox_env/python/virtual_env/package/api.py @@ -62,11 +62,7 @@ class ToxCmdStatus(CmdStatus): if status.exit_code is not None: # pragma: no branch return True # pragma: no cover # 2. the backend output reported back that our command is done - content = status.out - at = content.rfind(b"Backend: Wrote response ") - if at != -1 and content.find(b"\n", at) != -1: - return True - return False + return b"\n" in status.out.rpartition(b"Backend: Wrote response ")[0] def out_err(self) -> Tuple[str, str]: status = self._execute_status |
