Skip to content

Commit e15c209

Browse files
committed
Automated refactoring to replace ancient print % formatting with f-strings amd the like
1 parent c9fa34d commit e15c209

19 files changed

+46
-52
lines changed

cmd2/history.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ class to gain access to the historical record.
165165
_history_items_field = 'history_items'
166166

167167
def __init__(self, seq: Iterable[HistoryItem] = ()) -> None:
168-
super(History, self).__init__(seq)
168+
super().__init__(seq)
169169
self.session_start_index = 0
170170

171171
def start_session(self) -> None:
@@ -192,7 +192,7 @@ def append(self, new: Union[Statement, HistoryItem]) -> None:
192192
and added to the end of the list
193193
"""
194194
history_item = HistoryItem(new) if isinstance(new, Statement) else new
195-
super(History, self).append(history_item)
195+
super().append(history_item)
196196

197197
def clear(self) -> None:
198198
"""Remove all items from the History list."""

examples/cmd_as_argument.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def do_speak(self, args):
4949
words = []
5050
for word in args.words:
5151
if args.piglatin:
52-
word = '%s%say' % (word[1:], word[0])
52+
word = f'{word[1:]}{word[0]}ay'
5353
if args.shout:
5454
word = word.upper()
5555
words.append(word)

examples/colors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def do_speak(self, args):
6363
words = []
6464
for word in args.words:
6565
if args.piglatin:
66-
word = '%s%say' % (word[1:], word[0])
66+
word = f'{word[1:]}{word[0]}ay'
6767
if args.shout:
6868
word = word.upper()
6969
words.append(word)

examples/decorator_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def do_speak(self, args: argparse.Namespace):
4646
words = []
4747
for word in args.words:
4848
if args.piglatin:
49-
word = '%s%say' % (word[1:], word[0])
49+
word = f'{word[1:]}{word[0]}ay'
5050
if args.shout:
5151
word = word.upper()
5252
words.append(word)

examples/default_categories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ExampleApp(cmd2.Cmd):
7575
"""
7676

7777
def __init__(self):
78-
super(ExampleApp, self).__init__()
78+
super().__init__()
7979

8080
def do_something(self, arg):
8181
self.poutput('this is the something command')

examples/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def do_speak(self, args):
4545
words = []
4646
for word in args.words:
4747
if args.piglatin:
48-
word = '%s%say' % (word[1:], word[0])
48+
word = f'{word[1:]}{word[0]}ay'
4949
if args.shout:
5050
word = word.upper()
5151
words.append(word)

examples/first_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def do_speak(self, args):
3939
words = []
4040
for word in args.words:
4141
if args.piglatin:
42-
word = '%s%say' % (word[1:], word[0])
42+
word = f'{word[1:]}{word[0]}ay'
4343
if args.shout:
4444
word = word.upper()
4545
words.append(word)

examples/modular_commands_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ExampleApp(cmd2.Cmd):
2828
"""
2929

3030
def __init__(self):
31-
super(ExampleApp, self).__init__()
31+
super().__init__()
3232

3333
def do_something(self, arg):
3434
self.poutput('this is the something command')

examples/subcommands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def base_foo(self, args):
7777

7878
def base_bar(self, args):
7979
"""bar subcommand of base command"""
80-
self.poutput('((%s))' % args.z)
80+
self.poutput(f'(({args.z}))')
8181

8282
def base_sport(self, args):
8383
"""sport subcommand of base command"""

tests/test_argparse.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def do_say(self, args, *, keyword_arg: Optional[str] = None):
4747
if word is None:
4848
word = ''
4949
if args.piglatin:
50-
word = '%s%say' % (word[1:], word[0])
50+
word = f'{word[1:]}{word[0]}ay'
5151
if args.shout:
5252
word = word.upper()
5353
words.append(word)
@@ -102,7 +102,7 @@ def do_speak(self, args, extra, *, keyword_arg: Optional[str] = None):
102102
if word is None:
103103
word = ''
104104
if args.piglatin:
105-
word = '%s%say' % (word[1:], word[0])
105+
word = f'{word[1:]}{word[0]}ay'
106106
if args.shout:
107107
word = word.upper()
108108
words.append(word)
@@ -266,11 +266,11 @@ def base_foo(self, args):
266266

267267
def base_bar(self, args):
268268
"""bar subcommand of base command"""
269-
self.poutput('((%s))' % args.z)
269+
self.poutput(f'(({args.z}))')
270270

271271
def base_helpless(self, args):
272272
"""helpless subcommand of base command"""
273-
self.poutput('((%s))' % args.z)
273+
self.poutput(f'(({args.z}))')
274274

275275
# create the top-level parser for the base command
276276
base_parser = cmd2.Cmd2ArgumentParser()

tests/test_argparse_completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
12101210
if action.get_complete_when_ready() is True and not app.is_ready:
12111211
matched_flags.append(flag)
12121212

1213-
return super(CustomCompleter, self)._complete_flags(text, line, begidx, endidx, matched_flags)
1213+
return super()._complete_flags(text, line, begidx, endidx, matched_flags)
12141214

12151215

12161216
# Add a custom argparse action attribute

tests/test_cmd2.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,14 @@ def test_run_script_nested_run_scripts(base_app, request):
477477
assert base_app.last_result is True
478478

479479
# Check that the right commands were executed.
480-
expected = (
481-
"""
482-
%s
480+
expected = f"""
481+
{initial_run}
483482
_relative_run_script precmds.txt
484483
set allow_style Always
485484
help
486485
shortcuts
487486
_relative_run_script postcmds.txt
488487
set allow_style Never"""
489-
% initial_run
490-
)
491488
out, err = run_cmd(base_app, 'history -s')
492489
assert out == normalize(expected)
493490

@@ -498,16 +495,13 @@ def test_runcmds_plus_hooks(base_app, request):
498495
postfilepath = os.path.join(test_dir, 'scripts', 'postcmds.txt')
499496

500497
base_app.runcmds_plus_hooks(['run_script ' + prefilepath, 'help', 'shortcuts', 'run_script ' + postfilepath])
501-
expected = """
502-
run_script %s
498+
expected = f"""
499+
run_script {prefilepath}
503500
set allow_style Always
504501
help
505502
shortcuts
506-
run_script %s
507-
set allow_style Never""" % (
508-
prefilepath,
509-
postfilepath,
510-
)
503+
run_script {postfilepath}
504+
set allow_style Never"""
511505

512506
out, err = run_cmd(base_app, 'history -s')
513507
assert out == normalize(expected)

tests/test_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def base_foo(self, args):
12251225

12261226
def base_bar(self, args):
12271227
"""bar subcommand of base command"""
1228-
self.poutput('((%s))' % args.z)
1228+
self.poutput(f'(({args.z}))')
12291229

12301230
def base_sport(self, args):
12311231
"""sport subcommand of base command"""

tests/test_transcript.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def do_speak(self, opts, arg):
5353
"""Repeats what you tell me to."""
5454
arg = ' '.join(arg)
5555
if opts.piglatin:
56-
arg = '%s%say' % (arg[1:], arg[0])
56+
arg = f'{arg[1:]}{arg[0]}ay'
5757
if opts.shout:
5858
arg = arg.upper()
5959
repetitions = opts.repeat or 1

tests/test_utils_defining_class.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def parent_only_func(self, param1, param2):
1717

1818
class ChildClass(ParentClass):
1919
def func_with_overrides(self):
20-
super(ChildClass, self).func_with_overrides()
20+
super().func_with_overrides()
2121

2222
def child_function(self):
2323
pass

tests_isolated/test_commandset/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ class WithCommandSets(ExternalTestMixin, cmd2.Cmd):
178178
"""Class for testing custom help_* methods which override docstring help."""
179179

180180
def __init__(self, *args, **kwargs):
181-
super(WithCommandSets, self).__init__(*args, **kwargs)
181+
super().__init__(*args, **kwargs)
182182

183183

184184
@pytest.fixture

tests_isolated/test_commandset/test_argparse_subcommands.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class SubcommandSet(cmd2.CommandSet):
1616
"""Example cmd2 application where we a base command which has a couple subcommands."""
1717

1818
def __init__(self, dummy):
19-
super(SubcommandSet, self).__init__()
19+
super().__init__()
2020

2121
# subcommand functions for the base command
2222
def base_foo(self, args):
@@ -25,11 +25,11 @@ def base_foo(self, args):
2525

2626
def base_bar(self, args):
2727
"""bar subcommand of base command"""
28-
self._cmd.poutput('((%s))' % args.z)
28+
self._cmd.poutput(f'(({args.z}))')
2929

3030
def base_helpless(self, args):
3131
"""helpless subcommand of base command"""
32-
self._cmd.poutput('((%s))' % args.z)
32+
self._cmd.poutput(f'(({args.z}))')
3333

3434
# create the top-level parser for the base command
3535
base_parser = cmd2.Cmd2ArgumentParser()

tests_isolated/test_commandset/test_categories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class MyBaseCommandSet(CommandSet):
1616
"""Defines a default category for all sub-class CommandSets"""
1717

1818
def __init__(self, _: Any):
19-
super(MyBaseCommandSet, self).__init__()
19+
super().__init__()
2020

2121

2222
class ChildInheritsParentCategories(MyBaseCommandSet):
@@ -79,7 +79,7 @@ class ExampleApp(cmd2.Cmd):
7979
"""
8080

8181
def __init__(self):
82-
super(ExampleApp, self).__init__(auto_load_commands=False)
82+
super().__init__(auto_load_commands=False)
8383

8484
def do_something(self, arg):
8585
self.poutput('this is the something command')

tests_isolated/test_commandset/test_commandset.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def test_load_commandset_errors(command_sets_manual, capsys):
384384

385385
class LoadableBase(cmd2.CommandSet):
386386
def __init__(self, dummy):
387-
super(LoadableBase, self).__init__()
387+
super().__init__()
388388
self._dummy = dummy # prevents autoload
389389
self._cut_called = False
390390

@@ -444,7 +444,7 @@ def stir_pasta(self, ns: argparse.Namespace):
444444

445445
class LoadableBadBase(cmd2.CommandSet):
446446
def __init__(self, dummy):
447-
super(LoadableBadBase, self).__init__()
447+
super().__init__()
448448
self._dummy = dummy # prevents autoload
449449

450450
def do_cut(self, ns: argparse.Namespace):
@@ -462,7 +462,7 @@ def do_cut(self, ns: argparse.Namespace):
462462
@cmd2.with_default_category('Fruits')
463463
class LoadableFruits(cmd2.CommandSet):
464464
def __init__(self, dummy):
465-
super(LoadableFruits, self).__init__()
465+
super().__init__()
466466
self._dummy = dummy # prevents autoload
467467

468468
def do_apple(self, _: cmd2.Statement):
@@ -479,7 +479,7 @@ def cut_banana(self, ns: argparse.Namespace):
479479

480480
class LoadablePastaStir(cmd2.CommandSet):
481481
def __init__(self, dummy):
482-
super(LoadablePastaStir, self).__init__()
482+
super().__init__()
483483
self._dummy = dummy # prevents autoload
484484

485485
stir_pasta_vigor_parser = cmd2.Cmd2ArgumentParser()
@@ -493,7 +493,7 @@ def stir_pasta_vigorously(self, ns: argparse.Namespace):
493493
@cmd2.with_default_category('Vegetables')
494494
class LoadableVegetables(cmd2.CommandSet):
495495
def __init__(self, dummy):
496-
super(LoadableVegetables, self).__init__()
496+
super().__init__()
497497
self._dummy = dummy # prevents autoload
498498

499499
def do_arugula(self, _: cmd2.Statement):
@@ -679,7 +679,7 @@ def test_nested_subcommands(command_sets_manual):
679679

680680
class BadNestedSubcommands(cmd2.CommandSet):
681681
def __init__(self, dummy):
682-
super(BadNestedSubcommands, self).__init__()
682+
super().__init__()
683683
self._dummy = dummy # prevents autoload
684684

685685
stir_pasta_vigor_parser = cmd2.Cmd2ArgumentParser()
@@ -712,7 +712,7 @@ class AppWithSubCommands(cmd2.Cmd):
712712
"""Class for testing usage of `as_subcommand_to` decorator directly in a Cmd2 subclass."""
713713

714714
def __init__(self, *args, **kwargs):
715-
super(AppWithSubCommands, self).__init__(*args, **kwargs)
715+
super().__init__(*args, **kwargs)
716716

717717
cut_parser = cmd2.Cmd2ArgumentParser()
718718
cut_subparsers = cut_parser.add_subparsers(title='item', help='item to cut')
@@ -790,7 +790,7 @@ class SupportFuncProvider(cmd2.CommandSet):
790790

791791
def __init__(self, dummy):
792792
"""dummy variable prevents this from being autoloaded in other tests"""
793-
super(SupportFuncProvider, self).__init__()
793+
super().__init__()
794794

795795
def complete_states(self, text: str, line: str, begidx: int, endidx: int) -> list[str]:
796796
assert self is complete_states_expected_self
@@ -824,7 +824,7 @@ class SupportFuncUserUnrelated(cmd2.CommandSet):
824824

825825
def __init__(self, dummy):
826826
"""dummy variable prevents this from being autoloaded in other tests"""
827-
super(SupportFuncUserUnrelated, self).__init__()
827+
super().__init__()
828828

829829
parser = cmd2.Cmd2ArgumentParser()
830830
parser.add_argument('state', type=str, completer=SupportFuncProvider.complete_states)
@@ -969,7 +969,7 @@ def test_cross_commandset_completer(command_sets_manual, capsys):
969969
class CommandSetWithPathComplete(cmd2.CommandSet):
970970
def __init__(self, dummy):
971971
"""dummy variable prevents this from being autoloaded in other tests"""
972-
super(CommandSetWithPathComplete, self).__init__()
972+
super().__init__()
973973

974974
parser = cmd2.Cmd2ArgumentParser()
975975
parser.add_argument('path', nargs='+', help='paths', completer=cmd2.Cmd.path_complete)
@@ -998,7 +998,7 @@ class BadSubcommandApp(cmd2.Cmd):
998998
"""Class for testing usage of `as_subcommand_to` decorator directly in a Cmd2 subclass."""
999999

10001000
def __init__(self, *args, **kwargs):
1001-
super(BadSubcommandApp, self).__init__(*args, **kwargs)
1001+
super().__init__(*args, **kwargs)
10021002

10031003
cut_parser = cmd2.Cmd2ArgumentParser()
10041004
cut_subparsers = cut_parser.add_subparsers(title='item', help='item to cut')
@@ -1028,7 +1028,7 @@ def __init__(self):
10281028
# Declare a CommandSet with a settable of some arbitrary property
10291029
class WithSettablesA(CommandSetBase):
10301030
def __init__(self):
1031-
super(WithSettablesA, self).__init__()
1031+
super().__init__()
10321032

10331033
self._arbitrary = Arbitrary()
10341034
self._settable_prefix = 'addon'
@@ -1047,7 +1047,7 @@ def __init__(self):
10471047
# Declare a CommandSet with an empty settable prefix
10481048
class WithSettablesNoPrefix(CommandSetBase):
10491049
def __init__(self):
1050-
super(WithSettablesNoPrefix, self).__init__()
1050+
super().__init__()
10511051

10521052
self._arbitrary = Arbitrary()
10531053
self._settable_prefix = ''
@@ -1066,7 +1066,7 @@ def __init__(self):
10661066
# Declare a commandset with duplicate settable name
10671067
class WithSettablesB(CommandSetBase):
10681068
def __init__(self):
1069-
super(WithSettablesB, self).__init__()
1069+
super().__init__()
10701070

10711071
self._arbitrary = Arbitrary()
10721072
self._settable_prefix = 'some'
@@ -1200,7 +1200,7 @@ class NsProviderSet(cmd2.CommandSet):
12001200
# CommandSet which implements a namespace provider
12011201
def __init__(self, dummy):
12021202
# Use dummy argument so this won't be autoloaded by other tests
1203-
super(NsProviderSet, self).__init__()
1203+
super().__init__()
12041204

12051205
def ns_provider(self) -> argparse.Namespace:
12061206
ns = argparse.Namespace()
@@ -1213,7 +1213,7 @@ class NsProviderApp(cmd2.Cmd):
12131213
# Used to test namespace providers in CommandSets
12141214
def __init__(self, *args, **kwargs) -> None:
12151215
super().__init__(*args, **kwargs)
1216-
super(NsProviderApp, self).__init__(*args, **kwargs)
1216+
super().__init__(*args, **kwargs)
12171217

12181218
@cmd2.with_argparser(cmd2.Cmd2ArgumentParser(), ns_provider=NsProviderSet.ns_provider)
12191219
def do_test_ns(self, args: argparse.Namespace) -> None:

0 commit comments

Comments
 (0)