8000 ASYNC113 & ASYNC121 false alarm with nested sync functions (#287) · python-trio/flake8-async@cee691f · GitHub
[go: up one dir, main page]

Skip to content

Commit cee691f

Browse files
ASYNC113 & ASYNC121 false alarm with nested sync functions (#287)
* 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>
1 parent 50bae03 commit cee691f

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Changelog
44

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

7+
24.9.2
8+
======
9+
- Fix false alarm in :ref:`ASYNC113 <async113>` and :ref:`ASYNC121 <async121>` with sync functions nested inside an async function.
10+
11+
712
24.9.1
813
======
914
- Add :ref:`ASYNC121 <async121>` control-flow-in-taskgroup

flake8_async/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939

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

4343

4444
# taken from https://github.com/Zac-HD/shed

flake8_async/visitors/visitors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef):
188188
node, "asynccontextmanager"
189189
)
190190

191+
def visit_FunctionDef(self, node: ast.FunctionDef):
192+
self.save_state(node, "aenter")
193+
# sync function should never be named __aenter__ or have @asynccontextmanager
194+
self.aenter = False
195+
191196
def visit_Yield(self, node: ast.Yield):
192197
self.aenter = False
193198

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

401-
def visit_FunctionDef(self, node: ast.FunctionDef):
406+
def visit_FunctionDef(self, node: ast.FunctionDef | ast.AsyncFunctionDef):
402407
self.save_state(node, "unsafe_stack", copy=True)
403408
self.unsafe_stack = []
404409

410+
visit_AsyncFunctionDef = visit_FunctionDef
411+
405412

406413
@error_class_cst
407414
class Visitor300(Flake8AsyncVisitor_cst):

tests/eval_files/async113.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,13 @@ async def __aenter__(self):
105105
async def __aexit__(self, *args):
106106
assert self.moo is not None
107107
await self.nursery_manager.__aexit__(*args)
108+
109+
110+
@asynccontextmanager
111+
async def foo_nested_sync_def():
112+
with trio.open_nursery() as bar:
113+
114+
def non_async_func():
115+
bar.start_soon(trio.run_process)
116+
117+
yield

tests/eval_files/async121.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ async def foo_return_nested():
3030
def bar():
3131
return # safe
3232

33+
async def bar():
34+
return # safe
35+
3336

3437
async def foo_while_safe():
3538
async with trio.open_nursery():

0 commit comments

Comments
 (0)
0