Skip to content

Commit

Permalink
Continue removing .format in favor of f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dbieber committed Sep 20, 2024
1 parent da2fa86 commit a888f48
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion examples/widget/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def whack(self, n=1):

def bang(self, noise='bang'):
"""Makes a loud noise."""
return '{noise} bang!'.format(noise=noise)
return f'{noise} bang!'


def main():
Expand Down
7 changes: 2 additions & 5 deletions fire/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ def _FishScript(name, commands, default_options=None):
)

return fish_source.format(
global_options=' '.join(
'"{option}"'.format(option=option)
for option in global_options
)
global_options=' '.join(f'"{option}"' for option in global_options)
)


Expand Down Expand Up @@ -385,7 +382,7 @@ def _CompletionsFromArgs(fn_args):
completions = []
for arg in fn_args:
arg = arg.replace('_', '-')
completions.append('--{arg}'.format(arg=arg))
completions.append(f'--{arg}')
return completions


Expand Down
3 changes: 1 addition & 2 deletions fire/completion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ def testCompletionBashScript(self):
self.assertIn('command', script)
self.assertIn('halt', script)

assert_template = '{command})'
for last_command in ['command', 'halt']:
self.assertIn(assert_template.format(command=last_command), script)
self.assertIn(f'{last_command})', script)

def testCompletionFishScript(self):
# A sanity check test to make sure the fish completion script satisfies
Expand Down
30 changes: 11 additions & 19 deletions fire/helptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ def _SynopsisSection(component, actions_grouped_by_kind, spec, metadata,
continuations.append(trace.separator)
continuation = ' | '.join(continuations)

synopsis_template = '{current_command} {continuation}'
text = synopsis_template.format(
current_command=current_command,
continuation=continuation)

text = f'{current_command} {continuation}'
return ('SYNOPSIS', text)


Expand Down Expand Up @@ -243,8 +239,6 @@ def _ArgsAndFlagsSections(info, spec, metadata):
if spec.varkw:
# Include kwargs documented via :key param:
documented_kwargs = []
flag_string = '--{name}'
short_flag_string = '-{short_name}, --{name}'

# add short flags if possible
flags = docstring_info.args or []
Expand All @@ -253,11 +247,10 @@ def _ArgsAndFlagsSections(info, spec, metadata):
for flag in flags:
if isinstance(flag, docstrings.KwargInfo):
if flag.name[0] in unique_short_flags:
flag_string = short_flag_string.format(
name=flag.name, short_name=flag.name[0]
)
short_name = flag.name[0]
flag_string = f'-{short_name}, --{flag.name}'
else:
flag_string = flag_string.format(name=flag.name)
flag_string = f'--{flag.name}'

flag_item = _CreateFlagItem(
flag.name, docstring_info, spec,
Expand Down Expand Up @@ -484,29 +477,28 @@ def _CreateFlagItem(flag, docstring_info, spec, required=False,
description = _GetArgDescription(flag, docstring_info)

if not flag_string:
flag_string_template = '--{flag_name}={flag_name_upper}'
flag_string = flag_string_template.format(
flag_name=flag,
flag_name_upper=formatting.Underline(flag.upper()))
flag_name_upper=formatting.Underline(flag.upper())
flag_string = f'--{flag}={flag_name_upper}'
if required:
flag_string += ' (required)'
if short_arg:
flag_string = '-{short_flag}, '.format(short_flag=flag[0]) + flag_string
short_flag = flag[0]
flag_string = f'-{short_flag}, ' + flag_string

arg_type = _GetArgType(flag, spec)
arg_default = _GetArgDefault(flag, spec)

# We need to handle the case where there is a default of None, but otherwise
# the argument has another type.
if arg_default == 'None':
arg_type = 'Optional[{}]'.format(arg_type)
arg_type = f'Optional[{arg_type}]'

arg_type = 'Type: {}'.format(arg_type) if arg_type else ''
arg_type = f'Type: {arg_type}' if arg_type else ''
available_space = max_str_length - len(arg_type)
arg_type = (
formatting.EllipsisTruncate(arg_type, available_space, max_str_length))

arg_default = 'Default: {}'.format(arg_default) if arg_default else ''
arg_default = f'Default: {arg_default}' if arg_default else ''
available_space = max_str_length - len(arg_default)
arg_default = (
formatting.EllipsisTruncate(arg_default, available_space, max_str_length))
Expand Down

0 comments on commit a888f48

Please sign in to comment.