Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive for not-async-context-manager when contextlib.asynccontextmanager is used #3996

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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