Skip to content

Commit 6852908

Browse files
authored
Enforce f-strings via Ruff (#12393)
1 parent 9685f64 commit 6852908

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+201
-334
lines changed

docs/pip_sphinxext.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,22 +194,17 @@ def process_options(self) -> None:
194194
opt = option()
195195
opt_name = opt._long_opts[0]
196196
if opt._short_opts:
197-
short_opt_name = "{}, ".format(opt._short_opts[0])
197+
short_opt_name = f"{opt._short_opts[0]}, "
198198
else:
199199
short_opt_name = ""
200200

201201
if option in cmdoptions.general_group["options"]:
202202
prefix = ""
203203
else:
204-
prefix = "{}_".format(self.determine_opt_prefix(opt_name))
204+
prefix = f"{self.determine_opt_prefix(opt_name)}_"
205205

206206
self.view_list.append(
207-
"* :ref:`{short}{long}<{prefix}{opt_name}>`".format(
208-
short=short_opt_name,
209-
long=opt_name,
210-
prefix=prefix,
211-
opt_name=opt_name,
212-
),
207+
f"* :ref:`{short_opt_name}{opt_name}<{prefix}{opt_name}>`",
213208
"\n",
214209
)
215210

news/12393.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enforce and update code to use f-strings via Ruff rule UP032

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ select = [
102102
"PLR0",
103103
"W",
104104
"RUF100",
105+
"UP032",
105106
]
106107

107108
[tool.ruff.isort]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def get_version(rel_path: str) -> str:
7777
entry_points={
7878
"console_scripts": [
7979
"pip=pip._internal.cli.main:main",
80-
"pip{}=pip._internal.cli.main:main".format(sys.version_info[0]),
80+
f"pip{sys.version_info[0]}=pip._internal.cli.main:main",
8181
"pip{}.{}=pip._internal.cli.main:main".format(*sys.version_info[:2]),
8282
],
8383
},

src/pip/_internal/cli/cmdoptions.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,7 @@ def _handle_python_version(
582582
"""
583583
version_info, error_msg = _convert_python_version(value)
584584
if error_msg is not None:
585-
msg = "invalid --python-version value: {!r}: {}".format(
586-
value,
587-
error_msg,
588-
)
585+
msg = f"invalid --python-version value: {value!r}: {error_msg}"
589586
raise_option_error(parser, option=option, msg=msg)
590587

591588
parser.values.python_version = version_info
@@ -921,9 +918,9 @@ def _handle_merge_hash(
921918
algo, digest = value.split(":", 1)
922919
except ValueError:
923920
parser.error(
924-
"Arguments to {} must be a hash name "
921+
f"Arguments to {opt_str} must be a hash name "
925922
"followed by a value, like --hash=sha256:"
926-
"abcde...".format(opt_str)
923+
"abcde..."
927924
)
928925
if algo not in STRONG_HASHES:
929926
parser.error(

src/pip/_internal/cli/parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ def _update_defaults(self, defaults: Dict[str, Any]) -> Dict[str, Any]:
229229
val = strtobool(val)
230230
except ValueError:
231231
self.error(
232-
"{} is not a valid value for {} option, "
232+
f"{val} is not a valid value for {key} option, "
233233
"please specify a boolean value like yes/no, "
234-
"true/false or 1/0 instead.".format(val, key)
234+
"true/false or 1/0 instead."
235235
)
236236
elif option.action == "count":
237237
with suppress(ValueError):
@@ -240,10 +240,10 @@ def _update_defaults(self, defaults: Dict[str, Any]) -> Dict[str, Any]:
240240
val = int(val)
241241
if not isinstance(val, int) or val < 0:
242242
self.error(
243-
"{} is not a valid value for {} option, "
243+
f"{val} is not a valid value for {key} option, "
244244
"please instead specify either a non-negative integer "
245245
"or a boolean value like yes/no or false/true "
246-
"which is equivalent to 1/0.".format(val, key)
246+
"which is equivalent to 1/0."
247247
)
248248
elif option.action == "append":
249249
val = val.split()

src/pip/_internal/commands/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def remove_cache_items(self, options: Values, args: List[Any]) -> None:
175175
files += self._find_http_files(options)
176176
else:
177177
# Add the pattern to the log message
178-
no_matching_msg += ' for pattern "{}"'.format(args[0])
178+
no_matching_msg += f' for pattern "{args[0]}"'
179179

180180
if not files:
181181
logger.warning(no_matching_msg)

src/pip/_internal/commands/configuration.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,15 @@ def open_in_editor(self, options: Values, args: List[str]) -> None:
242242
e.filename = editor
243243
raise
244244
except subprocess.CalledProcessError as e:
245-
raise PipError(
246-
"Editor Subprocess exited with exit code {}".format(e.returncode)
247-
)
245+
raise PipError(f"Editor Subprocess exited with exit code {e.returncode}")
248246

249247
def _get_n_args(self, args: List[str], example: str, n: int) -> Any:
250248
"""Helper to make sure the command got the right number of arguments"""
251249
if len(args) != n:
252250
msg = (
253-
"Got unexpected number of arguments, expected {}. "
254-
'(example: "{} config {}")'
255-
).format(n, get_prog(), example)
251+
f"Got unexpected number of arguments, expected {n}. "
252+
f'(example: "{get_prog()} config {example}")'
253+
)
256254
raise PipError(msg)
257255

258256
if n == 1:

src/pip/_internal/commands/debug.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def show_actual_vendor_versions(vendor_txt_versions: Dict[str, str]) -> None:
9595
elif parse_version(actual_version) != parse_version(expected_version):
9696
extra_message = (
9797
" (CONFLICT: vendor.txt suggests version should"
98-
" be {})".format(expected_version)
98+
f" be {expected_version})"
9999
)
100100
logger.info("%s==%s%s", module_name, actual_version, extra_message)
101101

@@ -120,7 +120,7 @@ def show_tags(options: Values) -> None:
120120
if formatted_target:
121121
suffix = f" (target: {formatted_target})"
122122

123-
msg = "Compatible tags: {}{}".format(len(tags), suffix)
123+
msg = f"Compatible tags: {len(tags)}{suffix}"
124124
logger.info(msg)
125125

126126
if options.verbose < 1 and len(tags) > tag_limit:
@@ -134,9 +134,7 @@ def show_tags(options: Values) -> None:
134134
logger.info(str(tag))
135135

136136
if tags_limited:
137-
msg = (
138-
"...\n[First {tag_limit} tags shown. Pass --verbose to show all.]"
139-
).format(tag_limit=tag_limit)
137+
msg = f"...\n[First {tag_limit} tags shown. Pass --verbose to show all.]"
140138
logger.info(msg)
141139

142140

src/pip/_internal/commands/index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ def get_available_package_versions(self, options: Values, args: List[Any]) -> No
128128

129129
if not versions:
130130
raise DistributionNotFound(
131-
"No matching distribution found for {}".format(query)
131+
f"No matching distribution found for {query}"
132132
)
133133

134134
formatted_versions = [str(ver) for ver in sorted(versions, reverse=True)]
135135
latest = formatted_versions[0]
136136

137-
write_output("{} ({})".format(query, latest))
137+
write_output(f"{query} ({latest})")
138138
write_output("Available versions: {}".format(", ".join(formatted_versions)))
139139
print_dist_installation_info(query, latest)

0 commit comments

Comments
 (0)