From 4eb4b8994ef82d26f3575c5b26f842b03f63e32e Mon Sep 17 00:00:00 2001 From: Athanasios Keramas Date: Tue, 27 Oct 2026 10:35:17 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20Fix=20Rich=20emoji=20shortco?= =?UTF-8?q?des=20in=20zsh=20completion=20help=20text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_completion/test_sanitization.py | 18 +++++++++++++++++- typer/_completion_classes.py | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/test_completion/test_sanitization.py b/tests/test_completion/test_sanitization.py index 9150b94cd5..2642eb79e7 100644 --- a/tests/test_completion/test_sanitization.py +++ b/tests/test_completion/test_sanitization.py @@ -2,7 +2,8 @@ from unittest.mock import patch import pytest -from typer._completion_classes import _sanitize_help_text +from typer._click.shell_completion import CompletionItem +from typer._completion_classes import ZshComplete, _sanitize_help_text @pytest.mark.parametrize( @@ -36,3 +37,18 @@ def test_sanitize_help_text( with patch("importlib.util.find_spec", return_value=find_spec) as mock_find_spec: assert _sanitize_help_text(help_text) == expected mock_find_spec.assert_called_once_with("rich") + + +@pytest.mark.parametrize( + "help_text, expected", + [ + (":warning: Spin up [bold]now[/bold]", '"up":"⚠ Spin up now"'), + ("Note: careful", '"up":"Note\\\\: careful"'), + ], +) +def test_zsh_format_completion_renders_help_before_escaping( + help_text: str, expected: str +): + zsh_complete = ZshComplete(None, {}, "prog", "_PROG_COMPLETE") # type: ignore[arg-type] + item = CompletionItem("up", help=help_text) + assert zsh_complete.format_completion(item) == expected diff --git a/typer/_completion_classes.py b/typer/_completion_classes.py index cfae02c9bf..01fb66785a 100644 --- a/typer/_completion_classes.py +++ b/typer/_completion_classes.py @@ -131,7 +131,7 @@ def escape(s: str) -> str: # the difference with and without escape # return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}" if item.help: - return f'"{escape(item.value)}":"{_sanitize_help_text(escape(item.help))}"' + return f'"{escape(item.value)}":"{escape(_sanitize_help_text(item.help))}"' else: return f'"{escape(item.value)}"' From 5cb2ae80825400cc09d082a3fb31f143a8a82213 Mon Sep 17 00:00:00 2001 From: Athanasios Keramas Date: Mon, 2 Nov 2026 23:41:49 +0200 Subject: [PATCH 2/2] Fix: add a $/backtick case to the parametrize list as a guard for escape() in the test --- tests/test_completion/test_sanitization.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_completion/test_sanitization.py b/tests/test_completion/test_sanitization.py index 2642eb79e7..c2c1a4e0e7 100644 --- a/tests/test_completion/test_sanitization.py +++ b/tests/test_completion/test_sanitization.py @@ -44,6 +44,7 @@ def test_sanitize_help_text( [ (":warning: Spin up [bold]now[/bold]", '"up":"⚠ Spin up now"'), ("Note: careful", '"up":"Note\\\\: careful"'), + ("Cost $5 and `x`", '"up":"Cost \\$5 and \\`x\\`"'), ], ) def test_zsh_format_completion_renders_help_before_escaping(