Skip to content

Commit

Permalink
pythongh-111420: Allow type comments in parenthesized with statemen…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 authored and Glyphack committed Jan 27, 2024
1 parent 53e8c9c commit d9fe5c8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ for_stmt[stmt_ty]:

with_stmt[stmt_ty]:
| invalid_with_stmt_indent
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' tc=[TYPE_COMMENT] b=block {
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
| 'async' 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def to_tuple(t):
# With
"with x as y: pass",
"with x as y, z as q: pass",
"with (x as y): pass",
"with (x, y): pass",
# Raise
"raise Exception('string')",
# TryExcept
Expand Down Expand Up @@ -3015,6 +3017,8 @@ def main():
('Module', [('If', (1, 0, 6, 6), ('Name', (1, 3, 1, 4), 'a', ('Load',)), [('Pass', (2, 2, 2, 6))], [('If', (3, 0, 6, 6), ('Name', (3, 5, 3, 6), 'b', ('Load',)), [('Pass', (4, 2, 4, 6))], [('Pass', (6, 2, 6, 6))])])], []),
('Module', [('With', (1, 0, 1, 17), [('withitem', ('Name', (1, 5, 1, 6), 'x', ('Load',)), ('Name', (1, 10, 1, 11), 'y', ('Store',)))], [('Pass', (1, 13, 1, 17))], None)], []),
('Module', [('With', (1, 0, 1, 25), [('withitem', ('Name', (1, 5, 1, 6), 'x', ('Load',)), ('Name', (1, 10, 1, 11), 'y', ('Store',))), ('withitem', ('Name', (1, 13, 1, 14), 'z', ('Load',)), ('Name', (1, 18, 1, 19), 'q', ('Store',)))], [('Pass', (1, 21, 1, 25))], None)], []),
('Module', [('With', (1, 0, 1, 19), [('withitem', ('Name', (1, 6, 1, 7), 'x', ('Load',)), ('Name', (1, 11, 1, 12), 'y', ('Store',)))], [('Pass', (1, 15, 1, 19))], None)], []),
('Module', [('With', (1, 0, 1, 17), [('withitem', ('Name', (1, 6, 1, 7), 'x', ('Load',)), None), ('withitem', ('Name', (1, 9, 1, 10), 'y', ('Load',)), None)], [('Pass', (1, 13, 1, 17))], None)], []),
('Module', [('Raise', (1, 0, 1, 25), ('Call', (1, 6, 1, 25), ('Name', (1, 6, 1, 15), 'Exception', ('Load',)), [('Constant', (1, 16, 1, 24), 'string', None)], []), None)], []),
('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [('ExceptHandler', (3, 0, 4, 6), ('Name', (3, 7, 3, 16), 'Exception', ('Load',)), None, [('Pass', (4, 2, 4, 6))])], [], [])], []),
('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [], [], [('Pass', (4, 2, 4, 6))])], []),
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_type_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def foo():
pass
"""

parenthesized_withstmt = """\
with (a as b): # type: int
pass
with (a, b): # type: int
pass
"""

vardecl = """\
a = 0 # type: int
"""
Expand Down Expand Up @@ -300,6 +308,14 @@ def test_withstmt(self):
tree = self.classic_parse(withstmt)
self.assertEqual(tree.body[0].type_comment, None)

def test_parenthesized_withstmt(self):
for tree in self.parse_all(parenthesized_withstmt, minver=9):
self.assertEqual(tree.body[0].type_comment, "int")
self.assertEqual(tree.body[1].type_comment, "int")
tree = self.classic_parse(parenthesized_withstmt)
self.assertEqual(tree.body[0].type_comment, None)
self.assertEqual(tree.body[1].type_comment, None)

def test_vardecl(self):
for tree in self.parse_all(vardecl):
self.assertEqual(tree.body[0].type_comment, "int")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow type comments in parenthesized ``with`` statements
15 changes: 9 additions & 6 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d9fe5c8

Please sign in to comment.