Skip to content

Commit b38859d

Browse files
fix(cli): keep the extras name in the rl registration warning
The warning quotes a literal 'dashscope[rl]', which rich reads as a style tag and drops — users were told to run `pip install 'dashscope'`, which installs none of the extras the message is about. Escape the exception text. Also drop the duplicated instruction: the reinforcement package's ImportError already says how to install, so the CLI appended a second copy. The hint is now added only when the underlying message lacks one.
1 parent 514f6be commit b38859d

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎dashscope/cli/__init__.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
)
1919

2020
import typer # noqa: E402
21+
from rich.markup import escape # noqa: E402
2122

2223
import dashscope # noqa: E402
2324
from dashscope.cli.common import err_console # noqa: E402
@@ -437,16 +438,21 @@ def _register_rl_app():
437438
hidden=True,
438439
)
439440
except ImportError as exception:
441+
# The message quotes a literal 'dashscope[rl]'; unescaped, rich reads
442+
# [rl] as a style tag and prints an install command with no extras.
443+
detail = escape(str(exception))
444+
if "pip install" not in detail:
445+
detail += (
446+
". Install the optional dependencies with: "
447+
"[bold]pip install 'dashscope\\[rl]'[/bold]"
448+
)
440449
err_console.print(
441-
"[yellow]Warning:[/yellow] Failed to register rl command: "
442-
f"{exception}. "
443-
"Install the optional dependencies with: "
444-
"[bold]pip install 'dashscope[rl]'[/bold]",
450+
"[yellow]Warning:[/yellow] Failed to register rl command: " f"{detail}",
445451
)
446452
except Exception as exception:
447453
err_console.print(
448454
"[yellow]Warning:[/yellow] Failed to register rl command: "
449-
f"{exception}",
455+
f"{escape(str(exception))}",
450456
)
451457

452458

‎tests/unit/test_cli_main.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ def strip_ansi_codes(text):
2525
return ansi_escape.sub("", text)
2626

2727

28+
class _ImportErrorOnApp:
29+
"""Stand-in for a module whose ``from X import app`` raises ImportError.
30+
31+
Lets the rl-registration warning be exercised whether or not the
32+
optional dependencies happen to be installed in this environment.
33+
"""
34+
35+
def __init__(self, message):
36+
self._message = message
37+
38+
@property
39+
def app(self):
40+
raise ImportError(self._message)
41+
42+
2843
# pylint: disable=too-many-public-methods
2944
class TestCliMain:
3045
def test_main_prints_authentication_error_without_traceback(
@@ -245,6 +260,42 @@ def test_agentic_rl_hidden_alias_help(self):
245260
assert result.exit_code == 0
246261
assert "register_functions" in result.output
247262

263+
def test_rl_warning_keeps_the_extras_name(self, monkeypatch, capsys):
264+
# rich reads a literal [rl] as a style tag and drops it, which used to
265+
# print `pip install 'dashscope'` — a command that installs none of
266+
# the extras the warning is about.
267+
monkeypatch.setitem(
268+
sys.modules,
269+
"dashscope.cli.agentic_rl",
270+
_ImportErrorOnApp(
271+
"Agentic RL fine-tuning needs optional dependencies. "
272+
"Install them with: pip install 'dashscope[rl]'",
273+
),
274+
)
275+
276+
dashscope.cli._register_rl_app()
277+
278+
err = " ".join(capsys.readouterr().err.split())
279+
assert "pip install 'dashscope[rl]'" in err
280+
assert err.count("Install") == 1
281+
282+
def test_rl_warning_adds_a_hint_when_the_error_has_none(
283+
self,
284+
monkeypatch,
285+
capsys,
286+
):
287+
monkeypatch.setitem(
288+
sys.modules,
289+
"dashscope.cli.agentic_rl",
290+
_ImportErrorOnApp("No module named 'typer_x'"),
291+
)
292+
293+
dashscope.cli._register_rl_app()
294+
295+
err = " ".join(capsys.readouterr().err.split())
296+
assert "No module named 'typer_x'" in err
297+
assert "pip install 'dashscope[rl]'" in err
298+
248299
def test_subcommand_api_key_option_is_not_consumed_by_global_parser(
249300
self,
250301
monkeypatch,

0 commit comments

Comments
 (0)