From 9b760308653c5575fcb970f4ae0fa7e49b9344a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Gmach?= Date: Sun, 24 Mar 2024 11:24:20 +0100 Subject: [PATCH] Fix dropped leading characters `c` from constraints' packages `lstrip` takes a set, not a string, so `.lstrip("-c ")` did not work as intended. Until we only support Python 3.9 or higher, we need to use a custom function to remove a prefix. With Python 3.9+ we can use the builtin `.removeprefix`. This fixes #3247. --- docs/changelog/3247.bugfix.rst | 1 + src/tox/tox_env/python/pip/pip_install.py | 12 ++++++++++-- tests/tox_env/python/pip/test_pip_install.py | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 docs/changelog/3247.bugfix.rst diff --git a/docs/changelog/3247.bugfix.rst b/docs/changelog/3247.bugfix.rst new file mode 100644 index 0000000000..8db01b08bb --- /dev/null +++ b/docs/changelog/3247.bugfix.rst @@ -0,0 +1 @@ +Fix issue that the leading character `c` was dropped from packages in constraints files - by :user:`jugmac00`. diff --git a/src/tox/tox_env/python/pip/pip_install.py b/src/tox/tox_env/python/pip/pip_install.py index 4b75c2b3cc..f09d98d031 100644 --- a/src/tox/tox_env/python/pip/pip_install.py +++ b/src/tox/tox_env/python/pip/pip_install.py @@ -107,7 +107,7 @@ def constrain_package_deps(self) -> bool: def use_frozen_constraints(self) -> bool: return bool(self._env.conf["use_frozen_constraints"]) - def _install_requirement_file(self, arguments: PythonDeps, section: str, of_type: str) -> None: + def _install_requirement_file(self, arguments: PythonDeps, section: str, of_type: str) -> None: # noqa: C901 try: new_options, new_reqs = arguments.unroll() except ValueError as exception: @@ -145,7 +145,15 @@ def _install_requirement_file(self, arguments: PythonDeps, section: str, of_type if args: # pragma: no branch self._execute_installer(args, of_type) if self.constrain_package_deps and not self.use_frozen_constraints: - combined_constraints = new_requirements + [c.lstrip("-c ") for c in new_constraints] + # when we drop Python 3.8 we can use the builtin `.removeprefix` + def remove_prefix(text: str, prefix: str) -> str: + if text.startswith(prefix): + return text[len(prefix) :] + return text + + combined_constraints = new_requirements + [ + remove_prefix(text=c, prefix="-c ") for c in new_constraints + ] self.constraints_file().write_text("\n".join(combined_constraints)) @staticmethod diff --git a/tests/tox_env/python/pip/test_pip_install.py b/tests/tox_env/python/pip/test_pip_install.py index 9f90a0f022..ca5aa6ca53 100644 --- a/tests/tox_env/python/pip/test_pip_install.py +++ b/tests/tox_env/python/pip/test_pip_install.py @@ -309,8 +309,8 @@ def constrained_mock_project( "build.py": (demo_pkg_inline / "build.py").read_text(), } exp_constraints: list[str] = [] - requirement = "foo==1.2.3" - constraint = "foo<2" + requirement = "coo==1.2.3" + constraint = "coo<2" if request.param == "explicit": deps = requirement exp_constraints.append(requirement)