Skip to content

Commit

Permalink
Fix false positive for not-async-context-manager when `contextlib.a…
Browse files Browse the repository at this point in the history
…synccontextmanager` is used

Close #3862
  • Loading branch information
PCManticore committed Dec 28, 2020
1 parent 3a065a1 commit f24d765
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pylint/checkers/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -79,7 +84,6 @@ def visit_asyncwith(self, node):
continue
else:
continue

self.add_message(
"not-async-context-manager", node=node, args=(inferred.name,)
)
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/n/not_async_context_manager_py37.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f24d765

Please sign in to comment.