what's the usage of __traceback_hide__

__tracebackhide__ can be set to hide a function from the traceback when using PyTest. __traceback_hide__ appears to be used in the Python Paste package for the same purpose.

Here's what the paste.exceptions.collector documentation has to say about it:

If set and true, this indicates that the frame should be hidden from abbreviated tracebacks. This way you can hide some of the complexity of the larger framework and let the user focus on their own errors.

By setting it to 'before', all frames before this one will be thrown away. By setting it to 'after' then all frames after this will be thrown away until 'reset' is found. In each case the frame where it is set is included, unless you append '_and_this' to the value (e.g., 'before_and_this').

Note that formatters will ignore this entirely if the frame that contains the error wouldn’t normally be shown according to these rules.

And the PyTest documentation on its similar __tracebackhide__:

If you have a test helper function called from a test you can use the pytest.fail marker to fail a test with a certain message. The test support function will not show up in the traceback if you set the __tracebackhide__ option somewhere in the helper function.

So basically, they are to avoid cluttering your tracebacks with test helper functions or other functions that you know aren't part of the problem that you are trying to debug.


Looks like this is mostly a convenience for web frameworks (Sentry, werkzeug, Paste, Django) to make it so framework functions aren't included in the high level exception reporting features of the frameworks.

The exact behavior likely differs by framework, for example, for Paste specifically, it's documented as:

If set and true, this indicates that the frame should be hidden from abbreviated tracebacks. This way you can hide some of the complexity of the larger framework and let the user focus on their own errors.

By setting it to 'before', all frames before this one will be thrown away. By setting it to 'after' then all frames after this will be thrown away until 'reset' is found. In each case the frame where it is set is included, unless you append '_and_this' to the value (e.g., 'before_and_this').

Note that formatters will ignore this entirely if the frame that contains the error wouldn’t normally be shown according to these rules.

It's not a standard variable, and the core Python interpreter provides no support for it.

Tags:

Python