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

Remove redundant parentheses around awaited coroutines/tasks #2991

Merged
merged 8 commits into from Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -14,6 +14,7 @@

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

- Parentheses around awaited coroutines/tasks are now managed (#2991)
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
- Remove unnecessary parentheses from `with` statements (#2926)

### _Blackd_
Expand Down
31 changes: 31 additions & 0 deletions src/black/linegen.py
Expand Up @@ -226,6 +226,37 @@ def visit_power(self, node: Node) -> Iterator[Line]:
):
wrap_in_parentheses(node, leaf)

if (
Preview.remove_redundant_parens in self.mode
and node.children[0].type == token.AWAIT
and len(node.children) > 1
):
if (
node.children[1].type == syms.atom
and node.children[1].children[0].type == token.LPAR
):
if maybe_make_parens_invisible_in_atom(
node.children[1],
parent=node,
remove_brackets_around_comma=True,
):
wrap_in_parentheses(node, node.children[1], visible=False)
if (
node.children[1].children[1].type != syms.power
and isinstance(node.children[1].children[0], Leaf)
and isinstance(node.children[1].children[-1], Leaf)
):
# Since await is an expression
# it is syntactically possible
# that someone would write `await (1 % 2)`
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
# (although realistically unlikely given that it's a runtime error).
# If we don't encounter a power being wrapped then we
# should not remove all brackets due to operator precedence.
# https://peps.python.org/pep-0492/#updated-operator-precedence-table
# N.B. We've still removed any redundant nested brackets though :)
ensure_visible(node.children[1].children[0])
ensure_visible(node.children[1].children[-1])

yield from self.visit_default(node)

def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
Expand Down
122 changes: 122 additions & 0 deletions tests/data/remove_await_parens.py
@@ -0,0 +1,122 @@
import asyncio

# Control example
async def main():
await asyncio.sleep(1)

# Remove brackets for short coroutine/task
async def main():
await (asyncio.sleep(1))

async def main():
await (
asyncio.sleep(1)
)

async def main():
await (asyncio.sleep(1)
)

# Check comments
async def main():
await ( # Hello
asyncio.sleep(1)
)

async def main():
await (
asyncio.sleep(1) # Hello
)

async def main():
await (
asyncio.sleep(1)
) # Hello

# Long lines
async def main():
await asyncio.gather(asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1))

# Same as above but with magic trailing comma in function
async def main():
await asyncio.gather(asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1), asyncio.sleep(1),)

# Cr@zY Br@ck3Tz
async def main():
await (
(((((((((((((
jpy-git marked this conversation as resolved.
Show resolved Hide resolved
((( (((
((( (((
((( (((
((( (((
((black(1)))
))) )))
))) )))
))) )))
))) )))
)))))))))))))
)

# output
import asyncio

# Control example
async def main():
await asyncio.sleep(1)


# Remove brackets for short coroutine/task
async def main():
await asyncio.sleep(1)


async def main():
await asyncio.sleep(1)


async def main():
await asyncio.sleep(1)


# Check comments
async def main():
await asyncio.sleep(1) # Hello


async def main():
await asyncio.sleep(1) # Hello


async def main():
await asyncio.sleep(1) # Hello


# Long lines
async def main():
await asyncio.gather(
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
)


# Same as above but with magic trailing comma in function
async def main():
await asyncio.gather(
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
asyncio.sleep(1),
)


# Cr@zY Br@ck3Tz
async def main():
await black(1)
1 change: 1 addition & 0 deletions tests/test_format.py
Expand Up @@ -83,6 +83,7 @@
"remove_except_parens",
"remove_for_brackets",
"one_element_subscript",
"remove_await_parens",
]

SOURCES: List[str] = [
Expand Down