Skip to content

Commit e6b8e5f

Browse files
sirosenwebknjaz
andcommitted
Refine 'rewrite_comes_from' functions
Rather than a callable|str with dispatch, make this into a single codepath: always a callable. The `str` case is converted into a fixed-output function to fit this shape. Also, preserve the space separator in string splitting, so that it is clear that we are reconstructing data with the same shape (same separator). Co-authored-by: 🇺🇦 Sviatoslav Sydorenko (Святослав Сидоренко) <578543+webknjaz@users.noreply.github.com>
1 parent 9322a4b commit e6b8e5f

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

piptools/_compat/pip_compat.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ def parse_requirements(
8787
comes_from_stdin: bool = False,
8888
) -> Iterator[InstallRequirement]:
8989
# the `comes_from` data will be rewritten in different ways in different conditions
90-
# each rewrite rule is expressible as a str->str function or as a static replacement
91-
rewrite_comes_from: Callable[[str], str] | str
90+
# each rewrite rule is expressible as a str->str function
91+
rewrite_comes_from: Callable[[str], str]
9292

9393
if comes_from_stdin:
9494
# if data is coming from stdin, then `comes_from="-r -"`
95-
rewrite_comes_from = "-r -"
95+
rewrite_comes_from = _rewrite_comes_from_to_hardcoded_stdin_value
9696
elif pathlib.Path(filename).is_absolute():
9797
# if the input path is absolute, just normalize paths to posix-style
9898
rewrite_comes_from = _normalize_comes_from_location
@@ -113,14 +113,16 @@ def parse_requirements(
113113
install_req.link = file_link
114114
install_req = copy_install_requirement(install_req)
115115

116-
if isinstance(rewrite_comes_from, str):
117-
install_req.comes_from = rewrite_comes_from
118-
else:
119-
install_req.comes_from = rewrite_comes_from(install_req.comes_from)
116+
install_req.comes_from = rewrite_comes_from(install_req.comes_from)
120117

121118
yield install_req
122119

123120

121+
def _rewrite_comes_from_to_hardcoded_stdin_value(_: str, /) -> str:
122+
"""Produce the hardcoded ``comes_from`` value for stdin."""
123+
return "-r -"
124+
125+
124126
def _relativize_comes_from_location(original_comes_from: str, /) -> str:
125127
"""
126128
Convert a ``comes_from`` path to a relative posix path.
@@ -136,7 +138,7 @@ def _relativize_comes_from_location(original_comes_from: str, /) -> str:
136138
return original_comes_from
137139

138140
# split on the space
139-
prefix, _, suffix = original_comes_from.partition(" ")
141+
prefix, space_sep, suffix = original_comes_from.partition(" ")
140142

141143
file_path = pathlib.Path(suffix)
142144

@@ -146,7 +148,7 @@ def _relativize_comes_from_location(original_comes_from: str, /) -> str:
146148

147149
# make it relative to the current working dir
148150
suffix = file_path.relative_to(pathlib.Path.cwd()).as_posix()
149-
return f"{prefix} {suffix}"
151+
return f"{prefix}{space_sep}{suffix}"
150152

151153

152154
def _normalize_comes_from_location(original_comes_from: str, /) -> str:
@@ -165,11 +167,11 @@ def _normalize_comes_from_location(original_comes_from: str, /) -> str:
165167
return original_comes_from
166168

167169
# split on the space
168-
prefix, _, suffix = original_comes_from.partition(" ")
170+
prefix, space_sep, suffix = original_comes_from.partition(" ")
169171

170172
# convert to a posix-style path
171173
suffix = pathlib.Path(suffix).as_posix()
172-
return f"{prefix} {suffix}"
174+
return f"{prefix}{space_sep}{suffix}"
173175

174176

175177
def create_wheel_cache(cache_dir: str, format_control: str | None = None) -> WheelCache:

0 commit comments

Comments
 (0)