Skip to content

Commit 226cbf0

Browse files
Fix unsafe cast in linegen.py w/ await yield handling (#3533)
Fixes #3532.
1 parent f4ebc68 commit 226cbf0

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
- Fix an invalid quote escaping bug in f-string expressions where it produced invalid
4343
code. Implicitly concatenated f-strings with different quotes can now be merged or
4444
quote-normalized by changing the quotes used in expressions. (#3509)
45+
- Fix crash on `await (yield)` when Black is compiled with mypyc (#3533)
4546

4647
### Configuration
4748

src/black/linegen.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,18 +1223,19 @@ def remove_await_parens(node: Node) -> None:
12231223
# N.B. We've still removed any redundant nested brackets though :)
12241224
opening_bracket = cast(Leaf, node.children[1].children[0])
12251225
closing_bracket = cast(Leaf, node.children[1].children[-1])
1226-
bracket_contents = cast(Node, node.children[1].children[1])
1227-
if bracket_contents.type != syms.power:
1228-
ensure_visible(opening_bracket)
1229-
ensure_visible(closing_bracket)
1230-
elif (
1231-
bracket_contents.type == syms.power
1232-
and bracket_contents.children[0].type == token.AWAIT
1233-
):
1234-
ensure_visible(opening_bracket)
1235-
ensure_visible(closing_bracket)
1236-
# If we are in a nested await then recurse down.
1237-
remove_await_parens(bracket_contents)
1226+
bracket_contents = node.children[1].children[1]
1227+
if isinstance(bracket_contents, Node):
1228+
if bracket_contents.type != syms.power:
1229+
ensure_visible(opening_bracket)
1230+
ensure_visible(closing_bracket)
1231+
elif (
1232+
bracket_contents.type == syms.power
1233+
and bracket_contents.children[0].type == token.AWAIT
1234+
):
1235+
ensure_visible(opening_bracket)
1236+
ensure_visible(closing_bracket)
1237+
# If we are in a nested await then recurse down.
1238+
remove_await_parens(bracket_contents)
12381239

12391240

12401241
def _maybe_wrap_cms_in_parens(

tests/data/preview/remove_await_parens.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ async def main():
7777
async def main():
7878
await (await (await (await (await (asyncio.sleep(1))))))
7979

80+
async def main():
81+
await (yield)
82+
8083
# output
8184
import asyncio
8285

@@ -167,3 +170,7 @@ async def main():
167170

168171
async def main():
169172
await (await (await (await (await asyncio.sleep(1)))))
173+
174+
175+
async def main():
176+
await (yield)

0 commit comments

Comments
 (0)