From a45e25e3125f6ee0a9f32387545df318b0b3b2d0 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 23 Nov 2022 13:33:10 +0200 Subject: Replace %/.format/concatenation with f-strings where feasible (#927) Original conversion suggestions via flynt, edited by hand. --- tests/messages/test_catalog.py | 4 ++-- tests/messages/test_frontend.py | 13 +++++++------ tests/messages/test_pofile.py | 2 +- tests/test_core.py | 3 +-- tests/test_plural.py | 2 +- tests/test_support.py | 9 +++++---- 6 files changed, 17 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/messages/test_catalog.py b/tests/messages/test_catalog.py index cc5fb49..e9b15a8 100644 --- a/tests/messages/test_catalog.py +++ b/tests/messages/test_catalog.py @@ -357,7 +357,7 @@ def test_catalog_mime_headers(): ('MIME-Version', '1.0'), ('Content-Type', 'text/plain; charset=utf-8'), ('Content-Transfer-Encoding', '8bit'), - ('Generated-By', 'Babel %s\n' % catalog.VERSION), + ('Generated-By', f'Babel {catalog.VERSION}\n'), ] @@ -380,7 +380,7 @@ def test_catalog_mime_headers_set_locale(): ('MIME-Version', '1.0'), ('Content-Type', 'text/plain; charset=utf-8'), ('Content-Transfer-Encoding', '8bit'), - ('Generated-By', 'Babel %s\n' % catalog.VERSION), + ('Generated-By', f'Babel {catalog.VERSION}\n'), ] diff --git a/tests/messages/test_frontend.py b/tests/messages/test_frontend.py index 0eb7e8f..9e3ac9f 100644 --- a/tests/messages/test_frontend.py +++ b/tests/messages/test_frontend.py @@ -130,7 +130,7 @@ class ExtractMessagesTestCase(unittest.TestCase): assert ('file1.py' in msg.locations[0][0]) def test_input_paths_handle_spaces_after_comma(self): - self.cmd.input_paths = '%s, %s' % (this_dir, data_dir) + self.cmd.input_paths = f"{this_dir}, {data_dir}" self.cmd.output_file = pot_file self.cmd.finalize_options() assert self.cmd.input_paths == [this_dir, data_dir] @@ -1118,7 +1118,7 @@ msgstr[2] "" self.cli.run(sys.argv + ['compile', '--locale', 'de_DE', '-d', i18n_dir]) - assert not os.path.isfile(mo_file), 'Expected no file at %r' % mo_file + assert not os.path.isfile(mo_file), f'Expected no file at {mo_file!r}' assert sys.stderr.getvalue() == f'catalog {po_file} is marked as fuzzy, skipping\n' def test_compile_fuzzy_catalog(self): @@ -1397,14 +1397,15 @@ def test_extract_keyword_args_384(split, arg_name): ] if split: # Generate a command line with multiple -ks - kwarg_text = " ".join("%s %s" % (arg_name, kwarg_spec) for kwarg_spec in kwarg_specs) + kwarg_text = " ".join(f"{arg_name} {kwarg_spec}" for kwarg_spec in kwarg_specs) else: # Generate a single space-separated -k - kwarg_text = "%s \"%s\"" % (arg_name, " ".join(kwarg_specs)) + specs = ' '.join(kwarg_specs) + kwarg_text = f'{arg_name} "{specs}"' # (Both of those invocation styles should be equivalent, so there is no parametrization from here on out) cmdinst = configure_cli_command( - "extract -F babel-django.cfg --add-comments Translators: -o django232.pot %s ." % kwarg_text + f"extract -F babel-django.cfg --add-comments Translators: -o django232.pot {kwarg_text} ." ) assert isinstance(cmdinst, extract_messages) assert set(cmdinst.keywords.keys()) == {'_', 'dgettext', 'dngettext', @@ -1489,7 +1490,7 @@ def test_extract_error_code(monkeypatch, capsys): def test_extract_ignore_dirs(monkeypatch, capsys, tmp_path, with_underscore_ignore): pot_file = tmp_path / 'temp.pot' monkeypatch.chdir(project_dir) - cmd = "extract . -o '{}' --ignore-dirs '*ignored*' ".format(pot_file) + cmd = f"extract . -o '{pot_file}' --ignore-dirs '*ignored*' " if with_underscore_ignore: # This also tests that multiple arguments are supported. cmd += "--ignore-dirs '_*'" diff --git a/tests/messages/test_pofile.py b/tests/messages/test_pofile.py index 99e59ba..a72368b 100644 --- a/tests/messages/test_pofile.py +++ b/tests/messages/test_pofile.py @@ -859,7 +859,7 @@ class PofileFunctionsTestCase(unittest.TestCase): expected_denormalized = u'multi-line\n translation' assert expected_denormalized == pofile.denormalize(msgstr) - assert expected_denormalized == pofile.denormalize('""\n' + msgstr) + assert expected_denormalized == pofile.denormalize(f'""\n{msgstr}') def test_unknown_language_roundtrip(): diff --git a/tests/test_core.py b/tests/test_core.py index 2de79e2..605bf5c 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -309,8 +309,7 @@ def test_compatible_classes_in_global_and_localedata(filename): # *.dat files must have compatible classes between Python 2 and 3 if module.split('.')[0] == 'babel': return pickle.Unpickler.find_class(self, module, name) - raise pickle.UnpicklingError("global '%s.%s' is forbidden" % - (module, name)) + raise pickle.UnpicklingError(f"global '{module}.{name}' is forbidden") with open(filename, 'rb') as f: assert Unpickler(f).load() diff --git a/tests/test_plural.py b/tests/test_plural.py index 4210033..16601cf 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -273,7 +273,7 @@ def test_gettext_compilation(locale): chars = 'ivwft' # Test that these rules are valid for this test; i.e. that they contain at least one # of the gettext-unsupported characters. - assert any((" " + ch + " ") in rule for ch in chars for rule in ru_rules.values()) + assert any(f" {ch} " in rule for ch in chars for rule in ru_rules.values()) # Then test that the generated value indeed does not contain these. ru_rules_gettext = plural.to_gettext(ru_rules) assert not any(ch in ru_rules_gettext for ch in chars) diff --git a/tests/test_support.py b/tests/test_support.py index 9447107..c73e53b 100644 --- a/tests/test_support.py +++ b/tests/test_support.py @@ -63,8 +63,7 @@ class TranslationsTestCase(unittest.TestCase): def assertEqualTypeToo(self, expected, result): assert expected == result - assert type(expected) == type(result), "instance type's do not " + \ - "match: %r!=%r" % (type(expected), type(result)) + assert type(expected) == type(result), f"instance types do not match: {type(expected)!r}!={type(result)!r}" def test_pgettext(self): self.assertEqualTypeToo('Voh', self.translations.gettext('foo')) @@ -210,7 +209,7 @@ class NullTranslationsTestCase(unittest.TestCase): def test_same_methods(self): for name in self.method_names(): if not hasattr(self.null_translations, name): - self.fail('NullTranslations does not provide method %r' % name) + self.fail(f"NullTranslations does not provide method {name!r}") def test_method_signature_compatibility(self): for name in self.method_names(): @@ -346,11 +345,13 @@ def test_format_percent(): def test_lazy_proxy(): def greeting(name='world'): - return u'Hello, %s!' % name + return f"Hello, {name}!" + lazy_greeting = support.LazyProxy(greeting, name='Joe') assert str(lazy_greeting) == u"Hello, Joe!" assert u' ' + lazy_greeting == u' Hello, Joe!' assert u'(%s)' % lazy_greeting == u'(Hello, Joe!)' + assert f"[{lazy_greeting}]" == "[Hello, Joe!]" greetings = [ support.LazyProxy(greeting, 'world'), -- cgit v1.2.1