Open
Description
Bug Report
The __exit__
method of a context manager indicates whether an exception should be propagated to the caller by either returning False
or None
if the exception should be propagated, or by returning True
when the exception shouldn't be propagated. Mypy handles these cases w.r.t. reachability analysis as follows:
- If the return type is
None
, then the context manager cannot suppress exceptions, hence if thewith
block unconditionally raises an exception or calls aNoReturn
function, statements following after thewith
block are unreachable. - If the return type is
bool
, the context manager may suppress exceptions. Hence, statements after thewith
block are considered reachable, independent of what happens in thewith
block. - If the return type is
bool | None
, mypy treats this currently as the case where the return type isNone
.
I believe that if the return type is bool | None
, mypy should consider this similar to the bool
case, as it is possible for the context manager to suppress exceptions and be fine in terms of type checking.
To Reproduce
Your Environment
- Mypy version used: 1.2.0
- Mypy command-line flags:
--warn-unreachable
- Python version used: 3.11