Skip to content

Commit 1b94835

Browse files
committed
style: convert more string formatting to f-strings
1 parent 79f9f45 commit 1b94835

22 files changed

+40
-42
lines changed

coverage/disposition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def disposition_debug_msg(disp):
3333
if disp.original_filename != disp.source_filename:
3434
msg += f" as {disp.source_filename!r}"
3535
if disp.file_tracer:
36-
msg += ": will be traced by %r" % disp.file_tracer
36+
msg += f": will be traced by {disp.file_tracer!r}"
3737
else:
3838
msg = f"Not tracing {disp.original_filename!r}: {disp.reason}"
3939
return msg

coverage/execfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def _prepare2(self):
144144
self.arg0 = try_filename
145145
break
146146
else:
147-
raise NoSource("Can't find '__main__' module in '%s'" % self.arg0)
147+
raise NoSource(f"Can't find '__main__' module in '{self.arg0}'")
148148

149149
# Make a spec. I don't know if this is the right way to do it.
150150
try_filename = python_reported_file(try_filename)

coverage/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def bool_or_none(b):
184184

185185
def join_regex(regexes):
186186
"""Combine a list of regexes into one that matches any of them."""
187-
return "|".join("(?:%s)" % r for r in regexes)
187+
return "|".join(f"(?:{r})" for r in regexes)
188188

189189

190190
def file_be_gone(path):

coverage/results.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def format_lines(statements, lines, arcs=None):
317317
for ex in sorted(exits):
318318
if line not in lines and ex not in lines:
319319
dest = (ex if ex > 0 else "exit")
320-
line_items.append((line, "%d->%s" % (line, dest)))
320+
line_items.append((line, f"{line}->{dest}"))
321321

322322
ret = ', '.join(t[-1] for t in sorted(line_items))
323323
return ret

coverage/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def _make_version(major, minor, micro, releaselevel, serial):
1616
version += ".%d" % (micro,)
1717
if releaselevel != 'final':
1818
short = {'alpha': 'a', 'beta': 'b', 'candidate': 'rc'}[releaselevel]
19-
version += "%s%d" % (short, serial)
19+
version += f"{short}{serial}"
2020
return version
2121

2222

coverage/xmlreport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ def report(self, morfs, outfile=None):
6767
xcoverage.setAttribute("version", __version__)
6868
xcoverage.setAttribute("timestamp", str(int(time.time()*1000)))
6969
xcoverage.appendChild(self.xml_out.createComment(
70-
" Generated by coverage.py: %s " % __url__
70+
f" Generated by coverage.py: {__url__} "
7171
))
72-
xcoverage.appendChild(self.xml_out.createComment(" Based on %s " % DTD_URL))
72+
xcoverage.appendChild(self.xml_out.createComment(f" Based on {DTD_URL} "))
7373

7474
# Call xml_file for each file in the data.
7575
for fr, analysis in get_analysis_to_report(self.coverage, morfs):

igor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ def check_file(fname, crlf=True, trail_white=True):
282282
for n, line in enumerate(f, start=1):
283283
if crlf:
284284
if b"\r" in line:
285-
print("%s@%d: CR found" % (fname, n))
285+
print(f"{fname}@{n}: CR found")
286286
return
287287
if trail_white:
288288
line = line[:-1]
289289
if not crlf:
290290
line = line.rstrip(b'\r')
291291
if line.rstrip() != line:
292-
print("%s@%d: trailing whitespace found" % (fname, n))
292+
print(f"{fname}@{n}: trailing whitespace found")
293293
return
294294

295295
if line is not None and not line.strip():
@@ -391,7 +391,7 @@ def main(args):
391391
verb = args.pop(0)
392392
handler = globals().get('do_'+verb)
393393
if handler is None:
394-
print("*** No handler for %r" % verb)
394+
print(f"*** No handler for {verb!r}")
395395
return 1
396396
star, num_args = analyze_args(handler)
397397
if star:

tests/coveragetest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,11 @@ def assert_same_files(self, flist1, flist2):
275275

276276
def assert_exists(self, fname):
277277
"""Assert that `fname` is a file that exists."""
278-
msg = "File %r should exist" % fname
279-
assert os.path.exists(fname), msg
278+
assert os.path.exists(fname), f"File {fname!r} should exist"
280279

281280
def assert_doesnt_exist(self, fname):
282281
"""Assert that `fname` is a file that doesn't exist."""
283-
msg = "File %r shouldn't exist" % fname
284-
assert not os.path.exists(fname), msg
282+
assert not os.path.exists(fname), f"File {fname!r} shouldn't exist"
285283

286284
def assert_file_count(self, pattern, count):
287285
"""Assert that there are `count` files matching `pattern`."""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
> def b(x):
2-
> msg = "x is %s" % x
2+
> msg = f"x is {x}"
33
> print(msg)

tests/modules/process_test/try_execfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def without_same_files(filenames):
6161

6262
def my_function(a):
6363
"""A function to force execution of module-level values."""
64-
return "my_fn(%r)" % a
64+
return f"my_fn({a!r})"
6565

6666
FN_VAL = my_function("fooey")
6767

tests/test_annotate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def a(x):
3232
self.make_file("b/__init__.py")
3333
self.make_file("b/b.py", """\
3434
def b(x):
35-
msg = "x is %s" % x
35+
msg = f"x is {x}"
3636
print(msg)
3737
""")
3838

tests/test_arcs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,15 +1835,15 @@ def test_async(self):
18351835
import asyncio
18361836
18371837
async def compute(x, y): # 3
1838-
print("Compute %s + %s ..." % (x, y))
1838+
print(f"Compute {x} + {y} ...")
18391839
await asyncio.sleep(0.001)
18401840
return x + y # 6
18411841
18421842
async def print_sum(x, y): # 8
18431843
result = (0 +
18441844
await compute(x, y) # A
18451845
)
1846-
print("%s + %s = %s" % (x, y, result))
1846+
print(f"{x} + {y} = {result}")
18471847
18481848
loop = asyncio.new_event_loop() # E
18491849
loop.run_until_complete(print_sum(1, 2))

tests/test_concurrency.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ def try_some_code(self, code, concurrency, the_module, expected_out=None):
215215

216216
self.make_file("try_it.py", code)
217217

218-
cmd = "coverage run --concurrency=%s try_it.py" % concurrency
218+
cmd = f"coverage run --concurrency={concurrency} try_it.py"
219219
out = self.run_command(cmd)
220220

221221
expected_cant_trace = cant_trace_msg(concurrency, the_module)
@@ -366,11 +366,11 @@ def try_multiprocessing_code(
366366
):
367367
"""Run code using multiprocessing, it should produce `expected_out`."""
368368
self.make_file("multi.py", code)
369-
self.make_file(".coveragerc", """\
369+
self.make_file(".coveragerc", f"""\
370370
[run]
371-
concurrency = %s
371+
concurrency = {concurrency}
372372
source = .
373-
""" % concurrency)
373+
""")
374374

375375
for start_method in ["fork", "spawn"]:
376376
if start_method and start_method not in multiprocessing.get_all_start_methods():

tests/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_parse_errors(self):
181181
]
182182

183183
for bad_config, msg in bad_configs_and_msgs:
184-
print("Trying %r" % bad_config)
184+
print(f"Trying {bad_config!r}")
185185
self.make_file(".coveragerc", bad_config)
186186
with pytest.raises(CoverageException, match=msg):
187187
coverage.Coverage()
@@ -689,7 +689,7 @@ def test_unreadable_config(self):
689689
".",
690690
]
691691
for bad_file in bad_files:
692-
msg = "Couldn't read %r as a config file" % bad_file
692+
msg = f"Couldn't read {bad_file!r} as a config file"
693693
with pytest.raises(CoverageException, match=msg):
694694
coverage.Coverage(config_file=bad_file)
695695

tests/test_debug.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def test_debug_config(self):
171171
report_include report_omit
172172
""".split()
173173
for label in labels:
174-
label_pat = r"^\s*%s: " % label
175-
msg = "Incorrect lines for %r" % label
174+
label_pat = fr"^\s*{label}: "
175+
msg = f"Incorrect lines for {label!r}"
176176
assert 1 == len(re_lines(label_pat, out_lines)), msg
177177

178178
def test_debug_sys(self):
@@ -185,8 +185,8 @@ def test_debug_sys(self):
185185
pid cwd path environment command_line cover_match pylib_match
186186
""".split()
187187
for label in labels:
188-
label_pat = r"^\s*%s: " % label
189-
msg = "Incorrect lines for %r" % label
188+
label_pat = fr"^\s*{label}: "
189+
msg = f"Incorrect lines for {label!r}"
190190
assert 1 == len(re_lines(label_pat, out_lines)), msg
191191

192192
def test_debug_sys_ctracer(self):

tests/test_execfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_missing_final_newline(self):
8383
self.make_file("abrupt.py", """\
8484
if 1:
8585
a = 1
86-
print("a is %r" % a)
86+
print(f"a is {a!r}")
8787
#""")
8888
with open("abrupt.py") as f:
8989
abrupt = f.read()

tests/test_oddball.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def test_setting_new_trace_function(self):
475475
476476
def tracer(frame, event, arg):
477477
filename = os.path.basename(frame.f_code.co_filename)
478-
print("%s: %s @ %d" % (event, filename, frame.f_lineno))
478+
print(f"{event}: {filename} @ {frame.f_lineno}")
479479
return tracer
480480
481481
def begin():

tests/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ class Bar:
463463
fname = fname + ".py"
464464
self.make_file(fname, text, newline=newline)
465465
parser = self.parse_file(fname)
466-
assert parser.exit_counts() == counts, "Wrong for %r" % fname
466+
assert parser.exit_counts() == counts, f"Wrong for {fname!r}"
467467

468468
def test_encoding(self):
469469
self.make_file("encoded.py", """\

tests/test_phystokens.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class SourceEncodingTest(CoverageTest):
171171

172172
def test_detect_source_encoding(self):
173173
for _, source, expected in ENCODING_DECLARATION_SOURCES:
174-
assert source_encoding(source) == expected, "Wrong encoding in %r" % source
174+
assert source_encoding(source) == expected, f"Wrong encoding in {source!r}"
175175

176176
# PyPy3 gets this case wrong. Not sure what I can do about it, so skip the test.
177177
@pytest.mark.skipif(env.PYPY, reason="PyPy3 is wrong about non-comment encoding. Skip it.")
@@ -233,7 +233,7 @@ def test_neuter_encoding_declaration(self):
233233

234234
# The neutered source will be detected as having no encoding
235235
# declaration.
236-
assert source_encoding(neutered) == DEF_ENCODING, "Wrong encoding in %r" % neutered
236+
assert source_encoding(neutered) == DEF_ENCODING, f"Wrong encoding in {neutered!r}"
237237

238238
def test_two_encoding_declarations(self):
239239
input_src = textwrap.dedent("""\

tests/test_plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ def make_test_files(self):
988988
"""Make some files to use while testing dynamic context plugins."""
989989
self.make_file("rendering.py", """\
990990
def html_tag(tag, content):
991-
return '<%s>%s</%s>' % (tag, content, tag)
991+
return f'<{tag}>{content}</{tag}>'
992992
993993
def render_paragraph(text):
994994
return html_tag('p', text)

tests/test_process.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def test_combine_with_aliases(self):
382382
self.make_file("d1/x.py", """\
383383
a = 1
384384
b = 2
385-
print("%s %s" % (a, b))
385+
print(f"{a} {b}")
386386
""")
387387

388388
self.make_file("d2/x.py", """\
@@ -391,7 +391,7 @@ def test_combine_with_aliases(self):
391391
# 3
392392
c = 4
393393
d = 5
394-
print("%s %s" % (c, d))
394+
print(f"{c} {d}")
395395
""")
396396

397397
self.make_file(".coveragerc", """\
@@ -823,7 +823,7 @@ def foo():
823823
import coverage
824824
825825
for i in [1, 2]:
826-
sys.stderr.write("Run %s\\n" % i)
826+
sys.stderr.write(f"Run {i}\\n")
827827
inst = coverage.Coverage(source=['foo'])
828828
inst.load()
829829
inst.start()
@@ -1206,9 +1206,9 @@ def test_aliases_used_in_messages(self):
12061206
"coverage-%d.%d" % sys.version_info[:2],
12071207
]
12081208
for cmd in cmds:
1209-
out = self.run_command("%s foobar" % cmd)
1209+
out = self.run_command(f"{cmd} foobar")
12101210
assert "Unknown command: 'foobar'" in out
1211-
assert "Use '%s help' for help" % cmd in out
1211+
assert f"Use '{cmd} help' for help" in out
12121212

12131213

12141214
class PydocTest(CoverageTest):
@@ -1557,7 +1557,7 @@ def test_subprocess_with_pth_files_and_parallel(self):
15571557
data_files = glob.glob(os.getcwd() + '/.coverage*')
15581558
msg = (
15591559
"Expected only .coverage after combine, looks like there are " +
1560-
"extra data files that were not cleaned up: %r" % data_files
1560+
f"extra data files that were not cleaned up: {data_files!r}"
15611561
)
15621562
assert len(data_files) == 1, msg
15631563

tests/test_summary.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def test_report_no_extension(self):
631631
if not b:
632632
c = 6
633633
d = 7
634-
print("xxx: %r %r %r %r" % (a, b, c, d))
634+
print(f"xxx: {a} {b} {c} {d}")
635635
""")
636636
out = self.run_command("coverage run --source=. xxx")
637637
assert out == "xxx: 3 4 0 7\n"

0 commit comments

Comments
 (0)