Skip to content
Myr

🐛 Ensure `rich_markup_mode=None` disables Rich formatting (#859)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent f17bb06 · Browse files · 4 files+64 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+64 added−11 removed

  • tests:+40 added−0 removed
  • other:+24 added−11 removed

4 files+64 added−11 removed

modified:docs/tutorial/commands/help.md+1 added-1 removed
206 unmodified lines
207Then you can use more formatting in the docstrings and the `help` parameter for *CLI arguments* and *CLI options*. You will see more about it below. 👇208209/// info210211By default, `rich_markup_mode` is `None`, which disables any rich text formatting.211By default, `rich_markup_mode` is `None` if Rich is not installed, and `"rich"` if it is installed. In the latter case, you can set `rich_markup_mode` to `None` to disable rich text formatting.212213///214215### Rich Markup
358 unmodified lines
added:tests/test_rich_markup_mode.py+40 added
1import typer2import typer.completion3from typer.testing import CliRunner45runner = CliRunner()6rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"]789def test_rich_markup_mode_none():10 app = typer.Typer(rich_markup_mode=None)1112 @app.command()13 def main(arg: str):14 """Main function"""15 print(f"Hello {arg}")1617 assert app.rich_markup_mode is None1819 result = runner.invoke(app, ["World"])20 assert "Hello World" in result.stdout2122 result = runner.invoke(app, ["--help"])23 assert all(c not in result.stdout for c in rounded)242526def test_rich_markup_mode_rich():27 app = typer.Typer(rich_markup_mode="rich")2829 @app.command()30 def main(arg: str):31 """Main function"""32 print(f"Hello {arg}")3334 assert app.rich_markup_mode == "rich"3536 result = runner.invoke(app, ["World"])37 assert "Hello World" in result.stdout3839 result = runner.invoke(app, ["--help"])40 assert any(c in result.stdout for c in rounded)
modified:typer/core.py+14 added-8 removed
29 unmodified lines
30 from typing import Literal31else:32 from typing_extensions import Literal3334MarkupMode = Literal["markdown", "rich", None]3536try:37 import rich3839 from . import rich_utils4041 DEFAULT_MARKUP_MODE: MarkupMode = "rich"4243except ImportError: # pragma: no cover44 rich = None # type: ignore45 DEFAULT_MARKUP_MODE = None4642MarkupMode = Literal["markdown", "rich", None]474448# Copy from click.parser._split_opt49def _split_opt(opt: str) -> Tuple[str, str]:50 first = opt[:1]51 if first.isalnum():
117 unmodified lines
169 prog_name: Optional[str] = None,170 complete_var: Optional[str] = None,171 standalone_mode: bool = True,172 windows_expand_args: bool = True,173 rich_markup_mode: MarkupMode = DEFAULT_MARKUP_MODE,174 **extra: Any,175) -> Any:176 # Typer override, duplicated from click.main() to handle custom rich exceptions177 # Verify that the environment is configured correctly, or reject
33 unmodified lines
211 except click.ClickException as e:212 if not standalone_mode:213 raise214 # Typer override211 if rich:215 if rich and rich_markup_mode is not None:216 rich_utils.rich_format_error(e)217 else:218 e.show()219 # Typer override end
21 unmodified lines
241 except click.Abort:242 if not standalone_mode:243 raise244 # Typer override241 if rich:245 if rich and rich_markup_mode is not None:246 rich_utils.rich_abort_error()247 else:248 click.echo(_("Aborted!"), file=sys.stderr)249 # Typer override end
367 unmodified lines
617 no_args_is_help: bool = False,618 hidden: bool = False,619 deprecated: bool = False,620 # Rich settings617 rich_markup_mode: MarkupMode = None,621 rich_markup_mode: MarkupMode = DEFAULT_MARKUP_MODE,622 rich_help_panel: Union[str, None] = None,623 ) -> None:624 super().__init__(625 name=name,
42 unmodified lines
668 prog_name=prog_name,669 complete_var=complete_var,670 standalone_mode=standalone_mode,671 windows_expand_args=windows_expand_args,672 rich_markup_mode=self.rich_markup_mode,673 **extra,674 )675676 def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:672 if not rich:677 if not rich or self.rich_markup_mode is None:678 return super().format_help(ctx, formatter)679 return rich_utils.rich_format_help(680 obj=self,681 ctx=ctx,
9 unmodified lines
691 commands: Optional[692 Union[Dict[str, click.Command], Sequence[click.Command]]693 ] = None,694 # Rich settings690 rich_markup_mode: MarkupMode = None,695 rich_markup_mode: MarkupMode = DEFAULT_MARKUP_MODE,696 rich_help_panel: Union[str, None] = None,697 **attrs: Any,698 ) -> None:699 super().__init__(name=name, commands=commands, **attrs)
31 unmodified lines
731 prog_name=prog_name,732 complete_var=complete_var,733 standalone_mode=standalone_mode,734 windows_expand_args=windows_expand_args,735 rich_markup_mode=self.rich_markup_mode,736 **extra,737 )738739 def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> None:734 if not rich:740 if not rich or self.rich_markup_mode is None:741 return super().format_help(ctx, formatter)742 return rich_utils.rich_format_help(743 obj=self,744 ctx=ctx,
2 unmodified lines
modified:typer/main.py+9 added-2 removed
13 unmodified lines
14import click1516from ._typing import get_args, get_origin, is_union17from .completion import get_completion_inspect_parameters18from .core import MarkupMode, TyperArgument, TyperCommand, TyperGroup, TyperOption18from .core import (19 DEFAULT_MARKUP_MODE,20 MarkupMode,21 TyperArgument,22 TyperCommand,23 TyperGroup,24 TyperOption,25)26from .models import (27 AnyType,28 ArgumentInfo,29 CommandFunctionType,
113 unmodified lines
143 hidden: bool = Default(False),144 deprecated: bool = Default(False),145 add_completion: bool = True,146 # Rich settings140 rich_markup_mode: MarkupMode = None,147 rich_markup_mode: MarkupMode = Default(DEFAULT_MARKUP_MODE),148 rich_help_panel: Union[str, None] = Default(None),149 pretty_exceptions_enable: bool = True,150 pretty_exceptions_show_locals: bool = True,151 pretty_exceptions_short: bool = True,
929 unmodified lines