From 6fa304148d6186de7c9bbf3cdcec08a3b4ad1605 Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Mon, 9 Sep 2024 14:57:08 +0200
Subject: [PATCH 1/5] fix async121 false alarm in asyncfunctiondef
---
flake8_async/visitors/visitors.py | 4 +++-
tests/eval_files/async121.py | 2 ++
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/flake8_async/visitors/visitors.py b/flake8_async/visitors/visitors.py
index 6d9ca23f..ea2e1639 100644
--- a/flake8_async/visitors/visitors.py
+++ b/flake8_async/visitors/visitors.py
@@ -398,10 +398,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):
diff --git a/tests/eval_files/async121.py b/tests/eval_files/async121.py
index 78a6b6c4..0c294a91 100644
--- a/tests/eval_files/async121.py
+++ b/tests/eval_files/async121.py
@@ -29,6 +29,8 @@ async def foo_return_nested():
def bar():
return # safe
+ async def bar():
+ return # safe
async def foo_while_safe():
From 0241dfac9ad4fd19cab661f4ba3b27baa1ef3198 Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Mon, 9 Sep 2024 15:18:14 +0200
Subject: [PATCH 2/5] fix async113 as well
---
flake8_async/visitors/visitors.py | 5 +++++
tests/eval_files/async113.py | 8 ++++++++
2 files changed, 13 insertions(+)
diff --git a/flake8_async/visitors/visitors.py b/flake8_async/visitors/visitors.py
index ea2e1639..ec7ee1d5 100644
--- a/flake8_async/visitors/visitors.py
+++ b/flake8_async/visitors/visitors.py
@@ -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
diff --git a/tests/eval_files/async113.py b/tests/eval_files/async113.py
index ebddc5be..ba514d2d 100644
--- a/tests/eval_files/async113.py
+++ b/tests/eval_files/async113.py
@@ -105,3 +105,11 @@ 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():
+ with trio.open_nursery() as bar:
+ def non_async_func():
+ bar.start_soon(trio.run_process)
+
+ yield
From ace8a7d19e1e755f19e964f8ea0e227ea144ee43 Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Mon, 9 Sep 2024 15:21:35 +0200
Subject: [PATCH 3/5] add changelog entry, bump version
---
docs/changelog.rst | 5 +++++
flake8_async/__init__.py | 2 +-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 87b7b726..f306eea6 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -4,6 +4,11 @@ Changelog
`CalVer, YY.month.patch `_
+24.9.2
+======
+- Fix false alarm in :ref:`ASYNC113 ` and :ref:`ASYNC121 ` with sync functions nested inside an async function.
+
+
24.9.1
======
- Add :ref:`ASYNC121 ` control-flow-in-taskgroup
diff --git a/flake8_async/__init__.py b/flake8_async/__init__.py
index d4827f86..6ac873fc 100644
--- a/flake8_async/__init__.py
+++ b/flake8_async/__init__.py
@@ -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
From 137611c61d3cd55ba89050b3920796af7f78681e Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Mon, 9 Sep 2024 15:42:53 +0200
Subject: [PATCH 4/5] fix pre-commit mypy error in test file
---
tests/eval_files/async113.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/tests/eval_files/async113.py b/tests/eval_files/async113.py
index ba514d2d..22fedc28 100644
--- a/tests/eval_files/async113.py
+++ b/tests/eval_files/async113.py
@@ -106,9 +106,11 @@ async def __aexit__(self, *args):
assert self.moo is not None
await self.nursery_manager.__aexit__(*args)
+
@asynccontextmanager
-async def foo():
+async def foo_nested_sync_def():
with trio.open_nursery() as bar:
+
def non_async_func():
bar.start_soon(trio.run_process)
From 32df606dad15367eadc256ce0c55d0b232a2c908 Mon Sep 17 00:00:00 2001
From: "pre-commit-ci[bot]"
<66853113+pre-commit-ci[bot]@users.noreply.github.com>
Date: Mon, 9 Sep 2024 13:43:57 +0000
Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---
tests/eval_files/async121.py | 1 +
1 file changed, 1 insertion(+)
diff --git a/tests/eval_files/async121.py b/tests/eval_files/async121.py
index 0c294a91..b55f21b9 100644
--- a/tests/eval_files/async121.py
+++ b/tests/eval_files/async121.py
@@ -29,6 +29,7 @@ async def foo_return_nested():
def bar():
return # safe
+
async def bar():
return # safe