summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2020-12-28 10:27:35 +0100
committerClaudiu Popa <pcmanticore@gmail.com>2020-12-28 10:49:27 +0100
commitf24d765d9fb68309d274c0bd1637da5387cb1f80 (patch)
tree2d40d1f6ed6db6a16c0dacf006bde49662491c44
parent3a065a16f0826f3d46025209b11d30a1fd5b3b49 (diff)
downloadpylint-git-f24d765d9fb68309d274c0bd1637da5387cb1f80.tar.gz
Fix false positive for `not-async-context-manager` when `contextlib.asynccontextmanager` is used
Close #3862
-rw-r--r--ChangeLog4
-rw-r--r--pylint/checkers/async.py8
-rw-r--r--tests/functional/n/not_async_context_manager_py37.py11
3 files changed, 21 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index f47d68b0c..e9bbc5230 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 1b581c0f1..420c0e211 100644
--- a/pylint/checkers/async.py
+++ b/pylint/checkers/async.py
@@ -58,7 +58,12 @@ class AsyncChecker(checkers.BaseChecker):
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 @@ class AsyncChecker(checkers.BaseChecker):
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 705e5afc9..c1ca26976 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