Skip to content

Add --show-traceback to control whether tracebacks are displayed. #187

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

Merged
merged 4 commits into from
Jan 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ all releases are available on `PyPI <https://pypi.org/project/pytask>`_ and
------------------

- :gh:`186` enhance live displays by deactivating auto-refresh among other things.
- :gh:`187` allows to enable and disable showing tracebacks and potentially different
styles in the future with :confval:`show_traceback=True|False`.
- :gh:`188` refactors some code related to :class:`_pytask.enums.ExitCode`.


Expand Down
5 changes: 5 additions & 0 deletions src/_pytask/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ def main(config_from_cli: Dict[str, Any]) -> Session:
default=None,
help="Print errors with tracebacks as soon as the task fails.",
)
@click.option(
"--show-traceback",
type=click.Choice(["yes", "no"]),
help="Choose whether tracebacks should be displayed or not. [default: yes]",
)
def build(**config_from_cli: Any) -> "NoReturn":
"""Collect and execute tasks and report the results.

Expand Down
18 changes: 10 additions & 8 deletions src/_pytask/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,18 @@ def pytask_execute_log_end(session: Session, reports: List[ExecutionReport]) ->

counts = count_outcomes(reports, TaskOutcome)

console.print()
if counts[TaskOutcome.FAIL]:
console.rule(
Text("Failures", style=TaskOutcome.FAIL.style), style=TaskOutcome.FAIL.style
)
if session.config["show_traceback"] != "no":
console.print()
if counts[TaskOutcome.FAIL]:
console.rule(
Text("Failures", style=TaskOutcome.FAIL.style),
style=TaskOutcome.FAIL.style,
)
console.print()

for report in reports:
if report.outcome in (TaskOutcome.FAIL, TaskOutcome.SKIP_PREVIOUS_FAILED):
_print_errored_task_report(session, report)
for report in reports:
if report.outcome in (TaskOutcome.FAIL, TaskOutcome.SKIP_PREVIOUS_FAILED):
_print_errored_task_report(session, report)

console.rule(style="dim")

Expand Down
28 changes: 28 additions & 0 deletions src/_pytask/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union
Expand Down Expand Up @@ -38,6 +39,13 @@ class _TimeUnit(TypedDict):
short: str
in_seconds: int

if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal

_ShowTraceback = Literal["no", "yes"]


@hookimpl
def pytask_extend_command_line_interface(cli: click.Group) -> None:
Expand Down Expand Up @@ -78,6 +86,26 @@ def pytask_parse_config(
"See https://github.com/pytask-dev/pytask/issues/171 for more information. "
"Resort to `editor_url_scheme='file'`."
)
config["show_traceback"] = get_first_non_none_value(
config_from_cli,
config_from_file,
key="show_traceback",
default="yes",
callback=_show_traceback_callback,
)


def _show_traceback_callback(
x: Optional["_ShowTraceback"],
) -> Optional["_ShowTraceback"]:
"""Validate the passed options for showing tracebacks."""
if x in [None, "None", "none"]:
x = None
elif x in ["no", "yes"]:
pass
else:
raise ValueError("'show_traceback' can only be one of ['no', 'yes'")
return x


@hookimpl
Expand Down
18 changes: 18 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from _pytask.logging import _format_plugin_names_and_versions
from _pytask.logging import _humanize_time
from _pytask.logging import pytask_log_session_footer
from _pytask.outcomes import ExitCode
from _pytask.outcomes import TaskOutcome
from pytask import cli

Expand Down Expand Up @@ -127,3 +128,20 @@ def test_humanize_time(amount, unit, short_label, expectation, expected):
with expectation:
result = _humanize_time(amount, unit, short_label)
assert result == expected


@pytest.mark.parametrize("show_traceback", ["no", "yes"])
def test_show_traceback(runner, tmp_path, show_traceback):
source = "def task_raises(): raise Exception"
tmp_path.joinpath("task_module.py").write_text(source)

result = runner.invoke(
cli, [tmp_path.as_posix(), "--show-traceback", show_traceback]
)

has_traceback = show_traceback == "yes"

assert result.exit_code == ExitCode.FAILED
assert ("Failures" in result.output) is has_traceback
assert ("Traceback" in result.output) is has_traceback
assert ("raise Exception" in result.output) is has_traceback