diff options
author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-02-21 14:26:22 +0100 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-02-21 15:36:16 +0100 |
commit | ee910755b90af58d6aa9b908b5afe4310c5b26db (patch) | |
tree | 347b74972f2b19ccab95a0bce5a70ffc35d41a2b | |
parent | 5bed07eba999130b6551acc7c43192d6a8eada43 (diff) | |
download | pylint-git-ee910755b90af58d6aa9b908b5afe4310c5b26db.tar.gz |
Migrate from % syntax or bad format() syntax to fstring
We can do that in python 3.6
-rw-r--r-- | pylint/checkers/base.py | 15 | ||||
-rw-r--r-- | pylint/checkers/classes.py | 2 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 6 | ||||
-rw-r--r-- | pylint/config/__init__.py | 2 | ||||
-rw-r--r-- | pylint/config/man_help_formatter.py | 72 | ||||
-rw-r--r-- | pylint/constants.py | 8 | ||||
-rw-r--r-- | pylint/message/message_definition.py | 8 | ||||
-rw-r--r-- | pylint/message/message_id_store.py | 12 | ||||
-rw-r--r-- | pylint/pyreverse/inspector.py | 8 | ||||
-rw-r--r-- | pylint/pyreverse/vcgutils.py | 7 | ||||
-rw-r--r-- | pylint/testutils/lint_module_test.py | 6 | ||||
-rw-r--r-- | pylint/utils/utils.py | 2 | ||||
-rw-r--r-- | tests/checkers/unittest_base.py | 22 | ||||
-rw-r--r-- | tests/checkers/unittest_spelling.py | 5 | ||||
-rw-r--r-- | tests/unittest_pyreverse_writer.py | 14 |
15 files changed, 62 insertions, 127 deletions
diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 647c201d9..0a836de4c 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -1331,7 +1331,6 @@ class BasicChecker(_BasicChecker): isinstance(value, astroid.Instance) and value.qname() in DEFAULT_ARGUMENT_SYMBOLS ): - if value is default: msg = DEFAULT_ARGUMENT_SYMBOLS[value.qname()] elif isinstance(value, astroid.Instance) or is_iterable(value): @@ -1350,10 +1349,7 @@ class BasicChecker(_BasicChecker): msg = f"{default.as_string()} ({value.qname()})" else: # this argument is a name - msg = "{} ({})".format( - default.as_string(), - DEFAULT_ARGUMENT_SYMBOLS[value.qname()], - ) + msg = f"{default.as_string()} ({DEFAULT_ARGUMENT_SYMBOLS[value.qname()]})" self.add_message("dangerous-default-value", node=node, args=(msg,)) @utils.check_messages("unreachable", "lost-exception") @@ -2437,14 +2433,9 @@ class ComparisonChecker(_BasicChecker): if checking_for_absence: absence_text = "not " if nan_left: - suggestion = "'{}math.isnan({})'".format( - absence_text, right_value.as_string() - ) + suggestion = f"'{absence_text}math.isnan({right_value.as_string()})'" else: - suggestion = "'{}math.isnan({})'".format( - absence_text, left_value.as_string() - ) - + suggestion = f"'{absence_text}math.isnan({left_value.as_string()})'" self.add_message( "nan-comparison", node=root_node, diff --git a/pylint/checkers/classes.py b/pylint/checkers/classes.py index aada6bbf3..83abf758f 100644 --- a/pylint/checkers/classes.py +++ b/pylint/checkers/classes.py @@ -1604,7 +1604,7 @@ a metaclass class method.", valid = repr(config[0]) else: valid = ", ".join(repr(v) for v in config[:-1]) - valid = "{} or {!r}".format(valid, config[-1]) + valid = f"{valid} or {config[-1]!r}" self.add_message(message, args=(method_name, valid), node=node) def _check_bases_classes(self, node): diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 01cf8ecbd..1e96f1b4e 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1643,11 +1643,7 @@ class VariablesChecker(BaseChecker): return if isinstance(stmt, astroid.ImportFrom): if asname is not None: - msg = "{} imported from {} as {}".format( - qname, - stmt.modname, - asname, - ) + msg = f"{qname} imported from {stmt.modname} as {asname}" else: msg = f"{name} imported from {stmt.modname}" self.add_message("unused-import", args=msg, node=stmt) diff --git a/pylint/config/__init__.py b/pylint/config/__init__.py index 41c303666..c893cdbea 100644 --- a/pylint/config/__init__.py +++ b/pylint/config/__init__.py @@ -68,7 +68,7 @@ else: def _get_pdata_path(base_name, recurs): base_name = base_name.replace(os.sep, "_") - return os.path.join(PYLINT_HOME, "{}{}{}".format(base_name, recurs, ".stats")) + return os.path.join(PYLINT_HOME, f"{base_name}{recurs}.stats") def load_results(base): diff --git a/pylint/config/man_help_formatter.py b/pylint/config/man_help_formatter.py index ab3b7e749..bd15e76a3 100644 --- a/pylint/config/man_help_formatter.py +++ b/pylint/config/man_help_formatter.py @@ -33,12 +33,9 @@ class _ManHelpFormatter(optparse.HelpFormatter): help_string = help_string.replace("[current:", "[default:") else: help_string = "" - return """.IP "{}" -{} -""".format( - optstring, - help_string, - ) + return f""".IP "{optstring}" +{help_string} +""" def format_head(self, optparser, pkginfo, section=1): long_desc = "" @@ -50,12 +47,10 @@ class _ManHelpFormatter(optparse.HelpFormatter): short_desc = self.format_short_description(pgm, pkginfo.description) if hasattr(pkginfo, "long_desc"): long_desc = self.format_long_description(pgm, pkginfo.long_desc) - return "{}\n{}\n{}\n{}".format( - self.format_title(pgm, section), - short_desc, - self.format_synopsis(pgm), - long_desc, - ) + return f"""{self.format_title(pgm, section)} +{short_desc} +{self.format_synopsis(pgm)} +{long_desc}""" @staticmethod def format_title(pgm, section): @@ -64,27 +59,21 @@ class _ManHelpFormatter(optparse.HelpFormatter): @staticmethod def format_short_description(pgm, short_desc): - return """.SH NAME -.B {} -\\- {} -""".format( - pgm, - short_desc.strip(), - ) + return f""".SH NAME +.B {pgm} +\\- {short_desc.strip()} +""" @staticmethod def format_synopsis(pgm): - return ( - """.SH SYNOPSIS -.B %s + return f""".SH SYNOPSIS +.B {pgm} [ .I OPTIONS ] [ .I <arguments> ] """ - % pgm - ) @staticmethod def format_long_description(pgm, long_desc): @@ -92,41 +81,28 @@ class _ManHelpFormatter(optparse.HelpFormatter): long_desc = long_desc.replace("\n.\n", "\n\n") if long_desc.lower().startswith(pgm): long_desc = long_desc[len(pgm) :] - return """.SH DESCRIPTION -.B {} -{} -""".format( - pgm, - long_desc.strip(), - ) + return f""".SH DESCRIPTION +.B {pgm} +{long_desc.strip()} +""" @staticmethod def format_tail(pkginfo): - tail = """.SH SEE ALSO -/usr/share/doc/pythonX.Y-{}/ + tail = f""".SH SEE ALSO +/usr/share/doc/pythonX.Y-{getattr(pkginfo, "debian_name", "pylint")}/ .SH BUGS Please report bugs on the project\'s mailing list: -{} +{pkginfo.mailinglist} .SH AUTHOR -{} <{}> -""".format( - getattr(pkginfo, "debian_name", "pylint"), - pkginfo.mailinglist, - pkginfo.author, - pkginfo.author_email, - ) - +{pkginfo.author} <{pkginfo.author_email}> +""" if hasattr(pkginfo, "copyright"): - tail += ( - """ + tail += f""" .SH COPYRIGHT -%s +{pkginfo.copyright} """ - % pkginfo.copyright - ) - return tail diff --git a/pylint/constants.py b/pylint/constants.py index f6f3b189b..929eed97e 100644 --- a/pylint/constants.py +++ b/pylint/constants.py @@ -44,8 +44,6 @@ class WarningScope: NODE = "node-based-msg" -full_version = "pylint {}\nastroid {}\nPython {}".format( - pylint_version, - astroid_version, - sys.version, -) +full_version = f"""pylint {pylint_version} +astroid {astroid_version} +Python { sys.version}""" diff --git a/pylint/message/message_definition.py b/pylint/message/message_definition.py index 3fb9af945..6f5f0dc51 100644 --- a/pylint/message/message_definition.py +++ b/pylint/message/message_definition.py @@ -39,17 +39,15 @@ class MessageDefinition: @staticmethod def check_msgid(msgid: str) -> None: if len(msgid) != 5: - raise InvalidMessageError("Invalid message id %r" % msgid) + raise InvalidMessageError(f"Invalid message id {msgid!r}") if msgid[0] not in MSG_TYPES: - raise InvalidMessageError( - "Bad message type {} in {!r}".format(msgid[0], msgid) - ) + raise InvalidMessageError(f"Bad message type {msgid[0]} in {msgid!r}") def __repr__(self): return f"MessageDefinition:{self.symbol} ({self.msgid})" def __str__(self): - return "{}:\n{} {}".format(repr(self), self.msg, self.description) + return f"{repr(self)}:\n{self.msg} {self.description}" def may_be_emitted(self): """return True if message may be emitted using the current interpreter""" diff --git a/pylint/message/message_id_store.py b/pylint/message/message_id_store.py index 0395f0878..438136cb5 100644 --- a/pylint/message/message_id_store.py +++ b/pylint/message/message_id_store.py @@ -81,9 +81,7 @@ class MessageIdStore: symbols = [symbol, other_symbol] symbols.sort() error_message = f"Message id '{msgid}' cannot have both " - error_message += "'{other_symbol}' and '{symbol}' as symbolic name.".format( - other_symbol=symbols[0], symbol=symbols[1] - ) + error_message += f"'{symbols[0]}' and '{symbols[1]}' as symbolic name." raise InvalidMessageError(error_message) @staticmethod @@ -97,10 +95,10 @@ class MessageIdStore: msgids = [msgid, other_msgid] msgids.sort() error_message = ( - "Message symbol '{symbol}' cannot be used for " - "'{other_msgid}' and '{msgid}' at the same time." - " If you're creating an 'old_names' use 'old-{symbol}' as the old symbol." - ).format(symbol=symbol, other_msgid=msgids[0], msgid=msgids[1]) + f"Message symbol '{symbol}' cannot be used for " + f"'{msgids[0]}' and '{msgids[1]}' at the same time." + f" If you're creating an 'old_names' use 'old-{symbol}' as the old symbol." + ) raise InvalidMessageError(error_message) def get_active_msgids(self, msgid_or_symbol: str) -> List[str]: diff --git a/pylint/pyreverse/inspector.py b/pylint/pyreverse/inspector.py index 17ef46bff..cdae95e6b 100644 --- a/pylint/pyreverse/inspector.py +++ b/pylint/pyreverse/inspector.py @@ -261,7 +261,7 @@ class Linker(IdGeneratorMixIn, utils.LocalsVisitor): if name[0] == "*": continue # analyze dependencies - fullname = "{}.{}".format(basename, name[0]) + fullname = f"{basename}.{name[0]}" if fullname.find(".") > -1: try: fullname = modutils.get_module_part(fullname, context_file) @@ -319,11 +319,7 @@ class Project: return self.modules def __repr__(self): - return "<Project {!r} at {} ({} modules)>".format( - self.name, - id(self), - len(self.modules), - ) + return f"<Project {self.name!r} at {id(self)} ({len(self.modules)} modules)>" def project_from_files( diff --git a/pylint/pyreverse/vcgutils.py b/pylint/pyreverse/vcgutils.py index 36e82032a..3041b076e 100644 --- a/pylint/pyreverse/vcgutils.py +++ b/pylint/pyreverse/vcgutils.py @@ -204,14 +204,13 @@ possible attributes are %s""" if not _type: self._stream.write(f'{self._indent}{key}:"{value}"\n') elif _type == 1: - self._stream.write("{}{}:{}\n".format(self._indent, key, int(value))) + self._stream.write(f"{self._indent}{key}:{int(value)}\n") elif value in _type: self._stream.write(f"{self._indent}{key}:{value}\n") else: raise Exception( - """value %s isn\'t correct for attribute %s -correct values are %s""" - % (value, key, _type) + f"""value {value} isn't correct for attribute {key} +correct values are {type}""" ) def _inc_indent(self): diff --git a/pylint/testutils/lint_module_test.py b/pylint/testutils/lint_module_test.py index 5b9dc5c7a..c0dc4f002 100644 --- a/pylint/testutils/lint_module_test.py +++ b/pylint/testutils/lint_module_test.py @@ -70,11 +70,7 @@ class LintModuleTest: ) def __str__(self): - return "{} ({}.{})".format( - self._test_file.base, - self.__class__.__module__, - self.__class__.__name__, - ) + return f"{self._test_file.base} ({self.__class__.__module__}.{self.__class__.__name__})" @staticmethod def get_expected_messages(stream): diff --git a/pylint/utils/utils.py b/pylint/utils/utils.py index 0f4e0f518..981bfb91b 100644 --- a/pylint/utils/utils.py +++ b/pylint/utils/utils.py @@ -52,7 +52,7 @@ def get_module_and_frameid(node): def get_rst_title(title, character): """Permit to get a title formatted as ReStructuredText test (underlined with a chosen character).""" - return "{}\n{}\n".format(title, character * len(title)) + return f"{title}\n{character * len(title)}\n" def get_rst_section(section, options, doc=None): diff --git a/tests/checkers/unittest_base.py b/tests/checkers/unittest_base.py index f8371d3ad..7534e0fbb 100644 --- a/tests/checkers/unittest_base.py +++ b/tests/checkers/unittest_base.py @@ -551,23 +551,17 @@ class TestNamePresets(unittest.TestCase): for name, name_type in always_pass_data: self._test_is_correct(naming_style, name, name_type) - def _test_is_correct(self, naming_style, name, name_type): + @staticmethod + def _test_is_correct(naming_style, name, name_type): rgx = naming_style.get_regex(name_type) - self.assertTrue( - rgx.match(name), - "{!r} does not match pattern {!r} (style: {}, type: {})".format( - name, rgx, naming_style, name_type - ), - ) + fail = f"{name!r} does not match pattern {rgx!r} (style: {naming_style}, type: {name_type})" + assert rgx.match(name), fail - def _test_is_incorrect(self, naming_style, name, name_type): + @staticmethod + def _test_is_incorrect(naming_style, name, name_type): rgx = naming_style.get_regex(name_type) - self.assertFalse( - rgx.match(name), - "{!r} match pattern {!r} but shouldn't (style: {}, type: {})".format( - name, rgx, naming_style, name_type - ), - ) + fail = f"{name!r} not match pattern {rgx!r} (style: {naming_style}, type: {name_type})" + assert not rgx.match(name), fail def test_snake_case(self): naming_style = base.SnakeCaseStyle diff --git a/tests/checkers/unittest_spelling.py b/tests/checkers/unittest_spelling.py index 80fdc3888..165199563 100644 --- a/tests/checkers/unittest_spelling.py +++ b/tests/checkers/unittest_spelling.py @@ -44,9 +44,8 @@ class TestSpellingChecker(CheckerTestCase): ) def _get_msg_suggestions(self, word, count=4): - return "'{}'".format( - "' or '".join(self.checker.spelling_dict.suggest(word)[:count]) - ) + suggestions = "' or '".join(self.checker.spelling_dict.suggest(word)[:count]) + return f"'{suggestions}'" @skip_on_missing_package_or_dict @set_config(spelling_dict=spell_dict) diff --git a/tests/unittest_pyreverse_writer.py b/tests/unittest_pyreverse_writer.py index 20f128574..1c2767e9a 100644 --- a/tests/unittest_pyreverse_writer.py +++ b/tests/unittest_pyreverse_writer.py @@ -106,12 +106,10 @@ def test_dot_files(generated_file): generated = "\n".join(generated) expected = "\n".join(expected) files = f"\n *** expected : {expected_file}, generated : {generated_file} \n" - assert expected == generated, "{}{}".format( - files, - "\n".join( - line for line in unified_diff(expected.splitlines(), generated.splitlines()) - ), + diff = "\n".join( + line for line in unified_diff(expected.splitlines(), generated.splitlines()) ) + assert expected == generated, f"{files}{diff}" os.remove(generated_file) @@ -130,8 +128,4 @@ def test_dot_files(generated_file): def test_get_visibility(names, expected): for name in names: got = get_visibility(name) - assert got == expected, "got {} instead of {} for value {}".format( - got, - expected, - name, - ) + assert got == expected, f"got {got} instead of {expected} for value {name}" |