Skip to content
Myr

🐛 Fix highlighting of optional variadic argument metavars (#1508)

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

authored by Benjy Wiener · committed by GitHub · parent 8fe2b8e · Browse files · 3 files+47 added−10 removed
This commit

State

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

Trailers

  • Co-authored-by: Benjy Wiener <benjywiener@gmail.com>
  • Co-authored-by: svlandeg <svlandeg@github.com>
  • Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>

Diff stats

3 files+47 added−10 removed

  • tests:+37 added−0 removed
  • other:+10 added−10 removed

3 files+47 added−10 removed

modified:tests/test_rich_utils.py+34 added
1import sys23import pytest4import typer5import typer.completion6from typer.testing import CliRunner78from tests.utils import needs_rich910runner = CliRunner()111213def test_rich_utils_click_rewrapp():
89 unmodified lines
103 assert "Usage" in result.stdout104 assert "BAR" in result.stdout105106107@needs_rich108@pytest.mark.parametrize("input_text", ["[ARGS]", "[ARGS]..."])109def test_metavar_highlighter(input_text: str):110 """111 Test that the MetavarHighlighter works correctly.112 cf PR 1508113 """114 from typer.rich_utils import (115 STYLE_METAVAR_SEPARATOR,116 Text,117 _get_rich_console,118 metavar_highlighter,119 )120121 console = _get_rich_console()122123 text = Text(input_text)124 highlighted = metavar_highlighter(text)125 console.print(highlighted)126127 # Get the style for each bracket128 opening_bracket_style = highlighted.get_style_at_offset(console, 0)129 closing_bracket_style = highlighted.get_style_at_offset(console, 5)130131 # The opening bracket should have metavar_sep style132 assert str(opening_bracket_style) == STYLE_METAVAR_SEPARATOR133134 # The closing bracket should have metavar_sep style (fails before PR 1508 when there are 3 dots)135 assert str(closing_bracket_style) == STYLE_METAVAR_SEPARATOR136137138def test_make_rich_text_with_ansi_escape_sequences():139 from typer.rich_utils import Text, _make_rich_text140141 ansi_text = "This is \x1b[4munderlined\x1b[0m text"
83 unmodified lines
modified:tests/utils.py+3 added
2 unmodified lines
3from os import getenv45import pytest6from typer._completion_shared import _get_shell_name7from typer.core import HAS_RICH89needs_py310 = pytest.mark.skipif(10 sys.version_info < (3, 10), reason="requires python3.10+"11)
1 unmodified line
13needs_linux = pytest.mark.skipif(14 not sys.platform.startswith("linux"), reason="Test requires Linux"15)1617needs_rich = pytest.mark.skipif(not HAS_RICH, reason="Test requires Rich")1819shell = _get_shell_name()20needs_bash = pytest.mark.skipif(21 shell is None or "bash" not in shell, reason="Test requires Bash"22)
24 unmodified lines
modified:typer/rich_utils.py+10 added-10 removed
119 unmodified lines
120 r"(^|\W)(?P<negative_option>\-\-[\w\-]+)(?![a-zA-Z0-9])",121 ]122123124# Highlighter to make [ | ] and <> dim125class MetavarHighlighter(RegexHighlighter):126 highlights = [127 r"^(?P<metavar_sep>(\[|<))",128 r"(?P<metavar_sep>\|)",129 r"(?P<metavar_sep>(\]|>))(\.\.\.)?$",130 ]131132133highlighter = OptionHighlighter()134negative_highlighter = NegativeOptionHighlighter()135metavar_highlighter = MetavarHighlighter()136137138def _has_ansi_character(text: str) -> bool:139 return ANSI_PREFIX in text
270 unmodified lines
410 required: Union[str, Text] = ""411 if param.required:412 required = Text(REQUIRED_SHORT_STRING, style=STYLE_REQUIRED_SHORT)413404 # Highlighter to make [ | ] and <> dim405 class MetavarHighlighter(RegexHighlighter):406 highlights = [407 r"^(?P<metavar_sep>(\[|<))",408 r"(?P<metavar_sep>\|)",409 r"(?P<metavar_sep>(\]|>)$)",410 ]411412 metavar_highlighter = MetavarHighlighter()413414 required_rows.append(required)415 options_rows.append(416 [417 highlighter(",".join(opt_long_strs)),
343 unmodified lines