From cef9f31a99793e806e4ca2a764c52d0e1652773d Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Sun, 27 Jan 2019 17:45:13 -0800 Subject: [PATCH] Don't allow type comments on expression statements Includes tests. Also tweak a comment in Grammar/Grammar. --- Grammar/Grammar | 5 ++--- Lib/test/test_type_comments.py | 5 +---- Python/graminit.c | 5 ++--- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Grammar/Grammar b/Grammar/Grammar index 7f736711ee01..e125b72cdfc6 100644 --- a/Grammar/Grammar +++ b/Grammar/Grammar @@ -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: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | diff --git a/Lib/test/test_type_comments.py b/Lib/test/test_type_comments.py index 9b393a274d33..5ed60c35a2f5 100644 --- a/Lib/test/test_type_comments.py +++ b/Lib/test/test_type_comments.py @@ -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") diff --git a/Python/graminit.c b/Python/graminit.c index 516eca85b1a5..6e0f19891baa 100644 --- a/Python/graminit.c +++ b/Python/graminit.c @@ -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}, -- 2.47.3