Skip to content

Conversation

emmanuel-ferdman
Copy link
Contributor

Type of Changes

Type
✨ New feature

Description

This PR adds a new pylint check async-context-manager-with-regular-with (E1145) that provides clearer error message when async context managers are incorrectly used with regular with statements instead of async with. Previously, users would see the generic error:

"Context manager 'async_generator' doesn't implement enter and exit"` (E1129)

which was confusing and didn't clearly indicate the solution. The new check specifically detects when an AsyncGenerator decorated with @asynccontextmanager is used with with and provides an actionable error message:

Context manager 'function_name' is async and should be used with 'async with' (E1145)

Example:

import time
from contextlib import asynccontextmanager


@asynccontextmanager
async def timeit():
    now = time.monotonic()
    try:
        yield
    finally:
        print(f'it took {time.monotonic() - now}s to run')


with timeit():
    print('hello world')

will return:

test.py:17:0: E1145: Context manager 'timeit' is async and should be used with 'async with'. (async-context-manager-with-regular-with)

Closes #10408

@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Sep 21, 2025
Copy link

codecov bot commented Sep 21, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.91%. Comparing base (47861c3) to head (500c0e8).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10575   +/-   ##
=======================================
  Coverage   95.91%   95.91%           
=======================================
  Files         176      176           
  Lines       19458    19463    +5     
=======================================
+ Hits        18663    18668    +5     
  Misses        795      795           
Files with missing lines Coverage Δ
pylint/checkers/typecheck.py 96.30% <100.00%> (+0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great work !

@Pierre-Sassoulas Pierre-Sassoulas added this to the 4.0.0 milestone Sep 21, 2025
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see what the primer tells us and wait for another maintainer opinion before merging :)

Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 500c0e8

@Pierre-Sassoulas Pierre-Sassoulas added the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Sep 21, 2025
@jacobtylerwalls jacobtylerwalls removed the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Sep 21, 2025
@Pierre-Sassoulas Pierre-Sassoulas merged commit 01f022d into pylint-dev:main Sep 21, 2025
47 checks passed
@cdce8p cdce8p mentioned this pull request Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement ✨ Improvement to a component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

False Context manager 'async_generator' doesn't implement __enter__ and __exit__

3 participants