exc_type
is the exception’s class. exc_val
is the exception instance. exc_tb
is a traceback object, of which there is a reference in types.TracebackType
.
In general it should be the case that
type(exc_val) is exc_type
exc_val.__traceback__ is exc_tb
Note that __exit__
is still invoked when there was no exception raised by the code under a context manager, and the args will be (None, None, None)
so all three arguments should be annotated optional.
Then a correct annotation for it should look something like this:
def __exit__(self, exctype: Optional[Type[BaseException]],
excinst: Optional[BaseException],
exctb: Optional[TracebackType]) -> bool: ...
You might wonder why this API has three arguments when two of them can be trivially determined from the exception instance itself. But it wasn’t always that way, in older versions of Python you could raise strings as exceptions, and the exception’s __traceback__
attribute wasn’t there until Python 2.5. And you can still raise old-style classes as exceptions in Python 2.7 (!)