-
-
Notifications
You must be signed in to change notification settings - Fork 672
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
✨ Show help items in order of definition #944
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ece1111
commit failing test
svlandeg a88eb3b
add list_commands function to TyperGroup to avoid sorting
svlandeg 96e9ad7
add order explanation to the docs
svlandeg 7393626
fix typing
svlandeg c7cccd7
preliminary 'fix' for unit tests (WIP)
svlandeg 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 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 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() | ||
|
||
|
||
@app.command() | ||
def delete(): | ||
print("Deleting user: Hiro Hamada") | ||
|
||
|
||
@app.command() | ||
def create(): | ||
print("Creating user: Hiro Hamada") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
This file contains 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 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
45 changes: 45 additions & 0 deletions
45
tests/test_tutorial/test_commands/test_index/test_tutorial004.py
This file contains 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,45 @@ | ||
import subprocess | ||
import sys | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.commands.index import tutorial004 as mod | ||
|
||
app = mod.app | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_help(): | ||
result = runner.invoke(app, ["--help"]) | ||
assert result.exit_code == 0 | ||
assert "[OPTIONS] COMMAND [ARGS]..." in result.output | ||
print(result.output) | ||
assert "Commands" in result.output | ||
assert "create" in result.output | ||
assert "delete" in result.output | ||
# Test that the 'delete' command precedes the 'create' command in the help output | ||
create_char = result.output.index("create") | ||
delete_char = result.output.index("delete") | ||
assert delete_char < create_char | ||
|
||
|
||
def test_create(): | ||
result = runner.invoke(app, ["create"]) | ||
assert result.exit_code == 0 | ||
assert "Creating user: Hiro Hamada" in result.output | ||
|
||
|
||
def test_delete(): | ||
result = runner.invoke(app, ["delete"]) | ||
assert result.exit_code == 0 | ||
assert "Deleting user: Hiro Hamada" in result.output | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
capture_output=True, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
This file contains 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 |
---|---|---|
|
@@ -738,3 +738,9 @@ def format_help(self, ctx: click.Context, formatter: click.HelpFormatter) -> Non | |
ctx=ctx, | ||
markup_mode=self.rich_markup_mode, | ||
) | ||
|
||
def list_commands(self, ctx: click.Context) -> List[str]: | ||
"""Returns a list of subcommand names. | ||
Note that in Click's Group class, these are sorted. | ||
In Typer, we wish to maintain the original order of creation (cf Issue #933)""" | ||
return [n for n, c in self.commands.items()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that we can't just
here as |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: if we keep the current behavour of command/group sorting, then we probably want to comment on that in the docs as well, because otherwise it may be confusing. I can add this after we decide how to move forward.