summaryrefslogtreecommitdiff
path: root/pylint/testutils
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-08-30 07:57:05 +0200
committerGitHub <noreply@github.com>2021-08-30 07:57:05 +0200
commit66ffcbc4c59e41327be1a2b5ef65727bf0314aa9 (patch)
tree5b1f37a0ffadc2888f851337b36f75717ebf4c72 /pylint/testutils
parent1a19421058dcc04446f8b91a825ed1078959d87a (diff)
downloadpylint-git-66ffcbc4c59e41327be1a2b5ef65727bf0314aa9.tar.gz
Add ``Consider-using-f-string`` checker (#4796)
* Add ``consider-using-f-string`` checker This adds a checker for normal strings which are formatted with ``.format()`` or '%'. The message is a convention to nudge users towards using f-strings. This closes #3592 * Update pylint code to use f-strings After adding `consider-using-f-strings` the codebase showed numerous cases of formatting which could be f-strings. This commit changes most of these to become f-strings, or adds ignores. * Apply suggestions from code review Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'pylint/testutils')
-rw-r--r--pylint/testutils/constants.py6
-rw-r--r--pylint/testutils/lint_module_test.py16
-rw-r--r--pylint/testutils/reporter_for_tests.py2
3 files changed, 14 insertions, 10 deletions
diff --git a/pylint/testutils/constants.py b/pylint/testutils/constants.py
index fa70d3b57..fc04d927d 100644
--- a/pylint/testutils/constants.py
+++ b/pylint/testutils/constants.py
@@ -7,7 +7,9 @@ import sys
from os.path import abspath, dirname
from pathlib import Path
-SYS_VERS_STR = "%d%d%d" % sys.version_info[:3]
+SYS_VERS_STR = (
+ "%d%d%d" % sys.version_info[:3] # pylint: disable=consider-using-f-string
+)
TITLE_UNDERLINES = ["", "=", "-", "."]
PREFIX = abspath(dirname(__file__))
UPDATE_OPTION = "--update-functional-output"
@@ -20,7 +22,7 @@ _MESSAGE = {"msg": r"[a-z][a-z\-]+"}
# - followed by a list of bracketed message symbols.
# Used to extract expected messages from testdata files.
_EXPECTED_RE = re.compile(
- r"\s*#\s*(?:(?P<line>[+-]?[0-9]+):)?"
+ r"\s*#\s*(?:(?P<line>[+-]?[0-9]+):)?" # pylint: disable=consider-using-f-string
r"(?:(?P<op>[><=]+) *(?P<version>[0-9.]+):)?"
r"\s*\[(?P<msgs>%(msg)s(?:,\s*%(msg)s)*)]" % _MESSAGE
)
diff --git a/pylint/testutils/lint_module_test.py b/pylint/testutils/lint_module_test.py
index 8f24daf86..2b5ad2dcb 100644
--- a/pylint/testutils/lint_module_test.py
+++ b/pylint/testutils/lint_module_test.py
@@ -53,8 +53,7 @@ class LintModuleTest:
def setUp(self):
if self._should_be_skipped_due_to_version():
pytest.skip(
- "Test cannot run with Python %s."
- % sys.version.split(" ", maxsplit=1)[0]
+ f"Test cannot run with Python {sys.version.split(' ', maxsplit=1)[0]}."
)
missing = []
for requirement in self._test_file.options["requires"]:
@@ -63,7 +62,7 @@ class LintModuleTest:
except ImportError:
missing.append(requirement)
if missing:
- pytest.skip("Requires %s to be present." % ",".join(missing))
+ pytest.skip(f"Requires {','.join(missing)} to be present.")
except_implementations = self._test_file.options["except_implementations"]
if except_implementations:
implementations = [i.strip() for i in except_implementations.split(",")]
@@ -74,7 +73,7 @@ class LintModuleTest:
if excluded_platforms:
platforms = [p.strip() for p in excluded_platforms.split(",")]
if sys.platform.lower() in platforms:
- pytest.skip("Test cannot run on platform %r" % sys.platform)
+ pytest.skip(f"Test cannot run on platform {sys.platform!r}")
def runTest(self):
self._runTest()
@@ -193,16 +192,19 @@ class LintModuleTest:
def error_msg_for_unequal_messages(
self, actual_messages, expected_messages, actual_output: List[OutputLine]
):
- msg = ['Wrong results for file "%s":' % (self._test_file.base)]
+ msg = [f'Wrong results for file "{self._test_file.base}":']
missing, unexpected = self.multiset_difference(
expected_messages, actual_messages
)
if missing:
msg.append("\nExpected in testdata:")
- msg.extend(" %3d: %s" % msg for msg in sorted(missing))
+ msg.extend(
+ " %3d: %s" % msg # pylint: disable=consider-using-f-string
+ for msg in sorted(missing)
+ )
if unexpected:
msg.append("\nUnexpected in testdata:")
- msg.extend(" %3d: %s" % msg for msg in sorted(unexpected)) # type: ignore
+ msg.extend(" %3d: %s" % msg for msg in sorted(unexpected)) # type: ignore #pylint: disable=consider-using-f-string
error_msg = "\n".join(msg)
if self._config and self._config.getoption("verbose") > 0:
error_msg += "\n\nActual pylint output for this file:\n"
diff --git a/pylint/testutils/reporter_for_tests.py b/pylint/testutils/reporter_for_tests.py
index 45aa63577..b1b7af7e4 100644
--- a/pylint/testutils/reporter_for_tests.py
+++ b/pylint/testutils/reporter_for_tests.py
@@ -32,7 +32,7 @@ class GenericTestReporter(BaseReporter):
str_message: str = msg.msg
self.message_ids[msg_id] = 1
if obj:
- obj = ":%s" % obj
+ obj = f":{obj}"
sigle = msg_id[0]
if linesep != "\n":
# 2to3 writes os.linesep instead of using