From 15793461e346eb21be3381dc5cf9cd6e19265371 Mon Sep 17 00:00:00 2001 From: Liam Beguin Date: Fri, 22 Dec 2023 13:33:34 -0500 Subject: [PATCH] make sure rich_markup_mode=None disables rich MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As stated in the documentation, rich_markup_mode=None is the default mode. Make sure that this is true even when rich is installed, fix current tests assuming it's on by default, and add new test cases to validate rich_markup_mode options. Signed-off-by: Liam Beguin 🎨 [pre-commit.ci] Auto format from pre-commit.com hooks --- tests/test_rich_markup_mode.py | 34 +++++++++++++++++++ .../test_help/test_tutorial007.py | 2 +- .../test_help/test_tutorial007_an.py | 2 +- .../test_help/test_tutorial003.py | 1 + .../test_help/test_tutorial002.py | 2 +- .../test_help/test_tutorial002_an.py | 2 +- typer/core.py | 13 ++++--- 7 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 tests/test_rich_markup_mode.py diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py new file mode 100644 index 0000000000..f9dba010ed --- /dev/null +++ b/tests/test_rich_markup_mode.py @@ -0,0 +1,34 @@ +import typer +import typer.completion +from typer.testing import CliRunner + +runner = CliRunner() +rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"] + + +def test_rich_markup_mode_default(): + app = typer.Typer() + + @app.command() + def main(arg: str): + """Main function""" + print("Hello World") + + assert app.rich_markup_mode == None + + result = runner.invoke(app, ["--help"]) + assert all(c not in result.stdout for c in rounded) + + +def test_rich_markup_mode_rich(): + app = typer.Typer(rich_markup_mode="rich") + + @app.command() + def main(arg: str): + """Main function""" + print("Hello World") + + assert app.rich_markup_mode == "rich" + + result = runner.invoke(app, ["--help"]) + assert any(c in result.stdout for c in rounded) diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial007.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial007.py index 3c6eddd028..01ec01a593 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial007.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial007.py @@ -9,7 +9,7 @@ runner = CliRunner() -app = typer.Typer() +app = typer.Typer(rich_markup_mode="rich") app.command()(mod.main) diff --git a/tests/test_tutorial/test_arguments/test_help/test_tutorial007_an.py b/tests/test_tutorial/test_arguments/test_help/test_tutorial007_an.py index fae243df06..eb898a092e 100644 --- a/tests/test_tutorial/test_arguments/test_help/test_tutorial007_an.py +++ b/tests/test_tutorial/test_arguments/test_help/test_tutorial007_an.py @@ -9,7 +9,7 @@ runner = CliRunner() -app = typer.Typer() +app = typer.Typer(rich_markup_mode="rich") app.command()(mod.main) diff --git a/tests/test_tutorial/test_commands/test_help/test_tutorial003.py b/tests/test_tutorial/test_commands/test_help/test_tutorial003.py index 7d5f47fa62..be92e04ea8 100644 --- a/tests/test_tutorial/test_commands/test_help/test_tutorial003.py +++ b/tests/test_tutorial/test_commands/test_help/test_tutorial003.py @@ -6,6 +6,7 @@ from docs_src.commands.help import tutorial003 as mod app = mod.app +app.rich_markup_mode = "rich" runner = CliRunner() diff --git a/tests/test_tutorial/test_options/test_help/test_tutorial002.py b/tests/test_tutorial/test_options/test_help/test_tutorial002.py index 260d6453e8..10ac2c20b6 100644 --- a/tests/test_tutorial/test_options/test_help/test_tutorial002.py +++ b/tests/test_tutorial/test_options/test_help/test_tutorial002.py @@ -8,7 +8,7 @@ runner = CliRunner() -app = typer.Typer() +app = typer.Typer(rich_markup_mode="rich") app.command()(mod.main) diff --git a/tests/test_tutorial/test_options/test_help/test_tutorial002_an.py b/tests/test_tutorial/test_options/test_help/test_tutorial002_an.py index 7d61a41dd8..0517cd7451 100644 --- a/tests/test_tutorial/test_options/test_help/test_tutorial002_an.py +++ b/tests/test_tutorial/test_options/test_help/test_tutorial002_an.py @@ -8,7 +8,7 @@ runner = CliRunner() -app = typer.Typer() +app = typer.Typer(rich_markup_mode="rich") app.command()(mod.main) diff --git a/typer/core.py b/typer/core.py index 0c73784c23..8a3baabab0 100644 --- a/typer/core.py +++ b/typer/core.py @@ -178,6 +178,7 @@ def _main( complete_var: Optional[str] = None, standalone_mode: bool = True, windows_expand_args: bool = True, + rich_markup_mode: MarkupMode = None, **extra: Any, ) -> Any: # Typer override, duplicated from click.main() to handle custom rich exceptions @@ -232,7 +233,7 @@ def _main( if not standalone_mode: raise # Typer override - if rich: + if rich and rich_markup_mode is not None: rich_utils.rich_format_error(e) else: e.show() @@ -262,7 +263,7 @@ def _main( if not standalone_mode: raise # Typer override - if rich: + if rich and rich_markup_mode is not None: rich_utils.rich_abort_error() else: click.echo(_("Aborted!"), file=sys.stderr) @@ -712,6 +713,7 @@ def main( complete_var: Optional[str] = None, standalone_mode: bool = True, windows_expand_args: bool = True, + rich_markup_mode: MarkupMode = None, **extra: Any, ) -> Any: return _main( @@ -721,11 +723,12 @@ def main( complete_var=complete_var, standalone_mode=standalone_mode, windows_expand_args=windows_expand_args, + rich_markup_mode=rich_markup_mode, **extra, ) def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None: - if not rich: + if not rich or self.rich_markup_mode is None: return super().format_help(ctx, formatter) return rich_utils.rich_format_help( obj=self, @@ -774,6 +777,7 @@ def main( complete_var: Optional[str] = None, standalone_mode: bool = True, windows_expand_args: bool = True, + rich_markup_mode: MarkupMode = None, **extra: Any, ) -> Any: return _main( @@ -783,11 +787,12 @@ def main( complete_var=complete_var, standalone_mode=standalone_mode, windows_expand_args=windows_expand_args, + rich_markup_mode=rich_markup_mode, **extra, ) def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None: - if not rich: + if not rich or self.rich_markup_mode is None: return super().format_help(ctx, formatter) return rich_utils.rich_format_help( obj=self,