Skip to content

Commit

Permalink
Fix dropped leading characters c from constraints' packages (#3250)
Browse files Browse the repository at this point in the history
`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.

Co-authored-by: Bernát Gábor <gaborjbernat@gmail.com>
  • Loading branch information
jugmac00 and gaborbernat committed Mar 24, 2024
1 parent 1f431a8 commit 51ce2bc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions 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`.
12 changes: 10 additions & 2 deletions src/tox/tox_env/python/pip/pip_install.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/tox_env/python/pip/test_pip_install.py
Expand Up @@ -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)
Expand Down

0 comments on commit 51ce2bc

Please sign in to comment.