Skip to content

Commit 9c259c0

Browse files
authoredJul 18, 2021
Hide traceback frames with __tracebackhide__ = True. (#129)
1 parent f317cfe commit 9c259c0

File tree

5 files changed

+40
-6
lines changed

5 files changed

+40
-6
lines changed
 

‎.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ repos:
3232
- id: rst-inline-touching-normal
3333
- id: text-unicode-replacement-char
3434
- repo: https://github.com/asottile/pyupgrade
35-
rev: v2.21.0
35+
rev: v2.21.2
3636
hooks:
3737
- id: pyupgrade
3838
args: [--py36-plus]
@@ -45,7 +45,7 @@ repos:
4545
hooks:
4646
- id: setup-cfg-fmt
4747
- repo: https://github.com/psf/black
48-
rev: 21.6b0
48+
rev: 21.7b0
4949
hooks:
5050
- id: black
5151
- repo: https://github.com/asottile/blacken-docs

‎docs/source/changes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
2323
- :gh:`121` add skipped and persisted tasks to the execution footer.
2424
- :gh:`127` make the table during execution the default. Silence pytask with negative
2525
verbose mode integers and increase verbosity with positive ones.
26+
- :gh:`129` allows to hide frames from the traceback by using ``__tracebackhide__ =
27+
True``.
2628

2729

2830
0.0.16 - 2021-06-25

‎src/_pytask/traceback.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ def remove_internal_traceback_frames_from_exc_info(exc_info):
2727
return exc_info
2828

2929

30-
def _is_internal_traceback_frame(frame):
31-
"""Returns ``True`` if traceback frame belongs to internal packages.
30+
def _is_internal_or_hidden_traceback_frame(frame):
31+
"""Returns ``True`` if traceback frame belongs to internal packages or is hidden.
3232
33-
Internal packages are ``_pytask`` and ``pluggy``.
33+
Internal packages are ``_pytask`` and ``pluggy``. A hidden frame is indicated by a
34+
local variable called ``__tracebackhide__ = True``.
3435
3536
"""
37+
is_hidden = frame.tb_frame.f_locals.get("__tracebackhide__", False)
38+
if is_hidden:
39+
return True
40+
3641
path = Path(frame.tb_frame.f_code.co_filename)
3742
return any(root in path.parents for root in [_PLUGGY_DIRECTORY, _PYTASK_DIRECTORY])
3843

@@ -44,7 +49,7 @@ def _filter_internal_traceback_frames(frame):
4449
4550
"""
4651
for frame in _yield_traceback_frames(frame):
47-
if frame is None or not _is_internal_traceback_frame(frame):
52+
if frame is None or not _is_internal_or_hidden_traceback_frame(frame):
4853
break
4954
return frame
5055

‎tests/test_traceback.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import textwrap
2+
3+
import pytest
4+
from pytask import cli
5+
6+
7+
@pytest.mark.parametrize("is_hidden", [True, False])
8+
def test_hide_traceback_from_error_report(runner, tmp_path, is_hidden):
9+
source = f"""
10+
def task_main():
11+
a = "This variable should not be shown."
12+
__tracebackhide__ = {is_hidden}
13+
14+
15+
helper()
16+
17+
18+
def helper():
19+
raise Exception
20+
"""
21+
tmp_path.joinpath("task_main.py").write_text(textwrap.dedent(source))
22+
23+
result = runner.invoke(cli, [tmp_path.as_posix(), "--show-locals"])
24+
25+
assert result.exit_code == 1
26+
assert ("This variable should not be shown." in result.output) is not is_hidden

‎tox.ini

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ignore =
6868
max-line-length = 88
6969
per-file-ignores =
7070
src/_pytask/hookspecs.py: U100
71+
src/_pytask/outcomes.py: N818
7172
tests/test_capture.py: T000, T001, N802, PT011
7273
pytest-mark-no-parentheses = true
7374
warn-symbols =

0 commit comments

Comments
 (0)
Please sign in to comment.