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

Parenthesize singleton yield tuples #3912

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<!-- Changes that affect Black's stable style -->

- Fix comments getting removed from inside parenthesized strings (#3909)
- Parenthesize singleton tuples in `yield` expressions as they already are in `return`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go in the preview style, not the stable style. (That's also why CI is failing.)

statements, e.g. `yield 5,` ➡️ `yield (5,)` (#3912)

### Preview style

Expand Down
16 changes: 16 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ def visit_stmt(

yield from self.visit(child)

def visit_yield_expr(self, node: Node) -> Iterator[Line]:
"""
If a "yield expr", treat similarly to "return expr",
but if it is a "yield from expr", treat expr as a group
"""
content = node.children[1]
if content.type != syms.yield_arg:
# this is a "yield expr"
yield from self.visit_stmt(node, keywords=set(), parens={"yield"})
return
# this is a "yield from expr"
assert isinstance(content, Node)
wrap_in_parentheses(content, content.children[1], visible=False)
yield from self.visit_default(node)

def visit_typeparams(self, node: Node) -> Iterator[Line]:
yield from self.visit_default(node)
node.children[0].prefix = ""
Expand Down Expand Up @@ -1414,6 +1429,7 @@ def maybe_make_parens_invisible_in_atom(
syms.expr_stmt,
syms.assert_stmt,
syms.return_stmt,
syms.yield_expr,
syms.except_clause,
syms.funcdef,
syms.with_stmt,
Expand Down
101 changes: 101 additions & 0 deletions tests/data/simple_cases/parenthesized_yield_tuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
async def main():
yield x
(yield x)
await (yield x)


# Regression test for https://github.com/psf/black/issues/3851.
def f():
yield x,
return x,

yield (y,)
yield from (y,)
return (y,)

yield this_variable_is_over_the_line_length_limit_of_88_______________________________________,
return this_variable_is_over_the_line_length_limit_of_88_______________________________________,

yield (this_variable_is_over_the_line_length_limit_of_88_______________________________________,)
yield from (this_variable_is_over_the_line_length_limit_of_88_______________________________________,)
return (this_variable_is_over_the_line_length_limit_of_88_______________________________________,)

yield """
multiline string
""",
return """
multiline string
""",

yield ("""
multiline string
""",)
yield from ("""
multiline string
""",)
return ("""
multiline string
""",)


# output


async def main():
yield x
(yield x)
await (yield x)


# Regression test for https://github.com/psf/black/issues/3851.
def f():
yield (x,)
return (x,)

yield (y,)
yield from (y,)
return (y,)

yield (
this_variable_is_over_the_line_length_limit_of_88_______________________________________,
)
return (
this_variable_is_over_the_line_length_limit_of_88_______________________________________,
)

yield (
this_variable_is_over_the_line_length_limit_of_88_______________________________________,
)
yield from (
this_variable_is_over_the_line_length_limit_of_88_______________________________________,
)
return (
this_variable_is_over_the_line_length_limit_of_88_______________________________________,
)

yield (
"""
multiline string
""",
)
return (
"""
multiline string
""",
)

yield (
"""
multiline string
""",
)
yield from (
"""
multiline string
""",
)
return (
"""
multiline string
""",
)
Loading