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

Type annotations for completion and debug in commands #8051

Merged
merged 5 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
13 changes: 10 additions & 3 deletions src/pip/_internal/commands/completion.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False

from __future__ import absolute_import

import sys
import textwrap

from pip._internal.cli.base_command import Command
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.utils.misc import get_prog
from pip._internal.utils.typing import MYPY_CHECK_RUNNING

if MYPY_CHECK_RUNNING:
from typing import Any, List
from optparse import Values

BASE_COMPLETION = """
# pip {shell} completion start{script}# pip {shell} completion end
Expand Down Expand Up @@ -54,6 +57,7 @@ class CompletionCommand(Command):
ignore_require_venv = True

def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
super(CompletionCommand, self).__init__(*args, **kw)

cmd_opts = self.cmd_opts
Expand All @@ -80,6 +84,7 @@ def __init__(self, *args, **kw):
self.parser.insert_option_group(0, cmd_opts)

def run(self, options, args):
# type: (Values, List[Any]) -> int
deveshks marked this conversation as resolved.
Show resolved Hide resolved
"""Prints the completion code of the given shell"""
shells = COMPLETION_SCRIPTS.keys()
shell_options = ['--' + shell for shell in sorted(shells)]
Expand All @@ -89,7 +94,9 @@ def run(self, options, args):
prog=get_prog())
)
print(BASE_COMPLETION.format(script=script, shell=options.shell))
return SUCCESS
else:
sys.stderr.write(
'ERROR: You must pass {}\n' .format(' or '.join(shell_options))
)
return ERROR
deveshks marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 2 additions & 5 deletions src/pip/_internal/commands/debug.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# The following comment should be removed at some point in the future.
# mypy: disallow-untyped-defs=False

from __future__ import absolute_import

import locale
Expand Down Expand Up @@ -67,7 +64,6 @@ def create_vendor_txt_map():

def get_module_from_module_name(module_name):
# type: (str) -> ModuleType

# Module name can be uppercase in vendor.txt for some reason...
module_name = module_name.lower()
# PATCH: setuptools is actually only pkg_resources.
Expand All @@ -85,7 +81,6 @@ def get_module_from_module_name(module_name):

def get_vendor_version_from_module(module_name):
# type: (str) -> str
deveshks marked this conversation as resolved.
Show resolved Hide resolved

module = get_module_from_module_name(module_name)
version = getattr(module, '__version__', None)

Expand Down Expand Up @@ -169,6 +164,7 @@ def show_tags(options):


def ca_bundle_info(config):
# type: (Dict[str, str]) -> str
levels = set()
for key, value in config.items():
levels.add(key.split('.')[0])
Expand Down Expand Up @@ -197,6 +193,7 @@ class DebugCommand(Command):
ignore_require_venv = True

def __init__(self, *args, **kw):
# type: (*Any, **Any) -> None
deveshks marked this conversation as resolved.
Show resolved Hide resolved
super(DebugCommand, self).__init__(*args, **kw)

cmd_opts = self.cmd_opts
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ def test_completion_alone(autocomplete_script):
"""
Test getting completion for none shell, just pip completion
"""
result = autocomplete_script.pip('completion', allow_stderr_error=True)
result = autocomplete_script.pip('completion', expect_error=True)
deveshks marked this conversation as resolved.
Show resolved Hide resolved
assert 'ERROR: You must pass --bash or --fish or --zsh' in result.stderr, \
'completion alone failed -- ' + result.stderr
assert result.returncode == 1


def test_completion_for_un_snippet(autocomplete):
Expand Down Expand Up @@ -314,3 +315,4 @@ def test_completion_uses_same_executable_name(
executable_name, 'completion', flag, expect_stderr=deprecated_python,
)
assert executable_name in result.stdout
assert result.returncode == 0