Get traceback class

76 Views Asked by At

I want to apply type hints to a class' __exit__ method.

class Foo:

    def __enter__(self):
        return self

    def __exit__(self, typ, value, traceback):
        print(typ, value, traceback)
        print(type(typ), type(value), type(traceback))
        print(type(traceback).__module__)


def main():

    with Foo():
        raise ValueError('Oops.')


if __name__ == '__main__':
    main()

While the parameter type is of type type, and value is of type BaseException, traceback appears to be a built-in class traceback. How can I import this class traceback so that I can write:

class Foo:

    def __enter__(self):
        return self

    def __exit__(self, typ: type, value: BaseException, traceback: traceback):
        print(typ, value, traceback)
        print(type(typ), type(value), type(traceback))
        print(type(traceback).__module__)

?

0

There are 0 best solutions below