From 757f1071aa20a3f814530cf442a71e0086f4ca99 Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Fri, 17 Jan 2020 15:08:35 +0200 Subject: [PATCH] Only add parentheses in parse_snippet in tests if needed --- tests/test_mark_tokens.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_mark_tokens.py b/tests/test_mark_tokens.py index dd25afe..efd8746 100644 --- a/tests/test_mark_tokens.py +++ b/tests/test_mark_tokens.py @@ -709,8 +709,11 @@ def parse_snippet(self, text, node): """ # If text is indented, it's a statement, and we need to put in a scope for indents to be valid # (using textwrap.dedent is insufficient because some lines may not indented, e.g. comments or - # multiline strings). If text is an expression but has newlines, we parenthesize it to make it - # parsable. + # multiline strings). + # If text is an expression but: + # - has newlines + # - or is an assignment expression + # we parenthesize it to make it parsable. # For expressions and statements, we add a dummy statement '_' before it because if it's just a # string contained in an astroid.Const or astroid.Expr it will end up in the doc attribute and be # a pain to extract for comparison @@ -718,7 +721,9 @@ def parse_snippet(self, text, node): if indented: return self.module.parse('def dummy():\n' + text).body[0].body[0] if util.is_expr(node): - return self.module.parse('_\n(' + text + ')').body[1].value + if '\n' in text or node.__class__.__name__ == 'NamedExpr': + text = '(' + text + ')' + return self.module.parse('_\n' + text).body[1].value if util.is_module(node): return self.module.parse(text) return self.module.parse('_\n' + text).body[1]