Skip to content
Myr

🐛 Fix `TYPER_USE_RICH` parsing to allow disabling Rich completely (#1539)

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

authored by Brian Kohan · committed by GitHub · parent d1cb863 · Browse files · 3 files+56 added−3 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: svlandeg <svlandeg@github.com>

Diff stats

3 files+56 added−3 removed

  • tests:+41 added−0 removed
  • other:+15 added−3 removed

3 files+56 added−3 removed

modified:tests/test_rich_markup_mode.py+41 added
1import os2import subprocess3import sys45import pytest6import typer7import typer.completion8from typer.testing import CliRunner
37 unmodified lines
46 assert any(c in result.stdout for c in rounded)474849@pytest.mark.parametrize(50 "env_var_value, expected_result",51 [52 ("1", True),53 ("True", True),54 ("TRUE", True),55 ("true", True),56 ("0", False),57 ("False", False),58 ("FALSE", False),59 ("false", False),60 ("somerandomvalue", True),61 ],62)63def test_rich_markup_mode_envvar(env_var_value: str, expected_result: bool):64 result = subprocess.run(65 [66 sys.executable,67 "-m",68 "coverage",69 "run",70 "-m",71 "typer",72 "tests/assets/cli/sample.py",73 "--help",74 ],75 capture_output=True,76 env={77 **os.environ,78 "TYPER_USE_RICH": env_var_value,79 "PYTHONIOENCODING": "utf-8",80 },81 encoding="utf-8",82 )83 assert any(c in result.stdout for c in rounded) == expected_result848586@pytest.mark.parametrize(87 "mode,lines",88 [89 pytest.param(90 "markdown",
242 unmodified lines
modified:typer/core.py+3 added-2 removed
21 unmodified lines
22import click.types23import click.utils2425from ._typing import Literal26from .utils import parse_boolean_env_var2728MarkupMode = Literal["markdown", "rich", None]29MARKUP_MODE_KEY = "TYPER_RICH_MARKUP_MODE"3030HAS_RICH = os.getenv("TYPER_USE_RICH", True)31HAS_RICH = parse_boolean_env_var(os.getenv("TYPER_USE_RICH"), default=True)3233if HAS_RICH:34 DEFAULT_MARKUP_MODE: MarkupMode = "rich"34else: # pragma: no cover35else:36 DEFAULT_MARKUP_MODE = None373839# Copy from click.parser._split_opt
800 unmodified lines
modified:typer/utils.py+12 added-1 removed
1import inspect2import sys3from copy import copy4from typing import Any, Callable, cast4from typing import Any, Callable, Optional, cast56from ._typing import Annotated, get_args, get_origin, get_type_hints7from .models import ArgumentInfo, OptionInfo, ParameterInfo, ParamMeta8
178 unmodified lines
187 params[param.name] = ParamMeta(188 name=param.name, default=default, annotation=annotation189 )190 return params191192193def parse_boolean_env_var(env_var_value: Optional[str], default: bool) -> bool:194 if env_var_value is None:195 return default196 value = env_var_value.lower()197 if value in ("y", "yes", "t", "true", "on", "1"):198 return True199 if value in ("n", "no", "f", "false", "off", "0"):200 return False201 return default