Skip to content

Commit 3b83c02

Browse files
committed
Use str.format and f-strings where possible
1 parent ea9ac33 commit 3b83c02

20 files changed

+52
-52
lines changed

bpython/args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def error(self, msg):
2424

2525

2626
def version_banner():
27-
return "bpython version %s on top of Python %s %s" % (
27+
return "bpython version {} on top of Python {} {}".format(
2828
__version__,
2929
sys.version.split()[0],
3030
sys.executable,

bpython/autocomplete.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def attr_lookup(self, obj, expr, attr):
351351
n = len(attr)
352352
for word in words:
353353
if self.method_match(word, n, attr) and word != "__builtins__":
354-
matches.append("%s.%s" % (expr, word))
354+
matches.append(f"{expr}.{word}")
355355
return matches
356356

357357
def list_attributes(self, obj):
@@ -377,7 +377,7 @@ def matches(self, cursor_offset, line, **kwargs):
377377
return None
378378
if isinstance(obj, dict) and obj.keys():
379379
matches = {
380-
"{0!r}]".format(k)
380+
f"{k!r}]"
381381
for k in obj.keys()
382382
if repr(k).startswith(r.word)
383383
}
@@ -562,7 +562,7 @@ def matches(self, cursor_offset, line, **kwargs):
562562
history = kwargs["history"]
563563

564564
if "\n" in current_block:
565-
assert cursor_offset <= len(line), "%r %r" % (
565+
assert cursor_offset <= len(line), "{!r} {!r}".format(
566566
cursor_offset,
567567
line,
568568
)

bpython/cli.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def mkargspec(self, topline, in_arg, down):
773773
if args:
774774
self.list_win.addstr(", ", punctuation_colpair)
775775
self.list_win.addstr(
776-
"*%s" % (_args,), get_colpair(self.config, "token")
776+
f"*{_args}", get_colpair(self.config, "token")
777777
)
778778

779779
if kwonly:
@@ -799,7 +799,7 @@ def mkargspec(self, topline, in_arg, down):
799799
if args or _args or kwonly:
800800
self.list_win.addstr(", ", punctuation_colpair)
801801
self.list_win.addstr(
802-
"**%s" % (_kwargs,), get_colpair(self.config, "token")
802+
f"**{_kwargs}", get_colpair(self.config, "token")
803803
)
804804
self.list_win.addstr(")", punctuation_colpair)
805805

@@ -978,7 +978,7 @@ def p_key(self, key):
978978
try:
979979
source = self.get_source_of_current_name()
980980
except repl.SourceNotFound as e:
981-
self.statusbar.message("%s" % (e,))
981+
self.statusbar.message(f"{e}")
982982
else:
983983
if config.highlight_show_source:
984984
source = format(
@@ -1072,7 +1072,7 @@ def prompt(self, more):
10721072
"""Show the appropriate Python prompt"""
10731073
if not more:
10741074
self.echo(
1075-
"\x01%s\x03%s" % (self.config.color_scheme["prompt"], self.ps1)
1075+
"\x01{}\x03{}".format(self.config.color_scheme["prompt"], self.ps1)
10761076
)
10771077
self.stdout_hist += self.ps1
10781078
self.screen_hist.append(
@@ -1081,10 +1081,10 @@ def prompt(self, more):
10811081
)
10821082
else:
10831083
prompt_more_color = self.config.color_scheme["prompt_more"]
1084-
self.echo("\x01%s\x03%s" % (prompt_more_color, self.ps2))
1084+
self.echo(f"\x01{prompt_more_color}\x03{self.ps2}")
10851085
self.stdout_hist += self.ps2
10861086
self.screen_hist.append(
1087-
"\x01%s\x03%s\x04" % (prompt_more_color, self.ps2)
1087+
f"\x01{prompt_more_color}\x03{self.ps2}\x04"
10881088
)
10891089

10901090
def push(self, s, insert_into_history=True):
@@ -1480,7 +1480,7 @@ def undo(self, n=1):
14801480
def writetb(self, lines):
14811481
for line in lines:
14821482
self.write(
1483-
"\x01%s\x03%s" % (self.config.color_scheme["error"], line)
1483+
"\x01{}\x03{}".format(self.config.color_scheme["error"], line)
14841484
)
14851485

14861486
def yank_from_buffer(self):
@@ -1700,7 +1700,7 @@ def init_wins(scr, config):
17001700
)
17011701

17021702
message = " ".join(
1703-
"<%s> %s" % (key, command) for command, key in commands if key
1703+
f"<{key}> {command}" for command, key in commands if key
17041704
)
17051705

17061706
statusbar = Statusbar(

bpython/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def fill_config_with_default_values(config, default_values):
4848

4949
for (opt, val) in default_values[section].items():
5050
if not config.has_option(section, opt):
51-
config.set(section, opt, "%s" % (val,))
51+
config.set(section, opt, f"{val}")
5252

5353

5454
def loadini(struct, configfile):
@@ -270,7 +270,7 @@ def get_key_no_doublebind(command):
270270
load_theme(struct, path, struct.color_scheme, default_colors)
271271
except EnvironmentError:
272272
sys.stderr.write(
273-
"Could not load theme '%s'.\n" % (color_scheme_name,)
273+
f"Could not load theme '{color_scheme_name}'.\n"
274274
)
275275
sys.exit(1)
276276

bpython/curtsiesfrontend/filewatch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def track_module(self, path):
5858

5959
def activate(self):
6060
if self.activated:
61-
raise ValueError("%r is already activated." % (self,))
61+
raise ValueError(f"{self!r} is already activated.")
6262
if not self.started:
6363
self.started = True
6464
self.observer.start()
@@ -71,7 +71,7 @@ def activate(self):
7171

7272
def deactivate(self):
7373
if not self.activated:
74-
raise ValueError("%r is not activated." % (self,))
74+
raise ValueError(f"{self!r} is not activated.")
7575
self.observer.unschedule_all()
7676
self.activated = False
7777

bpython/curtsiesfrontend/interpreter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class BPythonFormatter(Formatter):
4545
def __init__(self, color_scheme, **options):
4646
self.f_strings = {}
4747
for k, v in color_scheme.items():
48-
self.f_strings[k] = "\x01%s" % (v,)
48+
self.f_strings[k] = f"\x01{v}"
4949
super().__init__(**options)
5050

5151
def format(self, tokensource, outfile):
@@ -54,7 +54,7 @@ def format(self, tokensource, outfile):
5454
for token, text in tokensource:
5555
while token not in self.f_strings:
5656
token = token.parent
57-
o += "%s\x03%s\x04" % (self.f_strings[token], text)
57+
o += "{}\x03{}\x04".format(self.f_strings[token], text)
5858
outfile.write(parse(o.rstrip()))
5959

6060

bpython/curtsiesfrontend/manual_readline.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def add(self, key, func, overwrite=False):
3535
if overwrite:
3636
del self[key]
3737
else:
38-
raise ValueError("key %r already has a mapping" % (key,))
38+
raise ValueError(f"key {key!r} already has a mapping")
3939
params = getargspec(func)
4040
args = {
4141
k: v for k, v in self.default_kwargs.items() if k in params
@@ -57,13 +57,13 @@ def add(self, key, func, overwrite=False):
5757
self.cut_buffer_edits[key] = func
5858
else:
5959
raise ValueError(
60-
"return type of function %r not recognized" % (func,)
60+
f"return type of function {func!r} not recognized"
6161
)
6262

6363
def add_config_attr(self, config_attr, func):
6464
if config_attr in self.awaiting_config:
6565
raise ValueError(
66-
"config attribute %r already has a mapping" % (config_attr,)
66+
f"config attribute {config_attr!r} already has a mapping"
6767
)
6868
self.awaiting_config[config_attr] = func
6969

@@ -84,15 +84,15 @@ def __getitem__(self, key):
8484
return self.simple_edits[key]
8585
if key in self.cut_buffer_edits:
8686
return self.cut_buffer_edits[key]
87-
raise KeyError("key %r not mapped" % (key,))
87+
raise KeyError(f"key {key!r} not mapped")
8888

8989
def __delitem__(self, key):
9090
if key in self.simple_edits:
9191
del self.simple_edits[key]
9292
elif key in self.cut_buffer_edits:
9393
del self.cut_buffer_edits[key]
9494
else:
95-
raise KeyError("key %r not mapped" % (key,))
95+
raise KeyError(f"key {key!r} not mapped")
9696

9797

9898
class UnconfiguredEdits(AbstractEdits):

bpython/curtsiesfrontend/repl.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,13 +1350,13 @@ def display_line_with_prompt(self):
13501350
if self.incr_search_mode == "reverse_incremental_search":
13511351
return (
13521352
prompt(
1353-
"(reverse-i-search)`{}': ".format(self.incr_search_target)
1353+
f"(reverse-i-search)`{self.incr_search_target}': "
13541354
)
13551355
+ self.current_line_formatted
13561356
)
13571357
elif self.incr_search_mode == "incremental_search":
13581358
return (
1359-
prompt("(i-search)`%s': ".format(self.incr_search_target))
1359+
prompt(f"(i-search)`%s': ")
13601360
+ self.current_line_formatted
13611361
)
13621362
return (
@@ -1976,7 +1976,7 @@ def show_source(self):
19761976
try:
19771977
source = self.get_source_of_current_name()
19781978
except SourceNotFound as e:
1979-
self.status_bar.message("%s" % (e,))
1979+
self.status_bar.message(f"{e}")
19801980
else:
19811981
if self.config.highlight_show_source:
19821982
source = pygformat(
@@ -2028,7 +2028,7 @@ def key_help_text(self):
20282028

20292029
max_func = max(len(func) for func, key in pairs)
20302030
return "\n".join(
2031-
"%s : %s" % (func.rjust(max_func), key) for func, key in pairs
2031+
"{} : {}".format(func.rjust(max_func), key) for func, key in pairs
20322032
)
20332033

20342034

bpython/curtsiesfrontend/replpainter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def formatted_argspec(funcprops, arg_pos, columns, config):
133133
if _args:
134134
if args:
135135
s += punctuation_color(", ")
136-
s += token_color("*%s" % (_args,))
136+
s += token_color(f"*{_args}")
137137

138138
if kwonly:
139139
if not _args:
@@ -155,7 +155,7 @@ def formatted_argspec(funcprops, arg_pos, columns, config):
155155
if _kwargs:
156156
if args or _args or kwonly:
157157
s += punctuation_color(", ")
158-
s += token_color("**%s" % (_kwargs,))
158+
s += token_color(f"**{_kwargs}")
159159
s += punctuation_color(")")
160160

161161
return linesplit(s, columns)

bpython/formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class BPythonFormatter(Formatter):
9999
def __init__(self, color_scheme, **options):
100100
self.f_strings = {}
101101
for k, v in theme_map.items():
102-
self.f_strings[k] = "\x01%s" % (color_scheme[v],)
102+
self.f_strings[k] = "\x01{}".format(color_scheme[v])
103103
if k is Parenthesis:
104104
# FIXME: Find a way to make this the inverse of the current
105105
# background colour
@@ -114,7 +114,7 @@ def format(self, tokensource, outfile):
114114

115115
while token not in self.f_strings:
116116
token = token.parent
117-
o += "%s\x03%s\x04" % (self.f_strings[token], text)
117+
o += "{}\x03{}\x04".format(self.f_strings[token], text)
118118
outfile.write(o.rstrip())
119119

120120

0 commit comments

Comments
 (0)