Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Surrounding parentheses in generator expressions #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions tests/test_mark_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,16 +709,21 @@ 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
indented = re.match(r'^[ \t]+\S', text)
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]
Expand Down