Repository navigation
Commit 052d997
Report one bad-context-manager error per broken with-item
Summary:
Fixes #5079
## Problem
A context manager that fails **both** protocol halves produced two `bad-context-manager` errors with an identical header and range:
```python
from contextlib import asynccontextmanager
asynccontextmanager
async def async_context():
yield
with async_context(): # TWO identical errors appear here
print("This will cause an error at runtime")
```
Both errors read "Cannot use `_AsyncGeneratorContextManager[None]` as a context manager"; they differ only in the detail line (`__enter__` vs `__exit__` missing). The error collector intentionally does not merge diagnostics whose details differ, so the pair always displayed.
## Change
`context_value` checks the protocol in two halves (enter, exit), each attributing failures to the with-item's range under the shared `bad-context-manager` context. Each half's diagnostics are now collected in their own delayed collector, and only the **first failing half's** errors are forwarded:
- enter broken → the enter error survives (it carries the actionable "Did you mean `__aenter__`?" hint, which is exactly the fix a user needs for the issue's case);
- enter fine → exit errors forwarded byte-for-byte as before, including the existing two-call consolidation for bad `__exit__` signatures;
- the exit half is still fully evaluated so its return type keeps feeding the `bool | None` check.
An object failing only one half still yields exactly one error naming that half; `async with` on async context managers is unaffected.
X-link: #5095
Reviewed By: rchen152
Differential Revision: D123266669
fbshipit-source-id: 036daaf3c0383dbab4f28f76fe7b5997a0782d0d1 parent 8c07754 commit 052d997
3 files changed
Lines changed: 43 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2149 | 2149 | | |
2150 | 2150 | | |
2151 | 2151 | | |
2152 | | - | |
2153 | | - | |
2154 | | - | |
2155 | | - | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
| 2164 | + | |
| 2165 | + | |
| 2166 | + | |
| 2167 | + | |
| 2168 | + | |
| 2169 | + | |
| 2170 | + | |
| 2171 | + | |
| 2172 | + | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
| 2176 | + | |
2156 | 2177 | | |
2157 | 2178 | | |
2158 | 2179 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
928 | 928 | | |
929 | 929 | | |
930 | 930 | | |
931 | | - | |
932 | | - | |
933 | | - | |
934 | | - | |
935 | | - | |
936 | | - | |
937 | | - | |
938 | | - | |
939 | | - | |
940 | | - | |
941 | | - | |
942 | | - | |
943 | | - | |
944 | 931 | | |
945 | 932 | | |
946 | 933 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
571 | 571 | | |
572 | 572 | | |
573 | 573 | | |
574 | | - | |
| 574 | + | |
575 | 575 | | |
576 | 576 | | |
577 | 577 | | |
578 | | - | |
| 578 | + | |
579 | 579 | | |
580 | 580 | | |
581 | 581 | | |
582 | 582 | | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
583 | 599 | | |
584 | 600 | | |
585 | 601 | | |
| |||
0 commit comments