about summary refs log tree commit diff stats
path: root/tests/pylint
diff options
context:
space:
mode:
Diffstat (limited to 'tests/pylint')
-rw-r--r--tests/pylint/py2_compat.py8
-rw-r--r--tests/pylint/test_py2_compat.py25
2 files changed, 33 insertions, 0 deletions
diff --git a/tests/pylint/py2_compat.py b/tests/pylint/py2_compat.py
index 7e136148..e0353260 100644
--- a/tests/pylint/py2_compat.py
+++ b/tests/pylint/py2_compat.py
@@ -51,6 +51,9 @@ class Py2CompatibilityChecker(BaseChecker):
                   "Python 2 subprocess.Popen objects were not contextmanagers,"
                   "popen23.Popen wraps them to enable use with"
                   "with-statements."),
+        "E4240": ("Use format method",
+                  "use-format-method",
+                  "Python 2 (and <3.6) does not support f-strings."),
     }
     # This class variable declares the options
     # that are configurable by the user.
@@ -121,6 +124,11 @@ class Py2CompatibilityChecker(BaseChecker):
                     self.add_message("implicit-format-spec", node=node,
                                      confidence=HIGH)
 
+    def visit_joinedstr(self, node):
+        """Make sure we don't use f-strings"""
+        if isinstance(node, astroid.nodes.JoinedStr):
+            self.add_message("use-format-method", node=node, confidence=HIGH)
+
     def visit_with(self, node):
         """Make sure subprocess.Popen objects aren't used in with-statements"""
         for (cm, _) in node.items:
diff --git a/tests/pylint/test_py2_compat.py b/tests/pylint/test_py2_compat.py
index 2eb51599..33fc5681 100644
--- a/tests/pylint/test_py2_compat.py
+++ b/tests/pylint/test_py2_compat.py
@@ -4,6 +4,7 @@ import py2_compat
 
 import astroid
 import pylint.testutils
+from pylint.interfaces import HIGH
 
 from sys import version_info
 PY2 = version_info[0] < 3
@@ -25,6 +26,7 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase):
             pylint.testutils.MessageTest(
                 msg_id='old-style-class',
                 node=oldstyle_class,
+                confidence=HIGH,
             ),
         ):
             self.checker.visit_classdef(oldstyle_class)
@@ -56,6 +58,7 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase):
             pylint.testutils.MessageTest(
                 msg_id='print-without-import',
                 node=print_function_call,
+                confidence=HIGH,
             ),
         ):
             self.checker.visit_call(print_function_call)
@@ -95,6 +98,7 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase):
             pylint.testutils.MessageTest(
                 msg_id='print-without-import',
                 node=early_print_function_call,
+                confidence=HIGH,
             ),
         ):
             self.checker.visit_call(early_print_function_call)
@@ -111,6 +115,7 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase):
             pylint.testutils.MessageTest(
                 msg_id='implicit-format-spec',
                 node=implicit_format_spec,
+                confidence=HIGH,
             ),
         ):
             self.checker.visit_call(implicit_format_spec)
@@ -134,6 +139,7 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase):
             pylint.testutils.MessageTest(
                 msg_id='with-popen23',
                 node=with_Popen,
+                confidence=HIGH,
             ),
         ):
             self.checker.visit_with(with_subprocess_Popen)
@@ -141,6 +147,25 @@ class TestPy2CompatibilityChecker(pylint.testutils.CheckerTestCase):
         with self.assertNoMessages():
             self.checker.visit_with(with_Popen23)
 
+    def test_use_format(self):
+        old_format, new_format, f_string = astroid.extract_node("""
+            "2 + 2 is %s" % (2+2) #@
+            "2 + 2 is {0}".format(2+2) #@
+            f"2 + 2 is {2+2}" #@
+        """)
+
+        with self.assertAddsMessages(
+            pylint.testutils.MessageTest(
+                msg_id='use-format-method',
+                node=f_string,
+                confidence=HIGH,
+            ),
+        ):
+            self.checker.visit_joinedstr(f_string)
+        with self.assertNoMessages():
+            self.checker.visit_joinedstr(old_format)
+            self.checker.visit_joinedstr(new_format)
+
     # # These checks still exist as old-division and no-absolute-import
     # def test_division_without_import(self):
     #     division = astroid.extract_node("""