Skip to content

Commit 4efd44d

Browse files
authored
Remove .format in favor of f-strings (#551)
* Remove .format in favor of f-strings
1 parent ca4e80b commit 4efd44d

File tree

8 files changed

+75
-95
lines changed

8 files changed

+75
-95
lines changed

docs/guide.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ the program to the command line.
3030
import fire
3131

3232
def hello(name):
33-
return 'Hello {name}!'.format(name=name)
33+
return f'Hello {name}!'
3434

3535
if __name__ == '__main__':
3636
fire.Fire()
@@ -52,7 +52,7 @@ command line.
5252
import fire
5353

5454
def hello(name):
55-
return 'Hello {name}!'.format(name=name)
55+
return f'Hello {name}!'
5656

5757
if __name__ == '__main__':
5858
fire.Fire(hello)
@@ -76,7 +76,7 @@ We can alternatively write this program like this:
7676
import fire
7777

7878
def hello(name):
79-
return 'Hello {name}!'.format(name=name)
79+
return f'Hello {name}!'
8080

8181
def main():
8282
fire.Fire(hello)
@@ -93,7 +93,7 @@ then simply this:
9393
import fire
9494

9595
def hello(name):
96-
return 'Hello {name}!'.format(name=name)
96+
return f'Hello {name}!'
9797

9898
def main():
9999
fire.Fire(hello)
@@ -105,7 +105,7 @@ If you have a file `example.py` that doesn't even import fire:
105105

106106
```python
107107
def hello(name):
108-
return 'Hello {name}!'.format(name=name)
108+
return f'Hello {name}!'
109109
```
110110

111111
Then you can use it with Fire like this:

examples/widget/widget.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def whack(self, n=1):
2525

2626
def bang(self, noise='bang'):
2727
"""Makes a loud noise."""
28-
return '{noise} bang!'.format(noise=noise)
28+
return f'{noise} bang!'
2929

3030

3131
def main():

fire/completion.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ def _FishScript(name, commands, default_options=None):
277277
)
278278

279279
return fish_source.format(
280-
global_options=' '.join(
281-
'"{option}"'.format(option=option)
282-
for option in global_options
283-
)
280+
global_options=' '.join(f'"{option}"' for option in global_options)
284281
)
285282

286283

@@ -385,7 +382,7 @@ def _CompletionsFromArgs(fn_args):
385382
completions = []
386383
for arg in fn_args:
387384
arg = arg.replace('_', '-')
388-
completions.append('--{arg}'.format(arg=arg))
385+
completions.append(f'--{arg}')
389386
return completions
390387

391388

fire/completion_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def testCompletionBashScript(self):
3333
self.assertIn('command', script)
3434
self.assertIn('halt', script)
3535

36-
assert_template = '{command})'
3736
for last_command in ['command', 'halt']:
38-
self.assertIn(assert_template.format(command=last_command), script)
37+
self.assertIn(f'{last_command})', script)
3938

4039
def testCompletionFishScript(self):
4140
# A sanity check test to make sure the fish completion script satisfies

fire/core.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ def Fire(component=None, command=None, name=None, serialize=None):
139139
_DisplayError(component_trace)
140140
raise FireExit(2, component_trace)
141141
if component_trace.show_trace and component_trace.show_help:
142-
output = ['Fire trace:\n{trace}\n'.format(trace=component_trace)]
142+
output = [f'Fire trace:\n{component_trace}\n']
143143
result = component_trace.GetResult()
144144
help_text = helptext.HelpText(
145145
result, trace=component_trace, verbose=component_trace.verbose)
146146
output.append(help_text)
147147
Display(output, out=sys.stderr)
148148
raise FireExit(0, component_trace)
149149
if component_trace.show_trace:
150-
output = ['Fire trace:\n{trace}'.format(trace=component_trace)]
150+
output = [f'Fire trace:\n{component_trace}']
151151
Display(output, out=sys.stderr)
152152
raise FireExit(0, component_trace)
153153
if component_trace.show_help:
@@ -231,9 +231,9 @@ def _IsHelpShortcut(component_trace, remaining_args):
231231

232232
if show_help:
233233
component_trace.show_help = True
234-
command = '{cmd} -- --help'.format(cmd=component_trace.GetCommand())
235-
print('INFO: Showing help with the command {cmd}.\n'.format(
236-
cmd=shlex.quote(command)), file=sys.stderr)
234+
command = f'{component_trace.GetCommand()} -- --help'
235+
print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
236+
file=sys.stderr)
237237
return show_help
238238

239239

@@ -287,9 +287,9 @@ def _DisplayError(component_trace):
287287
show_help = True
288288

289289
if show_help:
290-
command = '{cmd} -- --help'.format(cmd=component_trace.GetCommand())
291-
print('INFO: Showing help with the command {cmd}.\n'.format(
292-
cmd=shlex.quote(command)), file=sys.stderr)
290+
command = f'{component_trace.GetCommand()} -- --help'
291+
print(f'INFO: Showing help with the command {shlex.quote(command)}.\n',
292+
file=sys.stderr)
293293
help_text = helptext.HelpText(result, trace=component_trace,
294294
verbose=component_trace.verbose)
295295
output.append(help_text)
@@ -327,14 +327,13 @@ def _DictAsString(result, verbose=False):
327327
return '{}'
328328

329329
longest_key = max(len(str(key)) for key in result_visible.keys())
330-
format_string = '{{key:{padding}s}} {{value}}'.format(padding=longest_key + 1)
330+
format_string = f'{{key:{longest_key + 1}s}} {{value}}'
331331

332332
lines = []
333333
for key, value in result.items():
334334
if completion.MemberVisible(result, key, value, class_attrs=class_attrs,
335335
verbose=verbose):
336-
line = format_string.format(key=str(key) + ':',
337-
value=_OneLineResult(value))
336+
line = format_string.format(key=f'{key}:', value=_OneLineResult(value))
338337
lines.append(line)
339338
return '\n'.join(lines)
340339

@@ -348,10 +347,10 @@ def _OneLineResult(result):
348347
# TODO(dbieber): Show a small amount of usage information about the function
349348
# or module if it fits cleanly on the line.
350349
if inspect.isfunction(result):
351-
return '<function {name}>'.format(name=result.__name__)
350+
return f'<function {result.__name__}>'
352351

353352
if inspect.ismodule(result):
354-
return '<module {name}>'.format(name=result.__name__)
353+
return f'<module {result.__name__}>'
355354

356355
try:
357356
# Don't force conversion to ascii.
@@ -890,9 +889,10 @@ def _ParseKeywordArgs(args, fn_spec):
890889
if len(matching_fn_args) == 1:
891890
keyword = matching_fn_args[0]
892891
elif len(matching_fn_args) > 1:
893-
raise FireError("The argument '{}' is ambiguous as it could "
894-
"refer to any of the following arguments: {}".format(
895-
argument, matching_fn_args))
892+
raise FireError(
893+
f"The argument '{argument}' is ambiguous as it could "
894+
f"refer to any of the following arguments: {matching_fn_args}"
895+
)
896896

897897
# Determine the value.
898898
if not keyword:

0 commit comments

Comments
 (0)