Skip to content

Commit

Permalink
ASYNC113 & ASYNC121 false alarm with nested sync functions (#287)
Browse files Browse the repository at this point in the history
* fix async121 false alarm in asyncfunctiondef

* fix async113 as well

* add changelog entry, bump version

* fix pre-commit mypy error in test file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
jakkdl and pre-commit-ci[bot] authored Sep 9, 2024
1 parent 50bae03 commit cee691f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
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

0 comments on commit cee691f

Please sign in to comment.