8000 gh-111420: Allow type comments in parenthesized `with` statements (#1… · python/cpython@453e96e · GitHub
[go: up one dir, main page]

Skip to content

Commit 453e96e

Browse files
authored
gh-111420: Allow type comments in parenthesized with statements (#111468)
1 parent faa5f60 commit 453e96e

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ for_stmt[stmt_ty]:
391391

392392
with_stmt[stmt_ty]:
393393
| invalid_with_stmt_indent
394-
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
395-
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
394+
| 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' tc=[TYPE_COMMENT] b=block {
395+
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
396396
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
397397
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
398398
| 'async' 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {

Lib/test/test_ast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def to_tuple(t):
100100
# With
101101
"with x as y: pass",
102102
"with x as y, z as q: pass",
103+
"with (x as y): pass",
104+
"with (x, y): pass",
103105
# Raise
104106
"raise Exception('string')",
105107
# TryExcept
@@ -3015,6 +3017,8 @@ def main():
30153017
('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))])])], []),
30163018
('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)], []),
30173019
('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)], []),
3020+
('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)], []),
3021+
('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)], []),
30183022
('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)], []),
30193023
('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))])], [], [])], []),
30203024
('Module', [('Try', (1, 0, 4, 6), [('Pass', (2, 2, 2, 6))], [], [], [('Pass', (4, 2, 4, 6))])], []),

Lib/test/test_type_comments.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ def foo():
6666
pass
6767
"""
6868

69+
parenthesized_withstmt = """\
70+
with (a as b): # type: int
71+
pass
72+
73+
with (a, b): # type: int
74+
pass
75+
"""
76+
6977
vardecl = """\
7078
a = 0 # type: int
7179
"""
@@ -300,6 +308,14 @@ def test_withstmt(self):
300308
tree = self.classic_parse(withstmt)
301309
self.assertEqual(tree.body[0].type_comment, None)
302310

311+
def test_parenthesized_withstmt(self):
312+
for tree in self.parse_all(parenthesized_withstmt, minver=9):
313+
self.assertEqual(tree.body[0].type_comment, "int")
314+
self.assertEqual(tree.body[1].type_comment, "int")
315+
tree = self.classic_parse(parenthesized_withstmt)
316+
self.assertEqual(tree.body[0].type_comment, None)
317+
self.assertEqual(tree.body[1].type_comment, None)
318+
303319
def test_vardecl(self):
304320
for tree in self.parse_all(vardecl):
305321
self.assertEqual(tree.body[0].type_comment, "int")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow type comments in parenthesized ``with`` statements

Parser/parser.c

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0