New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[traceback] Add traceback frame opt-out via a _rich_traceback_omit = True
machinery
#2226
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import io | ||
import re | ||
import sys | ||
from typing import List | ||
|
||
import pytest | ||
|
||
|
@@ -307,6 +308,41 @@ def test_suppress(): | |
assert "foo" in traceback.suppress[1] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"rich_traceback_omit_for_level2,expected_frames_length,expected_frame_names", | ||
( | ||
# fmt: off | ||
[True, 3, ["test_rich_traceback_omit_optional_local_flag", "level1", "level3"]], | ||
[False, 4, ["test_rich_traceback_omit_optional_local_flag", "level1", "level2", "level3"]], | ||
# fmt: on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this. Black completely ruins the readability of some parametrised tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine by me! |
||
), | ||
) | ||
def test_rich_traceback_omit_optional_local_flag( | ||
rich_traceback_omit_for_level2: bool, | ||
expected_frames_length: int, | ||
expected_frame_names: List[str], | ||
): | ||
def level1(): | ||
return level2() | ||
|
||
def level2(): | ||
_rich_traceback_omit = rich_traceback_omit_for_level2 | ||
return level3() | ||
|
||
def level3(): | ||
return 1 / 0 | ||
|
||
try: | ||
level1() | ||
except Exception: | ||
exc_type, exc_value, traceback = sys.exc_info() | ||
trace = Traceback.from_exception(exc_type, exc_value, traceback).trace | ||
frames = trace.stacks[0].frames | ||
assert len(frames) == expected_frames_length | ||
frame_names = [f.name for f in frames] | ||
assert frame_names == expected_frame_names | ||
|
||
|
||
if __name__ == "__main__": # pragma: no cover | ||
|
||
expected = render(get_exception()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_rich_traceback_guard
mechanism is triggered by just the presence of the local (i.e. doesn't care about the value). I think this should be consistent.On reflection I think we should look at the value to make it easier to add a condition. The default should be
False
if the local isn't defined.Something like this:
The
"_rich_traceback_guard"
should also be updated in the same way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see! However,
f_locals
being a Python dictionary it doesn't support thegetattr
protocol if I'm not wrong?But I updated the
_rich_traceback_guard
management so that both look the same. Also, they are both activated as soon as the value is defined in the locals and its value is true-ish value, rather than strictTrue
🙂--> done there: f0ca6ff
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, should of course be
.get
.