From f24d765d9fb68309d274c0bd1637da5387cb1f80 Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Mon, 28 Dec 2020 10:27:35 +0100 Subject: [PATCH] Fix false positive for `not-async-context-manager` when `contextlib.asynccontextmanager` is used Close #3862 --- ChangeLog | 4 ++++ pylint/checkers/async.py | 8 ++++++-- tests/functional/n/not_async_context_manager_py37.py | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index f47d68b0c2..e9bbc52306 100644 --- a/ChangeLog +++ b/ChangeLog @@ -24,6 +24,10 @@ What's New in Pylint 2.6.1? =========================== Release date: TBA +* Fix false positive for `not-async-context-manager` when `contextlib.asynccontextmanager` is used + + Close #3862 + * Fix linter multiprocessing pool shutdown (triggered warnings when runned in parallels with other pytest plugins) Closes #3779 diff --git a/pylint/checkers/async.py b/pylint/checkers/async.py index 1b581c0f14..420c0e2118 100644 --- a/pylint/checkers/async.py +++ b/pylint/checkers/async.py @@ -58,7 +58,12 @@ def visit_asyncwith(self, node): if inferred is None or inferred is astroid.Uninferable: continue - if isinstance(inferred, bases.AsyncGenerator): + if isinstance(inferred, astroid.AsyncFunctionDef): + # Check if we are dealing with a function decorated + # with contextlib.asynccontextmanager. + if decorated_with(inferred, self._async_generators): + continue + elif isinstance(inferred, bases.AsyncGenerator): # Check if we are dealing with a function decorated # with contextlib.asynccontextmanager. if decorated_with(inferred.parent, self._async_generators): @@ -79,7 +84,6 @@ def visit_asyncwith(self, node): continue else: continue - self.add_message( "not-async-context-manager", node=node, args=(inferred.name,) ) diff --git a/tests/functional/n/not_async_context_manager_py37.py b/tests/functional/n/not_async_context_manager_py37.py index 705e5afc9b..c1ca26976d 100644 --- a/tests/functional/n/not_async_context_manager_py37.py +++ b/tests/functional/n/not_async_context_manager_py37.py @@ -10,3 +10,14 @@ async def context_manager(value): async with context_manager(42) as ans: assert ans == 42 + + +def async_context_manager(): + @asynccontextmanager + async def wrapper(): + pass + return wrapper + +async def func(): + async with async_context_manager(): + pass