Skip to content
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

Simplify the code related to tracebacks. #581

Merged
merged 2 commits into from
Mar 23, 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
1 change: 1 addition & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
- {pull}`573` removes the `pytask_execute_create_scheduler` hook.
- {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.

## 0.4.6 - 2024-03-13

Expand Down
1 change: 0 additions & 1 deletion docs/source/reference_guides/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ resolution and execution.
## Tracebacks

```{eval-rst}
.. autofunction:: pytask.remove_internal_traceback_frames_from_exc_info
.. autoclass:: pytask.Traceback
```

Expand Down
13 changes: 4 additions & 9 deletions src/_pytask/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Literal

import click
from rich.traceback import Traceback

from _pytask.capture_utils import CaptureMethod
from _pytask.capture_utils import ShowCapture
Expand All @@ -34,7 +33,7 @@
from _pytask.session import Session
from _pytask.shared import parse_paths
from _pytask.shared import to_list
from _pytask.traceback import remove_internal_traceback_frames_from_exc_info
from _pytask.traceback import Traceback

if TYPE_CHECKING:
from typing import NoReturn
Expand Down Expand Up @@ -66,7 +65,7 @@ def pytask_unconfigure(session: Session) -> None:
path.write_text(json.dumps(HashPathCache._cache))


def build( # noqa: C901, PLR0912, PLR0913, PLR0915
def build( # noqa: C901, PLR0912, PLR0913
*,
capture: Literal["fd", "no", "sys", "tee-sys"] | CaptureMethod = CaptureMethod.FD,
check_casing_of_paths: bool = True,
Expand Down Expand Up @@ -257,9 +256,7 @@ def build( # noqa: C901, PLR0912, PLR0913, PLR0915
session = Session.from_config(config_)

except (ConfigurationError, Exception):
exc_info = remove_internal_traceback_frames_from_exc_info(sys.exc_info())
traceback = Traceback.from_exception(*exc_info)
console.print(traceback)
console.print(Traceback(sys.exc_info()))
session = Session(exit_code=ExitCode.CONFIGURATION_FAILED)

else:
Expand All @@ -279,9 +276,7 @@ def build( # noqa: C901, PLR0912, PLR0913, PLR0915
session.exit_code = ExitCode.FAILED

except Exception: # noqa: BLE001
exc_info = remove_internal_traceback_frames_from_exc_info(sys.exc_info())
traceback = Traceback.from_exception(*exc_info)
console.print(traceback)
console.print(Traceback(sys.exc_info()))
session.exit_code = ExitCode.FAILED

session.hook.pytask_unconfigure(session=session)
Expand Down
6 changes: 2 additions & 4 deletions src/_pytask/dag_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import click
import networkx as nx
from rich.text import Text
from rich.traceback import Traceback

from _pytask.click import ColoredCommand
from _pytask.click import EnumChoice
Expand All @@ -31,7 +30,7 @@
from _pytask.shared import parse_paths
from _pytask.shared import reduce_names_of_multiple_nodes
from _pytask.shared import to_list
from _pytask.traceback import remove_internal_traceback_frames_from_exc_info
from _pytask.traceback import Traceback


class _RankDirection(enum.Enum):
Expand Down Expand Up @@ -114,9 +113,8 @@ def dag(**raw_config: Any) -> int:

except Exception: # noqa: BLE001
session.exit_code = ExitCode.FAILED
exc_info = remove_internal_traceback_frames_from_exc_info(sys.exc_info())
console.print()
console.print(Traceback.from_exception(*exc_info))
console.print(Traceback(sys.exc_info()))
console.rule(style="failed")

session.hook.pytask_unconfigure(session=session)
Expand Down
7 changes: 3 additions & 4 deletions src/_pytask/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

__all__ = [
"Traceback",
"remove_internal_traceback_frames_from_exc_info",
"remove_traceback_from_exc_info",
]

Expand Down Expand Up @@ -60,7 +59,7 @@ def __rich_console__(
if self.exc_info and isinstance(self.exc_info[1], Exit):
self.exc_info = remove_traceback_from_exc_info(self.exc_info)

filtered_exc_info = remove_internal_traceback_frames_from_exc_info(
filtered_exc_info = _remove_internal_traceback_frames_from_exc_info(
self.exc_info, suppress=self.suppress
)

Expand All @@ -80,7 +79,7 @@ def remove_traceback_from_exc_info(
return (exc_info[0], exc_info[1], None) # type: ignore[return-value]


def remove_internal_traceback_frames_from_exc_info(
def _remove_internal_traceback_frames_from_exc_info(
exc_info: OptionalExceptionInfo,
suppress: tuple[Path, ...] = (
_PLUGGY_DIRECTORY,
Expand Down Expand Up @@ -117,7 +116,7 @@ def _remove_internal_traceback_frames_from_exception(
if exc is None:
return exc

_, _, tb = remove_internal_traceback_frames_from_exc_info(
_, _, tb = _remove_internal_traceback_frames_from_exc_info(
(type(exc), exc, exc.__traceback__)
)
exc.__traceback__ = tb
Expand Down
2 changes: 0 additions & 2 deletions src/pytask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
from _pytask.session import Session
from _pytask.task_utils import task
from _pytask.traceback import Traceback
from _pytask.traceback import remove_internal_traceback_frames_from_exc_info
from _pytask.typing import Product
from _pytask.typing import is_task_function
from _pytask.warnings_utils import WarningReport
Expand Down Expand Up @@ -152,7 +151,6 @@
"parse_dependencies_from_task_function",
"parse_products_from_task_function",
"parse_warning_filter",
"remove_internal_traceback_frames_from_exc_info",
"remove_marks",
"set_marks",
"storage",
Expand Down
Loading