You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Make warn-unreachable understand exception-swallowing contextmanagers (#7317)
This pull request fixes#7214:
it makes mypy treat any context managers where the `__exit__` returns
`bool` or `Literal[True]` as ones that can potentially swallow
exceptions.
Context managers that return `Optional[bool]`, None, or `Literal[False]`
continue to be treated as non-exception-swallowing ones.
This distinction helps the `--warn-unreachable` flag do the right thing
in this example program:
```python
from contextlib import suppress
def should_warn() -> str:
with contextlib.suppress(IndexError):
return ["a", "b", "c"][0]
def should_not_warn() -> str:
with open("foo.txt") as f:
return "blah"
```
This behavior is partially disabled when strict-optional is disabled: we can't
necessarily distinguish between `Optional[bool]` vs `bool` in that mode, so
we conservatively treat the latter in the same way we treat the former.
0 commit comments