mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
bpo-38870: Implement a precedence algorithm in ast.unparse (GH-17377)
Implement a simple precedence algorithm for ast.unparse in order to avoid redundant parenthesis for nested structures in the final output.
This commit is contained in:
parent
185903de12
commit
397b96f6d7
3 changed files with 172 additions and 16 deletions
|
|
@ -247,6 +247,13 @@ eval_tests = [
|
|||
|
||||
class AST_Tests(unittest.TestCase):
|
||||
|
||||
def _is_ast_node(self, name, node):
|
||||
if not isinstance(node, type):
|
||||
return False
|
||||
if "ast" not in node.__module__:
|
||||
return False
|
||||
return name != 'AST' and name[0].isupper()
|
||||
|
||||
def _assertTrueorder(self, ast_node, parent_pos):
|
||||
if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
|
||||
return
|
||||
|
|
@ -335,7 +342,7 @@ class AST_Tests(unittest.TestCase):
|
|||
|
||||
def test_field_attr_existence(self):
|
||||
for name, item in ast.__dict__.items():
|
||||
if isinstance(item, type) and name != 'AST' and name[0].isupper():
|
||||
if self._is_ast_node(name, item):
|
||||
x = item()
|
||||
if isinstance(x, ast.AST):
|
||||
self.assertEqual(type(x._fields), tuple)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue