Context manager __exit__
return type bool | None
is treated as None for reachability analysis
#15158
Labels
Bug Report
The
__exit__
method of a context manager indicates whether an exception should be propagated to the caller by either returningFalse
orNone
if the exception should be propagated, or by returningTrue
when the exception shouldn't be propagated. Mypy handles these cases w.r.t. reachability analysis as follows: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.bool
, the context manager may suppress exceptions. Hence, statements after thewith
block are considered reachable, independent of what happens in thewith
block.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 thebool
case, as it is possible for the context manager to suppress exceptions and be fine in terms of type checking.To Reproduce
https://mypy-play.net/?mypy=master&python=3.11&flags=warn-unreachable&gist=579fa731e01cde87fc6ef9062de45d6c
Your Environment
--warn-unreachable
The text was updated successfully, but these errors were encountered: