Skip to content

Commit f24d765

Browse files
committed
Fix false positive for not-async-context-manager when contextlib.asynccontextmanager is used
Close #3862
1 parent 3a065a1 commit f24d765

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ What's New in Pylint 2.6.1?
2424
===========================
2525
Release date: TBA
2626

27+
* Fix false positive for `not-async-context-manager` when `contextlib.asynccontextmanager` is used
28+
29+
Close #3862
30+
2731
* Fix linter multiprocessing pool shutdown (triggered warnings when runned in parallels with other pytest plugins)
2832

2933
Closes #3779

pylint/checkers/async.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ def visit_asyncwith(self, node):
5858
if inferred is None or inferred is astroid.Uninferable:
5959
continue
6060

61-
if isinstance(inferred, bases.AsyncGenerator):
61+
if isinstance(inferred, astroid.AsyncFunctionDef):
62+
# Check if we are dealing with a function decorated
63+
# with contextlib.asynccontextmanager.
64+
if decorated_with(inferred, self._async_generators):
65+
continue
66+
elif isinstance(inferred, bases.AsyncGenerator):
6267
# Check if we are dealing with a function decorated
6368
# with contextlib.asynccontextmanager.
6469
if decorated_with(inferred.parent, self._async_generators):
@@ -79,7 +84,6 @@ def visit_asyncwith(self, node):
7984
continue
8085
else:
8186
continue
82-
8387
self.add_message(
8488
"not-async-context-manager", node=node, args=(inferred.name,)
8589
)

tests/functional/n/not_async_context_manager_py37.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,14 @@ async def context_manager(value):
1010

1111
async with context_manager(42) as ans:
1212
assert ans == 42
13+
14+
15+
def async_context_manager():
16+
@asynccontextmanager
17+
async def wrapper():
18+
pass
19+
return wrapper
20+
21+
async def func():
22+
async with async_context_manager():
23+
pass

0 commit comments

Comments
 (0)