Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog

`CalVer, YY.month.patch <https://calver.org/>`_

24.9.2
======
- Fix false alarm in :ref:`ASYNC113 <async113>` and :ref:`ASYNC121 <async121>` with sync functions nested inside an async function.


24.9.1
======
- Add :ref:`ASYNC121 <async121>` control-flow-in-taskgroup
Expand Down
2 changes: 1 addition & 1 deletion flake8_async/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


# CalVer: YY.month.patch, e.g. first release of July 2022 == "22.7.1"
__version__ = "24.9.1"
__version__ = "24.9.2"


# taken from https://github.com/Zac-HD/shed
Expand Down
9 changes: 8 additions & 1 deletion flake8_async/visitors/visitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef):
node, "asynccontextmanager"
)

def visit_FunctionDef(self, node: ast.FunctionDef):
self.save_state(node, "aenter")
# sync function should never be named __aenter__ or have @asynccontextmanager
self.aenter = False

def visit_Yield(self, node: ast.Yield):
self.aenter = False

Expand Down Expand Up @@ -398,10 +403,12 @@ def visit_Return(self, node: ast.Return) -> None:
if unsafe_cm in self.unsafe_stack:
self.error(node, "return", unsafe_cm)

def visit_FunctionDef(self, node: ast.FunctionDef):
def visit_FunctionDef(self, node: ast.FunctionDef | ast.AsyncFunctionDef):
self.save_state(node, "unsafe_stack", copy=True)
self.unsafe_stack = []

visit_AsyncFunctionDef = visit_FunctionDef


@error_class_cst
class Visitor300(Flake8AsyncVisitor_cst):
Expand Down
10 changes: 10 additions & 0 deletions tests/eval_files/async113.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,13 @@ async def __aenter__(self):
async def __aexit__(self, *args):
assert self.moo is not None
await self.nursery_manager.__aexit__(*args)


@asynccontextmanager
async def foo_nested_sync_def():
with trio.open_nursery() as bar:

def non_async_func():
bar.start_soon(trio.run_process)

yield
3 changes: 3 additions & 0 deletions tests/eval_files/async121.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ async def foo_return_nested():
def bar():
return # safe

async def bar():
return # safe


async def foo_while_safe():
async with trio.open_nursery():
Expand Down
Loading