Skip to content

Commit

Permalink
make sure rich_markup_mode=None disables rich
Browse files Browse the repository at this point in the history
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 <liambeguin@gmail.com>

🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
  • Loading branch information
liambeguin committed Dec 22, 2023
1 parent 3a7264c commit 1579346
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
34 changes: 34 additions & 0 deletions tests/test_rich_markup_mode.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from docs_src.commands.help import tutorial003 as mod

app = mod.app
app.rich_markup_mode = "rich"

runner = CliRunner()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

runner = CliRunner()

app = typer.Typer()
app = typer.Typer(rich_markup_mode="rich")
app.command()(mod.main)


Expand Down
13 changes: 9 additions & 4 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit 1579346

Please sign in to comment.