-
-
Notifications
You must be signed in to change notification settings - Fork 786
✨ Add optional parameter to enable command suggestions #1371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
savannahostrowski
wants to merge
8
commits into
fastapi:master
Choose a base branch
from
savannahostrowski:suggest-on-typo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
342d429
Initial commit for suggestions for typoed commands
savannahostrowski 967dd79
Rename to suggest_commands
savannahostrowski 6877c43
Tidy up
savannahostrowski 3872372
Remove extraneous newlines
savannahostrowski 0dbcda9
Fix test coverage
savannahostrowski c3b00ec
Simplify tests
savannahostrowski b9f505f
Update docs/tutorial/commands/help.md
savannahostrowski 80f199d
Address PR comments
savannahostrowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import typer | ||
|
||
app = typer.Typer(suggest_commands=True) | ||
|
||
|
||
@app.command() | ||
def create(): | ||
typer.echo("Creating...") | ||
|
||
|
||
@app.command() | ||
def delete(): | ||
typer.echo("Deleting...") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import typer | ||
from typer.testing import CliRunner | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_typo_suggestion_disabled_by_default(): | ||
"""Test that typo suggestions are disabled by default""" | ||
app = typer.Typer() | ||
|
||
@app.command() | ||
def create(): # pragma: no cover | ||
typer.echo("Creating...") | ||
|
||
@app.command() | ||
def delete(): # pragma: no cover | ||
typer.echo("Deleting...") | ||
|
||
result = runner.invoke(app, ["crate"]) | ||
assert result.exit_code != 0 | ||
assert "No such command" in result.output | ||
assert "Did you mean" not in result.output | ||
|
||
|
||
def test_typo_suggestion_enabled(): | ||
"""Test that typo suggestions work when enabled""" | ||
app = typer.Typer(suggest_commands=True) | ||
|
||
@app.command() | ||
def create(): # pragma: no cover | ||
typer.echo("Creating...") | ||
|
||
@app.command() | ||
def delete(): # pragma: no cover | ||
typer.echo("Deleting...") | ||
|
||
result = runner.invoke(app, ["crate"]) | ||
assert result.exit_code != 0 | ||
assert "No such command" in result.output | ||
assert "Did you mean 'create'?" in result.output | ||
|
||
|
||
def test_typo_suggestion_multiple_matches(): | ||
"""Test that multiple suggestions are shown when there are multiple close matches""" | ||
app = typer.Typer(suggest_commands=True) | ||
|
||
@app.command() | ||
def create(): # pragma: no cover | ||
typer.echo("Creating...") | ||
|
||
@app.command() | ||
def createnew(): # pragma: no cover | ||
typer.echo("Creating new...") | ||
|
||
result = runner.invoke(app, ["crate"]) | ||
assert result.exit_code != 0 | ||
assert "No such command" in result.output | ||
assert "Did you mean" in result.output | ||
assert "create" in result.output and "createnew" in result.output | ||
|
||
|
||
def test_typo_suggestion_no_matches(): | ||
"""Test that no suggestions are shown when there are no close matches""" | ||
app = typer.Typer(suggest_commands=True) | ||
|
||
@app.command() | ||
def create(): # pragma: no cover | ||
typer.echo("Creating...") | ||
|
||
@app.command() | ||
def delete(): # pragma: no cover | ||
typer.echo("Deleting...") | ||
|
||
result = runner.invoke(app, ["xyz"]) | ||
assert result.exit_code != 0 | ||
assert "No such command" in result.output | ||
assert "Did you mean" not in result.output | ||
|
||
|
||
def test_typo_suggestion_exact_match_works(): | ||
"""Test that exact matches still work normally""" | ||
app = typer.Typer(suggest_commands=True) | ||
|
||
@app.command() | ||
def create(): | ||
typer.echo("Creating...") | ||
|
||
@app.command() | ||
def delete(): | ||
typer.echo("Deleting...") | ||
|
||
result = runner.invoke(app, ["create"]) | ||
assert result.exit_code == 0 | ||
assert "Creating..." in result.output | ||
|
||
result = runner.invoke(app, ["delete"]) | ||
assert result.exit_code == 0 | ||
assert "Deleting..." in result.output | ||
|
||
|
||
def test_typo_suggestion_disabled_explicitly(): | ||
"""Test that typo suggestions can be explicitly disabled""" | ||
app = typer.Typer(suggest_commands=False) | ||
|
||
@app.command() | ||
def create(): # pragma: no cover | ||
typer.echo("Creating...") | ||
|
||
@app.command() | ||
def delete(): # pragma: no cover | ||
typer.echo("Deleting...") | ||
|
||
result = runner.invoke(app, ["crate"]) | ||
assert result.exit_code != 0 | ||
assert "No such command" in result.output | ||
assert "Did you mean" not in result.output | ||
|
||
|
||
def test_typo_suggestion_multiple_similar_commands(): | ||
svlandeg marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
"""Test that multiple similar commands are suggested with quotes around each""" | ||
app = typer.Typer(suggest_commands=True) | ||
|
||
@app.command() | ||
def start(): # pragma: no cover | ||
typer.echo("Starting...") | ||
|
||
@app.command() | ||
def stop(): # pragma: no cover | ||
typer.echo("Stopping...") | ||
|
||
@app.command() | ||
def status(): # pragma: no cover | ||
typer.echo("Status...") | ||
|
||
result = runner.invoke(app, ["sta"]) | ||
assert result.exit_code != 0 | ||
assert "No such command 'sta'" in result.output | ||
assert "Did you mean 'start', 'status'?" in result.output |
24 changes: 24 additions & 0 deletions
24
tests/test_tutorial/test_commands/test_index/test_tutorial005.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typer.testing import CliRunner | ||
|
||
from docs_src.commands.index import tutorial005 as mod | ||
|
||
app = mod.app | ||
runner = CliRunner() | ||
|
||
|
||
def test_creates_successfully(): | ||
"""Verify the example runs without errors""" | ||
result = runner.invoke(app, ["create"]) | ||
assert result.exit_code == 0 | ||
assert "Creating..." in result.output | ||
|
||
result = runner.invoke(app, ["delete"]) | ||
assert result.exit_code == 0 | ||
assert "Deleting..." in result.output | ||
|
||
|
||
def test_shows_suggestion(): | ||
"""Verify command suggestions appear for typos""" | ||
result = runner.invoke(app, ["crate"]) | ||
assert result.exit_code != 0 | ||
assert "Did you mean 'create'?" in result.output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.