From 8db2ce154ee23e4736e5da393a409eea54eba385 Mon Sep 17 00:00:00 2001 From: mshzy <156560471+mshzy@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:26:44 +0800 Subject: [PATCH 1/3] fix: respect TYPER_USE_RICH in shell completion help text _sanitize_help_text() checked whether rich is installed but ignored the HAS_RICH flag from TYPER_USE_RICH env var. When users set TYPER_USE_RICH=false, completions would still import Rich. Fix: import HAS_RICH from .core and check it. Fixes #1643 --- typer/_completion_classes.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/typer/_completion_classes.py b/typer/_completion_classes.py index cfae02c9bf..5380ead597 100644 --- a/typer/_completion_classes.py +++ b/typer/_completion_classes.py @@ -14,12 +14,19 @@ COMPLETION_SCRIPT_ZSH, Shells, ) +from .core import HAS_RICH def _sanitize_help_text(text: str) -> str: - """Sanitizes the help text by removing rich tags""" + """Sanitizes the help text by removing rich tags. + + Checks both that rich is installed AND that it is enabled + (respecting the TYPER_USE_RICH environment variable). + """ if not importlib.util.find_spec("rich"): return text + if not HAS_RICH: + return text from . import rich_utils return rich_utils.rich_render_text(text) From 9091c4f01bfa5eedf777305f5415f0d1d692640e Mon Sep 17 00:00:00 2001 From: mshzy <156560471+mshzy@users.noreply.github.com> Date: Tue, 2 Jun 2026 19:43:45 +0800 Subject: [PATCH 2/3] fix: add test coverage for HAS_RICH guard in _sanitize_help_text --- tests/test_completion/test_sanitization.py | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/test_completion/test_sanitization.py b/tests/test_completion/test_sanitization.py index 9150b94cd5..465ad5a0d5 100644 --- a/tests/test_completion/test_sanitization.py +++ b/tests/test_completion/test_sanitization.py @@ -36,3 +36,31 @@ 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( + "has_rich, help_text, expected", + [ + ( + False, + "help [bold]with[/] rich tags", + "help [bold]with[/] rich tags", + ), + ( + True, + "help [bold]with[/] rich tags", + "help with rich tags", + ), + ], +) +def test_sanitize_help_text_respects_has_rich( + has_rich: bool, help_text: str, expected: str +): + """When HAS_RICH is False, rich tags are preserved even if rich is installed.""" + with patch( + "typer._completion_classes.HAS_RICH", has_rich + ), patch( + "importlib.util.find_spec", + return_value=ModuleSpec("rich", loader=None), + ): + assert _sanitize_help_text(help_text) == expected From 7588e6111ca69f6744ec012626b0bcdd474b82dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Tue, 2 Jun 2026 11:44:28 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8E=A8=20Auto=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_completion/test_sanitization.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_completion/test_sanitization.py b/tests/test_completion/test_sanitization.py index 465ad5a0d5..bf6ed7bd04 100644 --- a/tests/test_completion/test_sanitization.py +++ b/tests/test_completion/test_sanitization.py @@ -57,10 +57,11 @@ def test_sanitize_help_text_respects_has_rich( has_rich: bool, help_text: str, expected: str ): """When HAS_RICH is False, rich tags are preserved even if rich is installed.""" - with patch( - "typer._completion_classes.HAS_RICH", has_rich - ), patch( - "importlib.util.find_spec", - return_value=ModuleSpec("rich", loader=None), + with ( + patch("typer._completion_classes.HAS_RICH", has_rich), + patch( + "importlib.util.find_spec", + return_value=ModuleSpec("rich", loader=None), + ), ): assert _sanitize_help_text(help_text) == expected