Skip to content

Commit 616e8f0

Browse files
committed
Fix suggestions exposing hidden commands
1 parent 60af34b commit 616e8f0

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

tests/test_suggest_commands.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import pytest
12
import typer
3+
import typer.core
24
from typer.testing import CliRunner
35

6+
from tests.utils import needs_rich
7+
48
runner = CliRunner()
59

610

@@ -96,3 +100,35 @@ def delete(): # pragma: no cover
96100
assert result.exit_code != 0
97101
assert "No such command" in result.output
98102
assert "Did you mean" not in result.output
103+
104+
105+
@pytest.mark.parametrize(
106+
"use_rich",
107+
[
108+
pytest.param(False),
109+
pytest.param(True, marks=needs_rich),
110+
],
111+
)
112+
def test_typo_suggestion_excludes_hidden_commands(
113+
monkeypatch: pytest.MonkeyPatch, use_rich: bool
114+
) -> None:
115+
monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)
116+
app = typer.Typer()
117+
118+
@app.command()
119+
def secret_agent(): # pragma: no cover
120+
typer.echo("Visible command")
121+
122+
@app.command(hidden=True)
123+
def secret_admin():
124+
typer.echo("Hidden command")
125+
126+
result = runner.invoke(app, ["secret-admi"])
127+
assert result.exit_code != 0
128+
assert "No such command" in result.output
129+
assert "'secret-agent'" in result.output
130+
assert "'secret-admin'" not in result.output
131+
132+
result = runner.invoke(app, ["secret-admin"])
133+
assert result.exit_code == 0
134+
assert "Hidden command" in result.output

typer/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,11 @@ def resolve_command(
11811181
return self._click_resolve_command(ctx, args)
11821182
except _click.exceptions.UsageError as e:
11831183
if self.suggest_commands:
1184-
available_commands = list(self.commands.keys())
1184+
available_commands = [
1185+
name
1186+
for name, command in self.commands.items()
1187+
if not command.hidden
1188+
]
11851189
if available_commands and args:
11861190
typo = args[0]
11871191
matches = get_close_matches(typo, available_commands)

0 commit comments

Comments
 (0)