Skip to content

Expand ~ in DirectoriesCompleter (fixes #529)#553

Open
apoorvdarshan wants to merge 1 commit into
kislyuk:mainfrom
apoorvdarshan:fix-directories-completer-tilde-529
Open

Expand ~ in DirectoriesCompleter (fixes #529)#553
apoorvdarshan wants to merge 1 commit into
kislyuk:mainfrom
apoorvdarshan:fix-directories-completer-tilde-529

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Summary

DirectoriesCompleter (and its base _FilteredFilesCompleter) yields no
completions for a tilde-prefixed path such as ~/, so ~/<TAB> shows nothing
even though the home directory has subdirectories. Reported in #529.

The cause is in _FilteredFilesCompleter.__call__: os.path.dirname("~/")
returns "~", and os.listdir("~") raises FileNotFoundError (there is no
literal ~ directory). That exception is swallowed by the surrounding
except Exception: return, so the completer silently returns an empty iterator.

Fix

Expand the prefix with os.path.expanduser before splitting and listing, so
the filesystem operations run against the real home directory. This mirrors the
behavior of the bash-backed FilesCompleter, whose compgen already expands
~. Non-tilde prefixes are unaffected (expanduser is a no-op for them).

expanded_prefix = os.path.expanduser(prefix)
target_dir = os.path.dirname(expanded_prefix)
...
incomplete_part = os.path.basename(expanded_prefix)

Tests

Added test_directory_completion_with_tilde (regression test for #529). It
points HOME/USERPROFILE at a temp directory so the test is hermetic, then
asserts that ~/, ~/ab, and ~/abc/ complete correctly. The test fails on
the unpatched code (empty result) and passes with the fix.

  • python -m unittest test.test.TestArgcomplete — 36 passed
  • ruff check argcomplete test/test.py — clean
  • ruff format --check argcomplete/completers.py test/test.py — clean
  • mypy --install-types --non-interactive argcomplete — clean

Disclosure: prepared with AI assistance; reviewed and verified locally.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant