]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add checks that no type comments are returned without type_comments=True
authorGuido van Rossum <guido@python.org>
Mon, 28 Jan 2019 01:28:09 +0000 (17:28 -0800)
committerGuido van Rossum <guido@python.org>
Mon, 28 Jan 2019 16:35:45 +0000 (08:35 -0800)
Lib/test/test_type_comments.py

index a50f976fed0781df050bed4f372eeff49d2d4fd4..ac5b5de59e322a8e3de66c24761765c78f637da2 100644 (file)
@@ -148,23 +148,36 @@ class TypeCommentTests(unittest.TestCase):
     def parse(self, source):
         return ast.parse(source, type_comments=True)
 
+    def classic_parse(self, source):
+        return ast.parse(source)
+
     def test_funcdef(self):
         tree = self.parse(funcdef)
         self.assertEqual(tree.body[0].type_comment, "() -> int")
         self.assertEqual(tree.body[1].type_comment, "() -> None")
+        tree = self.classic_parse(funcdef)
+        self.assertEqual(tree.body[0].type_comment, None)
+        self.assertEqual(tree.body[1].type_comment, None)
 
     def test_asyncdef(self):
         tree = self.parse(asyncdef)
         self.assertEqual(tree.body[0].type_comment, "() -> int")
         self.assertEqual(tree.body[1].type_comment, "() -> int")
+        tree = self.classic_parse(asyncdef)
+        self.assertEqual(tree.body[0].type_comment, None)
+        self.assertEqual(tree.body[1].type_comment, None)
 
     def test_forstmt(self):
         tree = self.parse(forstmt)
         self.assertEqual(tree.body[0].type_comment, "int")
+        tree = self.classic_parse(forstmt)
+        self.assertEqual(tree.body[0].type_comment, None)
 
     def test_withstmt(self):
         tree = self.parse(withstmt)
         self.assertEqual(tree.body[0].type_comment, "int")
+        tree = self.classic_parse(withstmt)
+        self.assertEqual(tree.body[0].type_comment, None)
 
     def test_vardecl(self):
         tree = self.parse(vardecl)
@@ -172,10 +185,14 @@ class TypeCommentTests(unittest.TestCase):
         # Curious fact: an expression can have a type comment but it
         # is lost in the AST, so we have this in the example source
         # code but don't test it here.
+        tree = self.classic_parse(vardecl)
+        self.assertEqual(tree.body[0].type_comment, None)
 
     def test_ignores(self):
         tree = self.parse(ignores)
         self.assertEqual([ti.lineno for ti in tree.type_ignores], [2, 5])
+        tree = self.classic_parse(ignores)
+        self.assertEqual(tree.type_ignores, [])
 
     def test_longargs(self):
         tree = self.parse(longargs)
@@ -197,6 +214,12 @@ class TypeCommentTests(unittest.TestCase):
                 self.assertEqual(arg.arg, c)  # That's the argument name
                 self.assertEqual(arg.type_comment, arg.arg.upper())
             assert not todo
+        tree = self.classic_parse(longargs)
+        for t in tree.body:
+            for arg in t.args.args + [t.args.vararg, t.args.kwarg]:
+                if arg is not None:
+                    self.assertIsNone(arg.type_comment, "%s(%s:%r)" %
+                                      (t.name, arg.arg, arg.type_comment))
 
     def test_inappropriate_type_comments(self):
         """Tests for inappropriately-placed type comments.