From 226cbf0226ee3bc26972357ba54c36409e9a84ae Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 30 Jan 2023 18:53:14 -0800 Subject: [PATCH 1/1] Fix unsafe cast in linegen.py w/ await yield handling (#3533) Fixes #3532. --- CHANGES.md | 1 + src/black/linegen.py | 25 ++++++++++++----------- tests/data/preview/remove_await_parens.py | 7 +++++++ 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2acb31d..8c6a4f4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -42,6 +42,7 @@ - Fix an invalid quote escaping bug in f-string expressions where it produced invalid code. Implicitly concatenated f-strings with different quotes can now be merged or quote-normalized by changing the quotes used in expressions. (#3509) +- Fix crash on `await (yield)` when Black is compiled with mypyc (#3533) ### Configuration diff --git a/src/black/linegen.py b/src/black/linegen.py index bfc28ca..9894a39 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1223,18 +1223,19 @@ def remove_await_parens(node: Node) -> None: # N.B. We've still removed any redundant nested brackets though :) opening_bracket = cast(Leaf, node.children[1].children[0]) closing_bracket = cast(Leaf, node.children[1].children[-1]) - bracket_contents = cast(Node, node.children[1].children[1]) - if bracket_contents.type != syms.power: - ensure_visible(opening_bracket) - ensure_visible(closing_bracket) - elif ( - bracket_contents.type == syms.power - and bracket_contents.children[0].type == token.AWAIT - ): - ensure_visible(opening_bracket) - ensure_visible(closing_bracket) - # If we are in a nested await then recurse down. - remove_await_parens(bracket_contents) + bracket_contents = node.children[1].children[1] + if isinstance(bracket_contents, Node): + if bracket_contents.type != syms.power: + ensure_visible(opening_bracket) + ensure_visible(closing_bracket) + elif ( + bracket_contents.type == syms.power + and bracket_contents.children[0].type == token.AWAIT + ): + ensure_visible(opening_bracket) + ensure_visible(closing_bracket) + # If we are in a nested await then recurse down. + remove_await_parens(bracket_contents) def _maybe_wrap_cms_in_parens( diff --git a/tests/data/preview/remove_await_parens.py b/tests/data/preview/remove_await_parens.py index 571210a..8c7223d 100644 --- a/tests/data/preview/remove_await_parens.py +++ b/tests/data/preview/remove_await_parens.py @@ -77,6 +77,9 @@ async def main(): async def main(): await (await (await (await (await (asyncio.sleep(1)))))) +async def main(): + await (yield) + # output import asyncio @@ -167,3 +170,7 @@ async def main(): async def main(): await (await (await (await (await asyncio.sleep(1))))) + + +async def main(): + await (yield) -- 2.39.5