-
Notifications
You must be signed in to change notification settings - Fork 91
fix: Use Click v8.x native shell completion #2639
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
matthewfeickert
wants to merge
2
commits into
scikit-hep:main
Choose a base branch
from
matthewfeickert:fix/use-click-8-shell-completions
base: main
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||||
|---|---|---|---|---|---|---|
| @@ -1,30 +1,62 @@ | ||||||
| '''Shell completions for pyhf.''' | ||||||
| """Shell completions for pyhf.""" | ||||||
|
|
||||||
| import click | ||||||
|
|
||||||
| try: | ||||||
| import click_completion | ||||||
|
|
||||||
| click_completion.init() | ||||||
| @click.command(help="Generate shell completion code.", name="completions") | ||||||
| @click.argument( | ||||||
| "shell", | ||||||
| required=False, | ||||||
| type=click.Choice(["bash", "zsh", "fish"], case_sensitive=False), | ||||||
| ) | ||||||
| def cli(shell): | ||||||
| """Generate shell completion code for various shells. | ||||||
| @click.command(help='Generate shell completion code.', name='completions') | ||||||
| @click.argument( | ||||||
| 'shell', | ||||||
| required=False, | ||||||
| type=click_completion.DocumentedChoice(click_completion.core.shells), | ||||||
| ) | ||||||
| def cli(shell): | ||||||
| '''Generate shell completion code for various shells.''' | ||||||
| click.echo(click_completion.core.get_code(shell, prog_name='pyhf')) | ||||||
|
|
||||||
| except ImportError: | ||||||
|
|
||||||
| @click.command(help='Generate shell completion code.', name='completions') | ||||||
| @click.argument('shell', default=None) | ||||||
| def cli(shell): | ||||||
| """Placeholder for shell completion code generatioon function if necessary dependency is missing.""" | ||||||
| click.secho( | ||||||
| "This requires the click_completion module.\n" | ||||||
| "You can install it with the shellcomplete extra:\n" | ||||||
| "python -m pip install 'pyhf[shellcomplete]'" | ||||||
| Supported shells: bash, zsh, fish | ||||||
| To enable completion, run the appropriate command for your shell: | ||||||
| \b | ||||||
| Bash: | ||||||
| mkdir -p ~/.completions | ||||||
| _PYHF_COMPLETE=bash_source pyhf > ~/.completions/pyhf-complete.sh | ||||||
| echo -e "\n. ~/.completions/pyhf-complete.sh" >> ~/.bashrc | ||||||
| \b | ||||||
| Zsh: | ||||||
| mkdir -p ~/.completions | ||||||
| _PYHF_COMPLETE=zsh_source pyhf > ~/.completions/pyhf-complete.zsh | ||||||
| echo -e "\n. ~/.completions/pyhf-complete.zsh" >> ~/.zshrc | ||||||
| \b | ||||||
| Fish: | ||||||
| _PYHF_COMPLETE=fish_source pyhf >> ~/.config/fish/completions/pyhf.fish | ||||||
| """ | ||||||
| if shell is None: | ||||||
| click.echo(cli.get_help(click.Context(cli))) | ||||||
| return | ||||||
|
|
||||||
| click.echo(f"To enable {shell} completion for pyhf run in your {shell} shell:\n") | ||||||
|
|
||||||
| instructions = { | ||||||
| "bash": ( | ||||||
| "mkdir -p ~/.completions\n" | ||||||
| "_PYHF_COMPLETE=bash_source pyhf > ~/.completions/pyhf-complete.sh\n" | ||||||
| 'echo -e "\\n. ~/.completions/pyhf-complete.sh" >> ~/.bashrc\n' | ||||||
| ), | ||||||
| "zsh": ( | ||||||
| "mkdir -p ~/.completions\n" | ||||||
| "_PYHF_COMPLETE=zsh_source pyhf > ~/.completions/pyhf-complete.zsh\n" | ||||||
| 'echo -e "\\n. ~/.completions/pyhf-complete.zsh" >> ~/.zshrc\n' | ||||||
| ), | ||||||
| "fish": ( | ||||||
| "_PYHF_COMPLETE=fish_source pyhf >> ~/.config/fish/completions/pyhf.fish\n" | ||||||
| ), | ||||||
| } | ||||||
| click.echo( | ||||||
| click.style( | ||||||
| instructions[shell], | ||||||
| bold=True, | ||||||
| ) | ||||||
| ) | ||||||
| click.echo("and then source your shell configuration or restart your shell.") | ||||||
|
||||||
| click.echo("and then source your shell configuration or restart your shell.") | |
| click.echo("And then source your shell configuration or restart your shell.") |
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 |
|---|---|---|
| @@ -1,21 +1,40 @@ | ||
| from click.testing import CliRunner | ||
| import sys | ||
| import importlib | ||
|
|
||
|
|
||
| def test_shllcomplete_cli(isolate_modules): | ||
| def test_shell_completion_cli_bash(): | ||
| from pyhf.cli.complete import cli | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ['bash']) | ||
| assert 'complete -F _pyhf_completion -o default pyhf' in result.output | ||
| result = runner.invoke(cli, ["bash"]) | ||
| assert result.exit_code == 0 | ||
| assert "_PYHF_COMPLETE=bash_source" in result.output | ||
| assert ".bashrc" in result.output | ||
|
|
||
|
|
||
| def test_shllcomplete_cli_missing_extra(isolate_modules): | ||
| sys.modules['click_completion'] = None | ||
| importlib.reload(sys.modules['pyhf.cli.complete']) | ||
| def test_shell_completion_cli_zsh(): | ||
| from pyhf.cli.complete import cli | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ['bash']) | ||
| assert 'You can install it with the shellcomplete extra' in result.output | ||
| result = runner.invoke(cli, ["zsh"]) | ||
| assert result.exit_code == 0 | ||
| assert "_PYHF_COMPLETE=zsh_source" in result.output | ||
| assert ".zshrc" in result.output | ||
|
|
||
|
|
||
| def test_shell_completion_cli_fish(): | ||
| from pyhf.cli.complete import cli | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ["fish"]) | ||
| assert result.exit_code == 0 | ||
| assert "_PYHF_COMPLETE=fish_source" in result.output | ||
| assert "fish/completions" in result.output | ||
|
|
||
|
|
||
| def test_shell_completion_cli_no_shell(): | ||
| from pyhf.cli.complete import cli | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke(cli) | ||
| assert result.exit_code == 0 | ||
| assert "Generate shell completion code" in result.output |
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.
The shell completion instructions are duplicated in both the docstring (lines 20-33) and the
instructionsdictionary (lines 42-55). This creates a maintenance burden where updates must be made in two places. Consider removing the detailed instructions from the docstring and keeping only the high-level description, or generate the help text dynamically from theinstructionsdictionary.