Skip to content

Reset class variables of ExecutionReport and Traceback. #588

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 3 commits into from
Mar 30, 2024
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
3 changes: 3 additions & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
- {pull}`579` fixes an interaction with `--pdb` and `--trace` and task that return. The
debugging modes swallowed the return and `None` was returned. Closes {issue}`574`.
- {pull}`581` simplifies the code for tracebacks and unpublishes some utility functions.
- {pull}`586` improves linting.
- {pull}`587` improves typing of `capture.py`.
- {pull}`588` resets class variables of `ExecutionReport` and `Traceback`.

## 0.4.6 - 2024-03-13

Expand Down
12 changes: 11 additions & 1 deletion src/_pytask/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from rich.text import Text

import _pytask
from _pytask.capture_utils import ShowCapture
from _pytask.console import IS_WINDOWS_TERMINAL
from _pytask.console import console
from _pytask.pluginmanager import hookimpl
Expand Down Expand Up @@ -67,7 +68,7 @@ def pytask_parse_config(config: dict[str, Any]) -> None:
@hookimpl
def pytask_post_parse(config: dict[str, Any]) -> None:
# Set class variables on traceback object.
Traceback.show_locals = config["show_locals"]
Traceback._show_locals = config["show_locals"]
# Set class variables on Executionreport.
ExecutionReport.editor_url_scheme = config["editor_url_scheme"]
ExecutionReport.show_capture = config["show_capture"]
Expand Down Expand Up @@ -124,6 +125,15 @@ def pytask_log_session_footer(
console.rule(message, style=outcome.style)


@hookimpl
def pytask_unconfigure() -> None:
"""Reset class variables."""
Traceback._show_locals = False
ExecutionReport.editor_url_scheme = "file"
ExecutionReport.show_capture = ShowCapture.ALL
ExecutionReport.show_locals = False


_TIME_UNITS: list[_TimeUnit] = [
_TimeUnit(singular="day", plural="days", short="d", in_seconds=86400),
_TimeUnit(singular="hour", plural="hours", short="h", in_seconds=3600),
Expand Down
12 changes: 9 additions & 3 deletions src/_pytask/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import pluggy
from attrs import define
from attrs import field
from rich.traceback import Traceback as RichTraceback

import _pytask
Expand Down Expand Up @@ -45,14 +46,19 @@
@define
class Traceback:
exc_info: OptionalExceptionInfo
show_locals: bool = field()

show_locals: ClassVar[bool] = False
_show_locals: ClassVar[bool] = False
suppress: ClassVar[tuple[Path, ...]] = (
_PLUGGY_DIRECTORY,
TREE_UTIL_LIB_DIRECTORY,
_PYTASK_DIRECTORY,
TREE_UTIL_LIB_DIRECTORY,
)

@show_locals.default
def _show_locals_default(self) -> bool:
return self._show_locals

def __rich_console__(
self, console: Console, console_options: ConsoleOptions
) -> RenderResult:
Expand All @@ -68,7 +74,7 @@ def __rich_console__(
yield filtered_exc_info[2]
else:
yield RichTraceback.from_exception(
*filtered_exc_info, show_locals=self.show_locals
*filtered_exc_info, show_locals=self._show_locals
)


Expand Down
10 changes: 10 additions & 0 deletions tests/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,13 @@ def test_render_traceback_with_string_traceback():
traceback = Traceback((Exception, Exception("Help"), "String traceback."))
rendered = render_to_string(traceback, console)
assert "String traceback." in rendered


@pytest.mark.unit()
def test_passing_show_locals():
traceback = Traceback(
(Exception, Exception("Help"), "String traceback."), show_locals=True
)
assert traceback.show_locals is True
# Also tests that the class variable has been reset.
assert traceback._show_locals is False