From bbdc28e080c5fd60172e2c52f98bc92f4d713069 Mon Sep 17 00:00:00 2001 From: Eddie Darling Date: Mon, 2 Oct 2023 02:33:46 -0700 Subject: [PATCH 1/4] Add tests --- .../simple_cases/parenthesized_yield_tuple.py | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/data/simple_cases/parenthesized_yield_tuple.py diff --git a/tests/data/simple_cases/parenthesized_yield_tuple.py b/tests/data/simple_cases/parenthesized_yield_tuple.py new file mode 100644 index 00000000000..5cbde4a7c22 --- /dev/null +++ b/tests/data/simple_cases/parenthesized_yield_tuple.py @@ -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 + """, + ) From 9afc3fd747e2410e8051b4e5dfbd8cffdf09c816 Mon Sep 17 00:00:00 2001 From: Eddie Darling Date: Mon, 2 Oct 2023 02:40:21 -0700 Subject: [PATCH 2/4] Parenthesize single element tuples in yield expressions Fixes #3851 --- src/black/linegen.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/black/linegen.py b/src/black/linegen.py index 9ddd4619f69..4e111b762af 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -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 = "" @@ -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, From be3753ce61a7fce1b7266f548f5f81048d341c87 Mon Sep 17 00:00:00 2001 From: Eddie Darling Date: Mon, 2 Oct 2023 02:55:58 -0700 Subject: [PATCH 3/4] Add entry to CHANGES --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 5e518497c92..22a1c3a9d64 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,8 @@ - Fix comments getting removed from inside parenthesized strings (#3909) +- Parenthesize singleton tuples in `yield` expressions as they already are in `return` statements, + e.g. `yield 5,` ➡️ `yield (5,)` (#3912) ### Preview style From ebefed856d9ab465eda8675d0c76cd902a0a77ee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 09:57:01 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 22a1c3a9d64..2784420ae6f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,8 +11,8 @@ - Fix comments getting removed from inside parenthesized strings (#3909) -- Parenthesize singleton tuples in `yield` expressions as they already are in `return` statements, - e.g. `yield 5,` ➡️ `yield (5,)` (#3912) +- Parenthesize singleton tuples in `yield` expressions as they already are in `return` + statements, e.g. `yield 5,` ➡️ `yield (5,)` (#3912) ### Preview style