bpo-43224: Implement PEP 646 grammar changes (GH-31018)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Matthew Rahtz 2022-03-26 16:55:35 +00:00 committed by GitHub
parent 26cca8067b
commit e8e737bcf6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 4324 additions and 3396 deletions

View file

@ -1476,20 +1476,17 @@ class _Unparser(NodeVisitor):
self.traverse(e)
def visit_Subscript(self, node):
def is_simple_tuple(slice_value):
# when unparsing a non-empty tuple, the parentheses can be safely
# omitted if there aren't any elements that explicitly requires
# parentheses (such as starred expressions).
def is_non_empty_tuple(slice_value):
return (
isinstance(slice_value, Tuple)
and slice_value.elts
and not any(isinstance(elt, Starred) for elt in slice_value.elts)
)
self.set_precedence(_Precedence.ATOM, node.value)
self.traverse(node.value)
with self.delimit("[", "]"):
if is_simple_tuple(node.slice):
if is_non_empty_tuple(node.slice):
# parentheses can be omitted if the tuple isn't empty
self.items_view(self.traverse, node.slice.elts)
else:
self.traverse(node.slice)