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

feat: Add pretty_exceptions_suppress option to extend suppressed frames in pretty exceptions #770

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/tutorial/exceptions.md
Expand Up @@ -202,6 +202,18 @@ $ python main.py

</div>

## Suppress Frames from Pretty Exceptions

By default, typer will **omit** all the parts of the traceback that come from the internal parts in Typer and Click, but you can exclude frames from additional
frameworks using `pretty_exceptions_suppress`, which should be a list of modules or str paths.

```Python
import typer
import sqlalchemy

app = typer.Typer(pretty_exceptions_suppress=[sqlalchemy])
```

## Disable Pretty Exceptions

You can also entirely disable pretty exceptions with the parameter `pretty_exceptions_enable=False`:
Expand Down
23 changes: 20 additions & 3 deletions typer/main.py
Expand Up @@ -7,8 +7,19 @@
from functools import update_wrapper
from pathlib import Path
from traceback import FrameSummary, StackSummary
from types import TracebackType
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple, Type, Union
from types import ModuleType, TracebackType
from typing import (
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Sequence,
Tuple,
Type,
Union,
)
from uuid import UUID

import click
Expand Down Expand Up @@ -67,14 +78,17 @@ def except_hook(
typer_path = os.path.dirname(__file__)
click_path = os.path.dirname(click.__file__)
supress_internal_dir_names = [typer_path, click_path]
suppress = supress_internal_dir_names + list(
exception_config.pretty_exceptions_suppress
)
exc = exc_value
if rich:
rich_tb = Traceback.from_exception(
type(exc),
exc,
exc.__traceback__,
show_locals=exception_config.pretty_exceptions_show_locals,
suppress=supress_internal_dir_names,
suppress=suppress,
)
console_stderr.print(rich_tb)
return
Expand Down Expand Up @@ -137,13 +151,15 @@ def __init__(
pretty_exceptions_enable: bool = True,
pretty_exceptions_show_locals: bool = True,
pretty_exceptions_short: bool = True,
pretty_exceptions_suppress: Iterable[Union[str, ModuleType]] = (),
):
self._add_completion = add_completion
self.rich_markup_mode: MarkupMode = rich_markup_mode
self.rich_help_panel = rich_help_panel
self.pretty_exceptions_enable = pretty_exceptions_enable
self.pretty_exceptions_show_locals = pretty_exceptions_show_locals
self.pretty_exceptions_short = pretty_exceptions_short
self.pretty_exceptions_suppress = pretty_exceptions_suppress
self.info = TyperInfo(
name=name,
cls=cls,
Expand Down Expand Up @@ -321,6 +337,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
pretty_exceptions_enable=self.pretty_exceptions_enable,
pretty_exceptions_show_locals=self.pretty_exceptions_show_locals,
pretty_exceptions_short=self.pretty_exceptions_short,
pretty_exceptions_suppress=self.pretty_exceptions_suppress,
),
)
raise e
Expand Down
4 changes: 4 additions & 0 deletions typer/models.py
@@ -1,10 +1,12 @@
import inspect
import io
from types import ModuleType
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Sequence,
Expand Down Expand Up @@ -513,7 +515,9 @@ def __init__(
pretty_exceptions_enable: bool = True,
pretty_exceptions_show_locals: bool = True,
pretty_exceptions_short: bool = True,
pretty_exceptions_suppress: Iterable[Union[str, ModuleType]] = (),
) -> None:
self.pretty_exceptions_enable = pretty_exceptions_enable
self.pretty_exceptions_show_locals = pretty_exceptions_show_locals
self.pretty_exceptions_short = pretty_exceptions_short
self.pretty_exceptions_suppress = pretty_exceptions_suppress