Skip to content

Commit 51ce2bc

Browse files
Fix dropped leading characters c from constraints' packages (#3250)
`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 <[email protected]>
1 parent 1f431a8 commit 51ce2bc

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

docs/changelog/3247.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issue that the leading character ``c`` was dropped from packages in constraints files - by :user:`jugmac00`.

src/tox/tox_env/python/pip/pip_install.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def constrain_package_deps(self) -> bool:
107107
def use_frozen_constraints(self) -> bool:
108108
return bool(self._env.conf["use_frozen_constraints"])
109109

110-
def _install_requirement_file(self, arguments: PythonDeps, section: str, of_type: str) -> None:
110+
def _install_requirement_file(self, arguments: PythonDeps, section: str, of_type: str) -> None: # noqa: C901
111111
try:
112112
new_options, new_reqs = arguments.unroll()
113113
except ValueError as exception:
@@ -145,7 +145,15 @@ def _install_requirement_file(self, arguments: PythonDeps, section: str, of_type
145145
if args: # pragma: no branch
146146
self._execute_installer(args, of_type)
147147
if self.constrain_package_deps and not self.use_frozen_constraints:
148-
combined_constraints = new_requirements + [c.lstrip("-c ") for c in new_constraints]
148+
# when we drop Python 3.8 we can use the builtin `.removeprefix`
149+
def remove_prefix(text: str, prefix: str) -> str:
150+
if text.startswith(prefix):
151+
return text[len(prefix) :]
152+
return text
153+
154+
combined_constraints = new_requirements + [
155+
remove_prefix(text=c, prefix="-c ") for c in new_constraints
156+
]
149157
self.constraints_file().write_text("\n".join(combined_constraints))
150158

151159
@staticmethod

tests/tox_env/python/pip/test_pip_install.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ def constrained_mock_project(
309309
"build.py": (demo_pkg_inline / "build.py").read_text(),
310310
}
311311
exp_constraints: list[str] = []
312-
requirement = "foo==1.2.3"
313-
constraint = "foo<2"
312+
requirement = "coo==1.2.3"
313+
constraint = "coo<2"
314314
if request.param == "explicit":
315315
deps = requirement
316316
exp_constraints.append(requirement)

0 commit comments

Comments
 (0)