Specify how type checkers should interpret __exit__
annotations
#1535
Labels
topic: documentation
Documentation-related issues and PRs
topic: typing spec
For improving the typing spec
The typing spec should provide guidance on how type checkers should handle
__exit__
annotations. This method is special because__exit__
methods can suppress exceptions, and whether or not they do so affects the control flow through a function.In code like this:
What is the type of
a
? It depends on whethersome_context()
can suppress exceptions or not. Most context managers don't, but in theory they can. A pedantically correct decision might be to assume that they can always suppress exceptions (soa
might still beNone
in the sample), but that will often lead to a bad user experience.Mypy currently follows a heuristic I proposed a few years ago. Should we formalize that heuristic into the spec, or something else?
This is important to specify because it affects how library authors should annotate their
__exit__
methods. I'll probably pursue getting this into the spec in the next few months.The text was updated successfully, but these errors were encountered: