🐛 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
Provenance
- Commit
fc2c54f- Tree
193522e- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f17bb06
4 files changed+64 added−11 removed
modified:docs/tutorial/commands/help.md+1 added-1 removed
206 unmodified lines207 Then 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. 👇208 209 /// info210 211-By default, `rich_markup_mode` is `None`, which disables any rich text formatting.211+By 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.212 213 ///214 215 ### Rich Markup358 unmodified linesadded:tests/test_rich_markup_mode.py+40 added
1+import typer2+import typer.completion3+from typer.testing import CliRunner4+5+runner = CliRunner()6+rounded = ["╭", "─", "┬", "╮", "│", "├", "┼", "┤", "╰", "┴", "╯"]7+8+9+def test_rich_markup_mode_none():10+ app = typer.Typer(rich_markup_mode=None)11+12+ @app.command()13+ def main(arg: str):14+ """Main function"""15+ print(f"Hello {arg}")16+17+ assert app.rich_markup_mode is None18+19+ result = runner.invoke(app, ["World"])20+ assert "Hello World" in result.stdout21+22+ result = runner.invoke(app, ["--help"])23+ assert all(c not in result.stdout for c in rounded)24+25+26+def test_rich_markup_mode_rich():27+ app = typer.Typer(rich_markup_mode="rich")28+29+ @app.command()30+ def main(arg: str):31+ """Main function"""32+ print(f"Hello {arg}")33+34+ assert app.rich_markup_mode == "rich"35+36+ result = runner.invoke(app, ["World"])37+ assert "Hello World" in result.stdout38+39+ 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 lines30 from typing import Literal31 else:32 from typing_extensions import Literal33 34+MarkupMode = Literal["markdown", "rich", None]35+36 try:37 import rich38 39 from . import rich_utils40 41+ DEFAULT_MARKUP_MODE: MarkupMode = "rich"42+43 except ImportError: # pragma: no cover44 rich = None # type: ignore45+ DEFAULT_MARKUP_MODE = None46 42-MarkupMode = Literal["markdown", "rich", None]47 44-48 # Copy from click.parser._split_opt49 def _split_opt(opt: str) -> Tuple[str, str]:50 first = opt[:1]51 if first.isalnum():117 unmodified lines169 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 reject33 unmodified lines211 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 end21 unmodified lines241 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 end367 unmodified lines617 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 lines668 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 )675 676 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 lines691 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 lines731 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 )738 739 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 linesmodified:typer/main.py+9 added-2 removed
13 unmodified lines14 import click15 16 from ._typing import get_args, get_origin, is_union17 from .completion import get_completion_inspect_parameters18-from .core import MarkupMode, TyperArgument, TyperCommand, TyperGroup, TyperOption18+from .core import (19+ DEFAULT_MARKUP_MODE,20+ MarkupMode,21+ TyperArgument,22+ TyperCommand,23+ TyperGroup,24+ TyperOption,25+)26 from .models import (27 AnyType,28 ArgumentInfo,29 CommandFunctionType,113 unmodified lines143 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