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

Fix crash on await (a ** b) #3994

Merged
merged 4 commits into from Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,8 @@

<!-- Changes that affect Black's stable style -->

- Fix crash on formatting code like `await (a ** b)` (#3994)

### Preview style

- Multiline dictionaries and lists that are the sole argument to a function are now
Expand Down
20 changes: 12 additions & 8 deletions src/black/linegen.py
Expand Up @@ -1354,14 +1354,18 @@ def remove_await_parens(node: Node) -> None:
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)
elif bracket_contents.type == syms.power:
if 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)
Copy link
Collaborator

Choose a reason for hiding this comment

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

When do we need to manually make this recursive call?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seems like we don't, all the tests pass if I remove it. We'll visit nested awaits anyway because this is invoked from a visitor. It also isn't necessary for removing multiple pairs of parens; we have tests for that.

elif any(
isinstance(child, Leaf) and child.type == token.DOUBLESTAR
for child in bracket_contents.children
):
ensure_visible(opening_bracket)
ensure_visible(closing_bracket)


def _maybe_wrap_cms_in_parens(
Expand Down
19 changes: 19 additions & 0 deletions tests/data/cases/remove_await_parens.py
Expand Up @@ -80,6 +80,15 @@ async def main():
async def main():
await (yield)

async def main():
await (a ** b)
await (a[b] ** c)
await (a ** b[c])
await ((a + b) ** (c + d))
await (a + b)
await (a[b])
await (a[b ** c])

# output
import asyncio

Expand Down Expand Up @@ -174,3 +183,13 @@ async def main():

async def main():
await (yield)


async def main():
await (a**b)
await (a[b] ** c)
await (a ** b[c])
await ((a + b) ** (c + d))
await (a + b)
await a[b]
await a[b**c]