-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove redundant parentheses around awaited coroutines/tasks (#2991)
This is a tricky one as await is technically an expression and therefore in certain situations requires brackets for operator precedence. However, the vast majority of await usage is just await some_coroutine(...) and similar in format to return statements. Therefore this PR removes redundant parens around these await expressions. Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Richard Si <[email protected]>
- Loading branch information
1 parent
98fccce
commit 75f99bd
Showing
4 changed files
with
210 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
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 ( | ||
((((((((((((( | ||
((( ((( | ||
((( ((( | ||
((( ((( | ||
((( ((( | ||
((black(1))) | ||
))) ))) | ||
))) ))) | ||
))) ))) | ||
))) ))) | ||
))))))))))))) | ||
) | ||
|
||
# Keep brackets around non power operations and nested awaits | ||
async def main(): | ||
await (set_of_tasks | other_set) | ||
|
||
async def main(): | ||
await (await asyncio.sleep(1)) | ||
|
||
# It's awaits all the way down... | ||
async def main(): | ||
await (await x) | ||
|
||
async def main(): | ||
await (yield x) | ||
|
||
async def main(): | ||
await (await (asyncio.sleep(1))) | ||
|
||
async def main(): | ||
await (await (await (await (await (asyncio.sleep(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) | ||
|
||
|
||
# Keep brackets around non power operations and nested awaits | ||
async def main(): | ||
await (set_of_tasks | other_set) | ||
|
||
|
||
async def main(): | ||
await (await asyncio.sleep(1)) | ||
|
||
|
||
# It's awaits all the way down... | ||
async def main(): | ||
await (await x) | ||
|
||
|
||
async def main(): | ||
await (yield x) | ||
|
||
|
||
async def main(): | ||
await (await asyncio.sleep(1)) | ||
|
||
|
||
async def main(): | ||
await (await (await (await (await asyncio.sleep(1))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters