diff --git a/docs/source/changes.md b/docs/source/changes.md index 3f7ce9b6..819234cd 100644 --- a/docs/source/changes.md +++ b/docs/source/changes.md @@ -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 diff --git a/src/_pytask/logging.py b/src/_pytask/logging.py index 545562a1..bd6ea4dd 100644 --- a/src/_pytask/logging.py +++ b/src/_pytask/logging.py @@ -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 @@ -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"] @@ -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), diff --git a/src/_pytask/traceback.py b/src/_pytask/traceback.py index fec29704..c8d21d4a 100644 --- a/src/_pytask/traceback.py +++ b/src/_pytask/traceback.py @@ -13,6 +13,7 @@ import pluggy from attrs import define +from attrs import field from rich.traceback import Traceback as RichTraceback import _pytask @@ -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: @@ -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 ) diff --git a/tests/test_traceback.py b/tests/test_traceback.py index 6348484e..d3276530 100644 --- a/tests/test_traceback.py +++ b/tests/test_traceback.py @@ -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