-
-
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 async121 control-flow-in-taskgroup (#282)
* add async121 control-flow-in-taskgroup * bump __version__ * aaand fix the version # in the changelog * reword the description after feedback from oremanj * enable rule for asyncio, add more details to rule explanation. Extend tests to be more thorough with state management. * also check AsyncFor, restructure tests
- Loading branch information
Showing
8 changed files
with
215 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
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,105 @@ | ||
# ASYNCIO_NO_ERROR # checked in async121_asyncio.py | ||
# ANYIO_NO_ERROR # checked in async121_anyio.py | ||
|
||
import trio | ||
from typing import Any | ||
|
||
|
||
# To avoid mypy unreachable-statement we wrap control flow calls in if statements | ||
# they should have zero effect on the visitor logic. | ||
def condition() -> bool: | ||
return False | ||
|
||
|
||
def bar() -> Any: ... | ||
|
||
|
||
async def foo_return(): | ||
async with trio.open_nursery(): | ||
if condition(): | ||
return # ASYNC121: 12, "return", "nursery" | ||
while condition(): | ||
return # ASYNC121: 12, "return", "nursery" | ||
|
||
return # safe | ||
|
||
|
||
async def foo_return_nested(): | ||
async with trio.open_nursery(): | ||
|
||
def bar(): | ||
return # safe | ||
|
||
|
||
async def foo_while_safe(): | ||
async with trio.open_nursery(): | ||
while True: | ||
if condition(): | ||
break # safe | ||
if condition(): | ||
continue # safe | ||
continue # safe | ||
|
||
|
||
async def foo_while_unsafe(): | ||
while True: | ||
async with trio.open_nursery(): | ||
if condition(): | ||
continue # ASYNC121: 16, "continue", "nursery" | ||
if condition(): | ||
break # ASYNC121: 16, "break", "nursery" | ||
if condition(): | ||
continue # safe | ||
break # safe | ||
|
||
|
||
async def foo_for_safe(): | ||
async with trio.open_nursery(): | ||
for _ in range(5): | ||
if condition(): | ||
continue # safe | ||
if condition(): | ||
break # safe | ||
|
||
|
||
async def foo_for_unsafe(): | ||
for _ in range(5): | ||
async with trio.open_nursery(): | ||
if condition(): | ||
continue # ASYNC121: 16, "continue", "nursery" | ||
if condition(): | ||
break # ASYNC121: 16, "break", "nursery" | ||
continue # safe | ||
|
||
|
||
async def foo_async_for_safe(): | ||
async with trio.open_nursery(): | ||
async for _ in bar(): | ||
if condition(): | ||
continue # safe | ||
if condition(): | ||
break # safe | ||
|
||
|
||
async def foo_async_for_unsafe(): | ||
async for _ in bar(): | ||
async with trio.open_nursery(): | ||
if condition(): | ||
continue # ASYNC121: 16, "continue", "nursery" | ||
if condition(): | ||
break # ASYNC121: 16, "break", "nursery" | ||
continue # safe | ||
|
||
|
||
# nested nursery | ||
async def foo_nested_nursery(): | ||
async with trio.open_nursery(): | ||
if condition(): | ||
return # ASYNC121: 12, "return", "nursery" | ||
async with trio.open_nursery(): | ||
if condition(): | ||
return # ASYNC121: 16, "return", "nursery" | ||
if condition(): | ||
return # ASYNC121: 12, "return", "nursery" | ||
if condition(): | ||
return # safe |
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,22 @@ | ||
# ASYNCIO_NO_ERROR # checked in async121_asyncio.py | ||
# TRIO_NO_ERROR # checked in async121.py | ||
# BASE_LIBRARY anyio | ||
|
||
import anyio | ||
|
||
|
||
# To avoid mypy unreachable-statement we wrap control flow calls in if statements | ||
# they should have zero effect on the visitor logic. | ||
def condition() -> bool: | ||
return False | ||
|
||
|
||
# only tests that asyncio.TaskGroup is detected, main tests in async121.py | ||
async def foo_return(): | ||
while True: | ||
async with anyio.create_task_group(): | ||
if condition(): | ||
continue # ASYNC121: 16, "continue", "task group" | ||
if condition(): | ||
break # ASYNC121: 16, "break", "task group" | ||
return # ASYNC121: 12, "return", "task group" |
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,24 @@ | ||
# ANYIO_NO_ERROR | ||
# TRIO_NO_ERROR # checked in async121.py | ||
# BASE_LIBRARY asyncio | ||
# TaskGroup was added in 3.11, we run type checking with 3.9 | ||
# mypy: disable-error-code=attr-defined | ||
|
||
import asyncio | ||
|
||
|
||
# To avoid mypy unreachable-statement we wrap control flow calls in if statements | ||
# they should have zero effect on the visitor logic. | ||
def condition() -> bool: | ||
return False | ||
|
||
|
||
# only tests that asyncio.TaskGroup is detected, main tests in async121.py | ||
async def foo_return(): | ||
while True: | ||
async with asyncio.TaskGroup(): | ||
if condition(): | ||
continue # ASYNC121: 16, "continue", "task group" | ||
if condition(): | ||
break # ASYNC121: 16, "break", "task group" | ||
return # ASYNC121: 12, "return", "task group" |
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