Expand ~ in DirectoriesCompleter (fixes #529)#553
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
os.path.dirname("~/") is "~", so os.listdir("~") raised
FileNotFoundError, which was swallowed by the bare except, causing
"~/<TAB>" to yield no completions. Expand the prefix with
os.path.expanduser before splitting and listing so tilde-prefixed
paths are completed.
Fixes kislyuk#529.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
DirectoriesCompleter(and its base_FilteredFilesCompleter) yields nocompletions for a tilde-prefixed path such as
~/, so~/<TAB>shows nothingeven though the home directory has subdirectories. Reported in #529.
The cause is in
_FilteredFilesCompleter.__call__:os.path.dirname("~/")returns
"~", andos.listdir("~")raisesFileNotFoundError(there is noliteral
~directory). That exception is swallowed by the surroundingexcept Exception: return, so the completer silently returns an empty iterator.Fix
Expand the prefix with
os.path.expanduserbefore splitting and listing, sothe filesystem operations run against the real home directory. This mirrors the
behavior of the bash-backed
FilesCompleter, whosecompgenalready expands~. Non-tilde prefixes are unaffected (expanduseris a no-op for them).Tests
Added
test_directory_completion_with_tilde(regression test for #529). Itpoints
HOME/USERPROFILEat a temp directory so the test is hermetic, thenasserts that
~/,~/ab, and~/abc/complete correctly. The test fails onthe unpatched code (empty result) and passes with the fix.
python -m unittest test.test.TestArgcomplete— 36 passedruff check argcomplete test/test.py— cleanruff format --check argcomplete/completers.py test/test.py— cleanmypy --install-types --non-interactive argcomplete— cleanDisclosure: prepared with AI assistance; reviewed and verified locally.