Skip to content

Commit 2c1f59f

Browse files
authored
Merge pull request #13797 from gcomneno/pr-13226-constraint-error
Fix constraint file open error message
2 parents 08f7a9b + 44eeb27 commit 2c1f59f

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

news/13226.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix misleading error message when a constraint file cannot be opened.

src/pip/_internal/req/req_file.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def _parse_and_recurse(
412412
def _parse_file(
413413
self, filename: str, constraint: bool
414414
) -> Generator[ParsedLine, None, None]:
415-
_, content = get_file_content(filename, self._session)
415+
_, content = get_file_content(filename, self._session, constraint=constraint)
416416

417417
lines_enum = preprocess(content)
418418

@@ -572,7 +572,9 @@ def expand_env_variables(lines_enum: ReqFileLines) -> ReqFileLines:
572572
yield line_number, line
573573

574574

575-
def get_file_content(url: str, session: PipSession) -> tuple[str, str]:
575+
def get_file_content(
576+
url: str, session: PipSession, *, constraint: bool = False
577+
) -> tuple[str, str]:
576578
"""Gets the content of a file; it may be a filename, file: URL, or
577579
http: URL. Returns (location, content). Content is unicode.
578580
Respects # -*- coding: declarations on the retrieved files.
@@ -595,7 +597,8 @@ def get_file_content(url: str, session: PipSession) -> tuple[str, str]:
595597
with open(url, "rb") as f:
596598
raw_content = f.read()
597599
except OSError as exc:
598-
raise InstallationError(f"Could not open requirements file: {exc}")
600+
kind = "constraint" if constraint else "requirements"
601+
raise InstallationError(f"Could not open {kind} file: {exc}")
599602

600603
content = _decode_req_file(raw_content, url)
601604

tests/unit/test_req_file.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ def parse_reqfile(
8484
)
8585

8686

87+
def test_missing_constraint_file_message_mentions_constraints(
88+
tmp_path: Path, session: PipSession
89+
) -> None:
90+
missing = tmp_path / "does-not-exist.txt"
91+
92+
with pytest.raises(InstallationError) as exc:
93+
list(parse_reqfile(missing, session=session, constraint=True))
94+
95+
assert "Could not open constraint file:" in str(exc.value)
96+
97+
8798
def test_read_file_url(tmp_path: Path, session: PipSession) -> None:
8899
reqs = tmp_path.joinpath("requirements.txt")
89100
reqs.write_text("foo")

0 commit comments

Comments
 (0)