-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add async120, await-in-except (#265)
* add async120, await-in-except * Fix false alarm on nested function definitions for async102 --------- Co-authored-by: Zac Hatfield-Dodds <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
fcd515a
commit e198655
Showing
7 changed files
with
212 additions
and
13 deletions.
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
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
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,123 @@ | ||
# ARG --enable=ASYNC102,ASYNC120 | ||
# NOASYNCIO # TODO: support asyncio shields (?) | ||
|
||
import trio | ||
|
||
|
||
def condition() -> bool: | ||
return False | ||
|
||
|
||
async def foo(): | ||
try: | ||
... | ||
except ValueError: | ||
await foo() # ASYNC120: 8, Stmt("except", lineno-1) | ||
if condition(): | ||
raise | ||
await foo() | ||
|
||
try: | ||
... | ||
except ValueError: | ||
await foo() # ASYNC120: 8, Stmt("except", lineno-1) | ||
raise | ||
|
||
# don't error if the raise is in a separate excepthandler | ||
try: | ||
... | ||
except ValueError: | ||
await foo() | ||
except TypeError: | ||
raise | ||
|
||
# does not support conditional branches | ||
try: | ||
... | ||
except ValueError: | ||
if ...: | ||
await foo() # ASYNC120: 12, Stmt("except", lineno-2) | ||
else: | ||
raise | ||
|
||
# don't trigger on cases of ASYNC102 (?) | ||
try: | ||
... | ||
except: | ||
await foo() # ASYNC102: 8, Stmt("bare except", lineno-1) | ||
raise | ||
|
||
# shielded awaits with timeouts don't trigger 120 | ||
try: | ||
... | ||
except: | ||
with trio.fail_after(10) as cs: | ||
cs.shield = True | ||
await foo() | ||
raise | ||
|
||
try: | ||
... | ||
except: | ||
with trio.fail_after(10) as cs: | ||
cs.shield = True | ||
await foo() | ||
raise | ||
|
||
# ************************ | ||
# Weird nesting edge cases | ||
# ************************ | ||
|
||
# nested excepthandlers should not trigger 120 on awaits in | ||
# their parent scope | ||
try: | ||
... | ||
except ValueError: | ||
await foo() | ||
try: | ||
... | ||
except TypeError: | ||
raise | ||
|
||
# but the other way around probably should(?) | ||
try: | ||
... | ||
except ValueError: | ||
try: | ||
... | ||
except TypeError: | ||
await foo() | ||
raise | ||
|
||
# but only when they're properly nested, this should not give 120 | ||
try: | ||
... | ||
except TypeError: | ||
await foo() | ||
if condition(): | ||
raise | ||
|
||
try: | ||
... | ||
except ValueError: | ||
await foo() # ASYNC120: 8, Statement("except", lineno-1) | ||
try: | ||
await foo() # ASYNC120: 12, Statement("except", lineno-3) | ||
except BaseException: | ||
await foo() # ASYNC102: 12, Statement("BaseException", lineno-1) | ||
except: | ||
await foo() | ||
await foo() # ASYNC120: 8, Statement("except", lineno-8) | ||
raise | ||
|
||
|
||
# nested funcdef | ||
async def foo_nested_funcdef(): | ||
try: | ||
... | ||
except ValueError: | ||
|
||
async def foobar(): | ||
await foo() | ||
|
||
raise |