Skip to content

Commit

Permalink
Fix error when autocompleting after flag
Browse files Browse the repository at this point in the history
When running autocomplete after any command-line option with unspecified
'metavar' attribute, a traceback for an 'AttributeError' would be
displayed. This is fixed for first checking if 'metavar' was defined.
Fixes pypa#5751
  • Loading branch information
EpicWink committed Sep 17, 2018
1 parent f0ab8df commit 10911fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pip/_internal/cli/autocompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def get_path_completion_type(cwords, cword, opts):
continue
for o in str(opt).split('/'):
if cwords[cword - 2].split('=')[0] == o:
if any(x in ('path', 'file', 'dir')
if not opt.metavar or any(
x in ('path', 'file', 'dir')
for x in opt.metavar.split('/')):
return opt.metavar

Expand Down
24 changes: 24 additions & 0 deletions tests/functional/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ def test_completion_not_files_after_option(script, data):
)


@pytest.mark.parametrize("cl_opts", ["-U", "--user", "-h"])
def test_completion_not_files_after_nonexpecting_option(script, data, cl_opts):
"""
Test not getting completion files after options which not applicable
(e.g. ``pip install``)
"""
res, env = setup_completion(
script=script,
words=('pip install %s r' % cl_opts),
cword='2',
cwd=data.completion_paths,
)
assert not any(out in res.stdout for out in
('requirements.txt', 'readme.txt',)), (
"autocomplete function completed <file> when "
"it should not complete"
)
assert not any(os.path.join(out, '') in res.stdout
for out in ('replay', 'resources')), (
"autocomplete function completed <dir> when "
"it should not complete"
)


def test_completion_directories_after_option(script, data):
"""
Test getting completion <dir> after options in command
Expand Down

0 comments on commit 10911fe

Please sign in to comment.