- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 3k
 
Open
Labels
bugmypy got something wrongmypy got something wrongtopic-reachabilityDetecting unreachable codeDetecting unreachable code
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 thewithblock unconditionally raises an exception or calls aNoReturnfunction, statements following after thewithblock are unreachable. - If the return type is 
bool, the context manager may suppress exceptions. Hence, statements after thewithblock are considered reachable, independent of what happens in thewithblock. - 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
 
DetachHead
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrongtopic-reachabilityDetecting unreachable codeDetecting unreachable code