Skip to content
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

🐛 Ensure that autocompletion works for Path arguments/options #1138

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

svlandeg
Copy link
Member

Resolves #951

Issue

The issue on master is that parameters (either an Argument or an Option) specifically typed as Path wouldn't be automatically completed on the commandline. For instance, having two files test_1.py and test_2.py in a directory, and typing "t" for an option typed as Path, would simply put a space after the "t", not autocompleting at all.

The desired behaviour instead would be to first autocomplete the "test_" part, and then after another TAB show the two options "test_1.py" and "test_2.py".

Proposed solution

After quite a bit of digging and reading @tiangolo's elaborate background information on issue #951, I've come to the conclusion that this unexpected behaviour is likely caused by an unwanted and inconsistent interplay between Typer and Click.

Stepping back for a second - if you would type the Argument or Option as str, ironically then the autocompletion would actually work out of the box. Things go haywire (IMO) in the get_click_type function where the parameter is converted to the corresponding click.Path when a type annotation of pathlib.Path is found. This click.Path class has a specific shell_complete function that:

Return a special completion marker that tells the completion system to use the shell to provide path completions for only directories or any paths.

But Typer's autocompletion functionality doesn't use that marker like Click's scripts (e.g. the Bash one) does. If instead we return the empty list (i.e. the default behaviour of click.ParamType), then it all seems to work again.

Testing

I could confirm the original bug as described in #951 on Bash and Zsh, and I could confirm that the autocompletion functionality works again with Path parameters with this code change.

If this is the route forward we want to take, I'll test on more systems.

However, I'm entirely unsure how to actually test this in the test suite, given the dependency on the actual console for completion.

Open questions

Not sure whether this is the kind of fix/resolution that @tiangolo has in mind, so I'll writing this up to solicit feedback. Also the naming of "TyperPath" is probably not ideal - open to suggestions 😁

@svlandeg svlandeg added the bug Something isn't working label Jan 30, 2025
@svlandeg svlandeg self-assigned this Jan 30, 2025
@svlandeg
Copy link
Member Author

The coverage test is failing, because there are no autocompletion tests with Path parameters. I can add one, but I'd rather add one that actually exhibits the wrong behaviour on master, and this is tricky... 🤔

@bckohan
Copy link
Contributor

bckohan commented Feb 3, 2025

You might have a look at how I do this in django-typer.

I run the shells in a pseudo terminal subprocess. This allows me to test end-to-end like you want here. There are a few downsides though:

1). I haven't been able to get this to work with fish yet (when I run it in a pty it decides its not an interactive shell and terminates prematurely - I'm sure theres a way around it I just havent found it yet). It does work with powershell, bash and zsh.
2) There doesn't seem to be a way to avoid sleeps to handle timing issues. This can make the tests take a bit of time if you run a lot of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autocompletion for Path
2 participants