Skip to content

Commit 1edb599

Browse files
bckohansvlandeg
andauthored
πŸ› Fix TYPER_USE_RICH parsing to allow disabling Rich completely (#1539)
Co-authored-by: svlandeg <svlandeg@github.com>
1 parent d1cb863 commit 1edb599

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

β€Žtests/test_rich_markup_mode.pyβ€Ž

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import os
2+
import subprocess
3+
import sys
4+
15
import pytest
26
import typer
37
import typer.completion
@@ -42,6 +46,43 @@ def main(arg: str):
4246
assert any(c in result.stdout for c in rounded)
4347

4448

49+
@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+
)
63+
def 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_result
84+
85+
4586
@pytest.mark.parametrize(
4687
"mode,lines",
4788
[

β€Žtyper/core.pyβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
import click.utils
2424

2525
from ._typing import Literal
26+
from .utils import parse_boolean_env_var
2627

2728
MarkupMode = Literal["markdown", "rich", None]
2829
MARKUP_MODE_KEY = "TYPER_RICH_MARKUP_MODE"
2930

30-
HAS_RICH = os.getenv("TYPER_USE_RICH", True)
31+
HAS_RICH = parse_boolean_env_var(os.getenv("TYPER_USE_RICH"), default=True)
3132

3233
if HAS_RICH:
3334
DEFAULT_MARKUP_MODE: MarkupMode = "rich"
34-
else: # pragma: no cover
35+
else:
3536
DEFAULT_MARKUP_MODE = None
3637

3738

β€Žtyper/utils.pyβ€Ž

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import inspect
22
import sys
33
from copy import copy
4-
from typing import Any, Callable, cast
4+
from typing import Any, Callable, Optional, cast
55

66
from ._typing import Annotated, get_args, get_origin, get_type_hints
77
from .models import ArgumentInfo, OptionInfo, ParameterInfo, ParamMeta
@@ -188,3 +188,14 @@ def get_params_from_function(func: Callable[..., Any]) -> dict[str, ParamMeta]:
188188
name=param.name, default=default, annotation=annotation
189189
)
190190
return params
191+
192+
193+
def parse_boolean_env_var(env_var_value: Optional[str], default: bool) -> bool:
194+
if env_var_value is None:
195+
return default
196+
value = env_var_value.lower()
197+
if value in ("y", "yes", "t", "true", "on", "1"):
198+
return True
199+
if value in ("n", "no", "f", "false", "off", "0"):
200+
return False
201+
return default

0 commit comments

Comments
Β (0)