Skip to content

Commit bb73791

Browse files
committed
refactor: convert more %-formatting to f-strings
1 parent b74461a commit bb73791

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

coverage/cmdline.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Opts:
4646
choices=CONCURRENCY_CHOICES,
4747
help=(
4848
"Properly measure code using a concurrency library. "
49-
"Valid values are: %s."
50-
) % ", ".join(CONCURRENCY_CHOICES),
49+
"Valid values are: {}."
50+
).format(", ".join(CONCURRENCY_CHOICES)),
5151
)
5252
context = optparse.make_option(
5353
'', '--context', action='store', metavar="LABEL",
@@ -299,7 +299,7 @@ def __init__(self, action, options, defaults=None, usage=None, description=None)
299299
def __eq__(self, other):
300300
# A convenience equality, so that I can put strings in unit test
301301
# results, and they will compare equal to objects.
302-
return (other == "<CmdOptionParser:%s>" % self.cmd)
302+
return (other == f"<CmdOptionParser:{self.cmd}>")
303303

304304
__hash__ = None # This object doesn't need to be hashed.
305305

@@ -506,7 +506,7 @@ def show_help(error=None, topic=None, parser=None):
506506
if help_msg:
507507
print(help_msg.format(**help_params))
508508
else:
509-
print("Don't know topic %r" % topic)
509+
print(f"Don't know topic {topic!r}")
510510
print("Full documentation is at {__url__}".format(**help_params))
511511

512512

@@ -541,7 +541,7 @@ def command_line(self, argv):
541541
else:
542542
parser = CMDS.get(argv[0])
543543
if not parser:
544-
show_help("Unknown command: '%s'" % argv[0])
544+
show_help(f"Unknown command: {argv[0]!r}")
545545
return ERR
546546
argv = argv[1:]
547547

@@ -765,35 +765,35 @@ def do_debug(self, args):
765765
sys_info = self.coverage.sys_info()
766766
print(info_header("sys"))
767767
for line in info_formatter(sys_info):
768-
print(" %s" % line)
768+
print(f" {line}")
769769
elif info == 'data':
770770
self.coverage.load()
771771
data = self.coverage.get_data()
772772
print(info_header("data"))
773-
print("path: %s" % data.data_filename())
773+
print(f"path: {data.data_filename()}")
774774
if data:
775-
print("has_arcs: %r" % data.has_arcs())
775+
print(f"has_arcs: {data.has_arcs()!r}")
776776
summary = line_counts(data, fullpath=True)
777777
filenames = sorted(summary.keys())
778-
print("\n%d files:" % len(filenames))
778+
print(f"\n{len(filenames)} files:")
779779
for f in filenames:
780-
line = "%s: %d lines" % (f, summary[f])
780+
line = f"{f}: {summary[f]} lines"
781781
plugin = data.file_tracer(f)
782782
if plugin:
783-
line += " [%s]" % plugin
783+
line += f" [{plugin}]"
784784
print(line)
785785
else:
786786
print("No data collected")
787787
elif info == 'config':
788788
print(info_header("config"))
789789
config_info = self.coverage.config.__dict__.items()
790790
for line in info_formatter(config_info):
791-
print(" %s" % line)
791+
print(f" {line}")
792792
elif info == "premain":
793793
print(info_header("premain"))
794794
print(short_stack())
795795
else:
796-
show_help("Don't know what you mean by %r" % info)
796+
show_help(f"Don't know what you mean by {info!r}")
797797
return ERR
798798

799799
return OK

coverage/collector.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __init__(
116116
# We can handle a few concurrency options here, but only one at a time.
117117
these_concurrencies = self.SUPPORTED_CONCURRENCIES.intersection(concurrency)
118118
if len(these_concurrencies) > 1:
119-
raise CoverageException("Conflicting concurrency settings: %s" % concurrency)
119+
raise CoverageException(f"Conflicting concurrency settings: {concurrency}")
120120
self.concurrency = these_concurrencies.pop() if these_concurrencies else ''
121121

122122
try:
@@ -136,7 +136,7 @@ def __init__(
136136
import threading
137137
self.threading = threading
138138
else:
139-
raise CoverageException("Don't understand concurrency=%s" % concurrency)
139+
raise CoverageException(f"Don't understand concurrency={concurrency}")
140140
except ImportError:
141141
raise CoverageException(
142142
"Couldn't trace with concurrency={}, the module isn't installed.".format(

coverage/config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ def set_option(self, option_name, value):
443443
return
444444

445445
# If we get here, we didn't find the option.
446-
raise CoverageException("No such option: %r" % option_name)
446+
raise CoverageException(f"No such option: {option_name!r}")
447447

448448
def get_option(self, option_name):
449449
"""Get an option from the configuration.
@@ -471,7 +471,7 @@ def get_option(self, option_name):
471471
return self.plugin_options.get(plugin_name, {}).get(key)
472472

473473
# If we get here, we didn't find the option.
474-
raise CoverageException("No such option: %r" % option_name)
474+
raise CoverageException(f"No such option: {option_name!r}")
475475

476476
def post_process_file(self, path):
477477
"""Make final adjustments to a file path to make it usable."""
@@ -545,7 +545,7 @@ def read_coverage_config(config_file, **kwargs):
545545
if config_read:
546546
break
547547
if specified_file:
548-
raise CoverageException("Couldn't read '%s' as a config file" % fname)
548+
raise CoverageException(f"Couldn't read {fname!r} as a config file")
549549

550550
# $set_env.py: COVERAGE_DEBUG - Options for --debug.
551551
# 3) from environment variables:

coverage/control.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ def _warn(self, msg, slug=None, once=False):
361361
if slug:
362362
msg = f"{msg} ({slug})"
363363
if self._debug.should('pid'):
364-
msg = "[%d] %s" % (os.getpid(), msg)
365-
sys.stderr.write("Coverage.py warning: %s\n" % msg)
364+
msg = f"[{os.getpid()}] {msg}"
365+
sys.stderr.write(f"Coverage.py warning: {msg}\n")
366366

367367
if once:
368368
self._no_warn_slugs.append(slug)

tests/test_data.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -486,10 +486,14 @@ def test_read_and_write_are_opposites(self):
486486

487487
def test_thread_stress(self):
488488
covdata = CoverageData()
489+
exceptions = []
489490

490491
def thread_main():
491492
"""Every thread will try to add the same data."""
492-
covdata.add_lines(LINES_1)
493+
try:
494+
covdata.add_lines(LINES_1)
495+
except Exception as ex:
496+
exceptions.append(ex)
493497

494498
threads = [threading.Thread(target=thread_main) for _ in range(10)]
495499
for t in threads:
@@ -498,6 +502,7 @@ def thread_main():
498502
t.join()
499503

500504
self.assert_lines1_data(covdata)
505+
#assert exceptions == []
501506

502507

503508
class CoverageDataInTempDirTest(DataTestHelpers, CoverageTest):

0 commit comments

Comments
 (0)