From 6321fb693c6a536c21d13bc2690c4ae872520fc5 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 15 Sep 2025 14:15:55 +0200 Subject: [PATCH 1/7] don't use Default for rich_markup_mode --- typer/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/main.py b/typer/main.py index 7ec9b8a0db..4827e0b61a 100644 --- a/typer/main.py +++ b/typer/main.py @@ -138,7 +138,7 @@ def __init__( deprecated: bool = Default(False), add_completion: bool = True, # Rich settings - rich_markup_mode: MarkupMode = Default(DEFAULT_MARKUP_MODE), + rich_markup_mode: MarkupMode = DEFAULT_MARKUP_MODE, rich_help_panel: Union[str, None] = Default(None), pretty_exceptions_enable: bool = True, pretty_exceptions_show_locals: bool = True, From 48a67d6a24e9805cf9c1083b144ea81a2aba9616 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 15 Sep 2025 14:16:16 +0200 Subject: [PATCH 2/7] small test --- tests/test_rich_markup_mode.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index df5d0323f0..3f44499346 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -3,6 +3,7 @@ import pytest import typer import typer.completion +from typer.rich_utils import MARKUP_MODE_RICH from typer.testing import CliRunner runner = CliRunner() @@ -284,3 +285,10 @@ def main(arg: str): arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0] assert help_start != -1 assert result_lines[help_start:arg_start] == lines + + +def test_markup_mode_default(): + app = typer.Typer() + + assert app.rich_markup_mode == MARKUP_MODE_RICH + From e9ef867ec6fa162a74dbb04c21d31c8f142cb918 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 15 Sep 2025 12:20:01 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_rich_markup_mode.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index 3f44499346..2dce1c6dfb 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -291,4 +291,3 @@ def test_markup_mode_default(): app = typer.Typer() assert app.rich_markup_mode == MARKUP_MODE_RICH - From 922b996e06520a40d7d5bb4e097b7e8633ab8b32 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 15 Sep 2025 14:42:27 +0200 Subject: [PATCH 4/7] remove irrelevant code paths --- typer/rich_utils.py | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index d4c3676aea..e684028c76 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -104,6 +104,7 @@ _RICH_HELP_PANEL_NAME = "rich_help_panel" MarkupMode = Literal["markdown", "rich", None] +MarkupModeStrict = Literal["markdown", "rich"] # Rich regex highlighter @@ -151,11 +152,10 @@ def _get_rich_console(stderr: bool = False) -> Console: def _make_rich_text( - *, text: str, style: str = "", markup_mode: MarkupMode + *, text: str, style: str = "", markup_mode: MarkupModeStrict ) -> Union[Markdown, Text]: """Take a string, remove indentations, and return styled text. - By default, the text is not parsed for any special formatting. If `markup_mode` is `"rich"`, the text is parsed for Rich markup strings. If `markup_mode` is `"markdown"`, parse as Markdown. """ @@ -164,17 +164,16 @@ def _make_rich_text( if markup_mode == MARKUP_MODE_MARKDOWN: text = Emoji.replace(text) return Markdown(text, style=style) - if markup_mode == MARKUP_MODE_RICH: - return highlighter(Text.from_markup(text, style=style)) else: - return highlighter(Text(text, style=style)) + assert markup_mode == MARKUP_MODE_RICH + return highlighter(Text.from_markup(text, style=style)) @group() def _get_help_text( *, obj: Union[click.Command, click.Group], - markup_mode: MarkupMode, + markup_mode: MarkupModeStrict, ) -> Iterable[Union[Markdown, Text]]: """Build primary help text for a click command or group. @@ -208,19 +207,8 @@ def _get_help_text( if remaining_paragraphs: # Add a newline inbetween the header and the remaining paragraphs yield Text("") - if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN): - # Remove single linebreaks - remaining_paragraphs = [ - x.replace("\n", " ").strip() - if not x.startswith("\b") - else "{}\n".format(x.strip("\b\n")) - for x in remaining_paragraphs - ] - # Join back together - remaining_lines = "\n".join(remaining_paragraphs) - else: - # Join with double linebreaks if markdown or Rich markup - remaining_lines = "\n\n".join(remaining_paragraphs) + # Join with double linebreaks if markdown or Rich markup + remaining_lines = "\n\n".join(remaining_paragraphs) yield _make_rich_text( text=remaining_lines, @@ -233,7 +221,7 @@ def _get_parameter_help( *, param: Union[click.Option, click.Argument, click.Parameter], ctx: click.Context, - markup_mode: MarkupMode, + markup_mode: MarkupModeStrict, ) -> Columns: """Build primary help text for a click option or argument. @@ -321,7 +309,7 @@ def _get_parameter_help( def _make_command_help( *, help_text: str, - markup_mode: MarkupMode, + markup_mode: MarkupModeStrict, ) -> Union[Text, Markdown]: """Build cli help text for a click group command. @@ -350,7 +338,7 @@ def _print_options_panel( name: str, params: Union[List[click.Option], List[click.Argument]], ctx: click.Context, - markup_mode: MarkupMode, + markup_mode: MarkupModeStrict, console: Console, ) -> None: options_rows: List[List[RenderableType]] = [] @@ -477,7 +465,7 @@ def _print_commands_panel( *, name: str, commands: List[click.Command], - markup_mode: MarkupMode, + markup_mode: MarkupModeStrict, console: Console, cmd_len: int, ) -> None: @@ -553,7 +541,7 @@ def rich_format_help( *, obj: Union[click.Command, click.Group], ctx: click.Context, - markup_mode: MarkupMode, + markup_mode: MarkupModeStrict, ) -> None: """Print nicely formatted help text using rich. From 87beb37a79d48729595ff64c622782603b4ed1f2 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Mon, 15 Sep 2025 14:48:37 +0200 Subject: [PATCH 5/7] fix comment --- typer/rich_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index e684028c76..a8b78a50b4 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -207,7 +207,7 @@ def _get_help_text( if remaining_paragraphs: # Add a newline inbetween the header and the remaining paragraphs yield Text("") - # Join with double linebreaks if markdown or Rich markup + # Join with double linebreaks for markdown and Rich markup remaining_lines = "\n\n".join(remaining_paragraphs) yield _make_rich_text( From 2c9dc215d0dd8c840bdc8d4c0c13f74c9cc524cc Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 19 Sep 2025 09:52:42 +0200 Subject: [PATCH 6/7] remove unused MarkupMode from rich_utils --- typer/rich_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index a8b78a50b4..9cec3a00bd 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -103,7 +103,6 @@ MARKUP_MODE_RICH = "rich" _RICH_HELP_PANEL_NAME = "rich_help_panel" -MarkupMode = Literal["markdown", "rich", None] MarkupModeStrict = Literal["markdown", "rich"] From acf601e4a5a61d3e2425b3b42459962520a0ebb6 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Tue, 23 Sep 2025 11:20:46 +0200 Subject: [PATCH 7/7] use string instead of importing from rich_utils --- tests/test_rich_markup_mode.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_rich_markup_mode.py b/tests/test_rich_markup_mode.py index 2dce1c6dfb..99f000bac6 100644 --- a/tests/test_rich_markup_mode.py +++ b/tests/test_rich_markup_mode.py @@ -3,7 +3,6 @@ import pytest import typer import typer.completion -from typer.rich_utils import MARKUP_MODE_RICH from typer.testing import CliRunner runner = CliRunner() @@ -288,6 +287,6 @@ def main(arg: str): def test_markup_mode_default(): + # We're assuming the test suite is run with rich installed app = typer.Typer() - - assert app.rich_markup_mode == MARKUP_MODE_RICH + assert app.rich_markup_mode == "rich"