Skip to content

Commit

Permalink
Add a separate switch for Rich traceback
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobKochems committed Aug 3, 2023
1 parent 98ea1c3 commit b0abe95
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion tests/assets/enable_rich.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typer

typer.enable_rich(False)
typer.enable_rich_help(False)

app = typer.Typer()

Expand Down
14 changes: 14 additions & 0 deletions tests/assets/enable_rich_traceback_false.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import typer

typer.enable_rich_traceback(False)

app = typer.Typer()


@app.command()
def raise_():
raise ValueError # raise some error to test traceback output


if __name__ == "__main__":
app()
12 changes: 12 additions & 0 deletions tests/test_enable_rich.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ def test_enable_rich_help_is_false():
)
# assert simple help text
assert "─" not in result.stdout


def test_enable_rich_traceback_is_false():
file_path = Path(__file__).parent / "assets/enable_rich_traceback_false.py"
result = subprocess.run(
[sys.executable, "-m", "coverage", "run", str(file_path)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
)
# assert simple help text
assert "─" not in result.stderr
3 changes: 2 additions & 1 deletion typer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

from . import colors as colors
from .main import Typer as Typer
from .main import enable_rich as enable_rich
from .main import enable_rich_help as enable_rich_help
from .main import enable_rich_traceback as enable_rich_traceback
from .main import run as run
from .models import CallbackParam as CallbackParam
from .models import Context as Context
Expand Down
12 changes: 9 additions & 3 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
TyperCommand,
TyperGroup,
TyperOption,
is_rich_enabled,
set_rich_output,
)
from .models import (
Expand Down Expand Up @@ -57,11 +56,18 @@
_original_except_hook = sys.excepthook
_typer_developer_exception_attr_name = "__typer_developer_exception__"

_is_rich_traceback_enabled = True

def enable_rich(enable: bool) -> None:

def enable_rich_help(enable: bool) -> None:
set_rich_output(enable)


def enable_rich_traceback(enable: bool) -> None:
global _is_rich_traceback_enabled
_is_rich_traceback_enabled = enable


def except_hook(
exc_type: Type[BaseException], exc_value: BaseException, tb: Optional[TracebackType]
) -> None:
Expand All @@ -80,7 +86,7 @@ def except_hook(
click_path = os.path.dirname(click.__file__)
supress_internal_dir_names = [typer_path, click_path]
exc = exc_value
if rich and is_rich_enabled():
if rich and _is_rich_traceback_enabled:
rich_tb = Traceback.from_exception(
type(exc),
exc,
Expand Down

0 comments on commit b0abe95

Please sign in to comment.