From b0abe955eb76f1b05474cf163a65e3a25c5f2308 Mon Sep 17 00:00:00 2001 From: Jacob Kochems Date: Thu, 3 Aug 2023 16:39:39 +0200 Subject: [PATCH] Add a separate switch for Rich traceback --- tests/assets/enable_rich.py | 2 +- tests/assets/enable_rich_traceback_false.py | 14 ++++++++++++++ tests/test_enable_rich.py | 12 ++++++++++++ typer/__init__.py | 3 ++- typer/main.py | 12 +++++++++--- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 tests/assets/enable_rich_traceback_false.py diff --git a/tests/assets/enable_rich.py b/tests/assets/enable_rich.py index f2baa05c28..43f348684c 100644 --- a/tests/assets/enable_rich.py +++ b/tests/assets/enable_rich.py @@ -1,6 +1,6 @@ import typer -typer.enable_rich(False) +typer.enable_rich_help(False) app = typer.Typer() diff --git a/tests/assets/enable_rich_traceback_false.py b/tests/assets/enable_rich_traceback_false.py new file mode 100644 index 0000000000..e2afa83972 --- /dev/null +++ b/tests/assets/enable_rich_traceback_false.py @@ -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() diff --git a/tests/test_enable_rich.py b/tests/test_enable_rich.py index 99ddd6057b..559edf2c8f 100644 --- a/tests/test_enable_rich.py +++ b/tests/test_enable_rich.py @@ -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 diff --git a/typer/__init__.py b/typer/__init__.py index 5f7c5760b0..395ce6caf3 100644 --- a/typer/__init__.py +++ b/typer/__init__.py @@ -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 diff --git a/typer/main.py b/typer/main.py index 876922fca6..e441c4cf28 100644 --- a/typer/main.py +++ b/typer/main.py @@ -20,7 +20,6 @@ TyperCommand, TyperGroup, TyperOption, - is_rich_enabled, set_rich_output, ) from .models import ( @@ -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: @@ -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,