-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
eb5f4a4
commit f15d677
Showing
5 changed files
with
414 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.