]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Don't allow type comments on expression statements
authorGuido van Rossum <guido@python.org>
Mon, 28 Jan 2019 01:45:13 +0000 (17:45 -0800)
committerGuido van Rossum <guido@python.org>
Mon, 28 Jan 2019 16:35:45 +0000 (08:35 -0800)
Includes tests.

Also tweak a comment in Grammar/Grammar.

Grammar/Grammar
Lib/test/test_type_comments.py
Python/graminit.c

index 7f736711ee016f4f8dfb1131a214abc858517a6a..e125b72cdfc63e0ecef8512b8d18b31c9626e258 100644 (file)
@@ -9,8 +9,7 @@
 #       eval_input is the input for the eval() functions.
 #       func_type_input is a PEP 484 Python 2 function type comment
 # NB: compound_stmt in single_input is followed by extra NEWLINE!
-# NB: due to the way TYPE_COMMENT is tokenized it will always be followed by a
-#      NEWLINE
+# NB: due to the way TYPE_COMMENT is tokenized it will always be followed by a NEWLINE
 single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
 file_input: (NEWLINE | stmt)* ENDMARKER
 eval_input: testlist NEWLINE* ENDMARKER
@@ -42,7 +41,7 @@ simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
 small_stmt: (expr_stmt | del_stmt | pass_stmt | flow_stmt |
              import_stmt | global_stmt | nonlocal_stmt | assert_stmt)
 expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) |
-                     ('=' (yield_expr|testlist_star_expr))* [TYPE_COMMENT])
+                     [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] )
 annassign: ':' test ['=' (yield_expr|testlist)]
 testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [',']
 augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' |
index 9b393a274d3398579034511c5c204a6eaa63c2e1..5ed60c35a2f5f5b6634363e1de2cc6911a7a4a3b 100644 (file)
@@ -33,7 +33,6 @@ with context() as a:  # type: int
 
 vardecl = """\
 a = 0  # type: int
-a  # type: int
 """
 
 ignores = """\
@@ -182,9 +181,6 @@ class TypeCommentTests(unittest.TestCase):
     def test_vardecl(self):
         tree = self.parse(vardecl)
         self.assertEqual(tree.body[0].type_comment, "int")
-        # 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)
 
@@ -236,6 +232,7 @@ class TypeCommentTests(unittest.TestCase):
                 ast.parse(source, type_comments=True)
 
         check_both_ways("pass  # type: int\n")
+        check_both_ways("foo()  # type: int\n")
         check_both_ways("x += 1  # type: int\n")
         check_both_ways("while True:  # type: int\n  continue\n")
         check_both_ways("while True:\n  continue  # type: int\n")
index 516eca85b1a50f0ec3e81816609657273c2a0992..6e0f19891baa01c7e691b90b98087425d4917511 100644 (file)
@@ -501,11 +501,10 @@ static state states_15[2] = {
 static arc arcs_16_0[1] = {
     {48, 1},
 };
-static arc arcs_16_1[5] = {
+static arc arcs_16_1[4] = {
     {49, 2},
     {50, 3},
     {32, 4},
-    {28, 2},
     {0, 1},
 };
 static arc arcs_16_2[1] = {
@@ -526,7 +525,7 @@ static arc arcs_16_5[3] = {
 };
 static state states_16[6] = {
     {1, arcs_16_0},
-    {5, arcs_16_1},
+    {4, arcs_16_1},
     {1, arcs_16_2},
     {2, arcs_16_3},
     {2, arcs_16_4},