🐛 Ensure that autocompletion works for Path
arguments/options
#1138
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.
Resolves #951
Issue
The issue on
master
is that parameters (either anArgument
or anOption
) specifically typed asPath
wouldn't be automatically completed on the commandline. For instance, having two filestest_1.py
andtest_2.py
in a directory, and typing"t"
for an option typed asPath
, 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
orOption
asstr
, ironically then the autocompletion would actually work out of the box. Things go haywire (IMO) in theget_click_type
function where the parameter is converted to the correspondingclick.Path
when a type annotation ofpathlib.Path
is found. Thisclick.Path
class has a specificshell_complete
function that: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 😁