From ae8391b4b76c641003aa0c39c1dc632a8759c063 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Wed, 15 Apr 2020 01:11:57 +0530 Subject: [PATCH 1/5] Type annotations for completion and debug in commands --- news/C7A26013-0E79-4DBB-B0E3-2DA5C5587CDC.trivial | 0 src/pip/_internal/commands/completion.py | 13 ++++++++++--- src/pip/_internal/commands/debug.py | 7 ++----- 3 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 news/C7A26013-0E79-4DBB-B0E3-2DA5C5587CDC.trivial diff --git a/news/C7A26013-0E79-4DBB-B0E3-2DA5C5587CDC.trivial b/news/C7A26013-0E79-4DBB-B0E3-2DA5C5587CDC.trivial new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_internal/commands/completion.py b/src/pip/_internal/commands/completion.py index 910fcbfe358..66448bbf695 100644 --- a/src/pip/_internal/commands/completion.py +++ b/src/pip/_internal/commands/completion.py @@ -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 @@ -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 @@ -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 """Prints the completion code of the given shell""" shells = COMPLETION_SCRIPTS.keys() shell_options = ['--' + shell for shell in sorted(shells)] @@ -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 diff --git a/src/pip/_internal/commands/debug.py b/src/pip/_internal/commands/debug.py index 05ff1c54e64..1a56db2fb19 100644 --- a/src/pip/_internal/commands/debug.py +++ b/src/pip/_internal/commands/debug.py @@ -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 @@ -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. @@ -85,7 +81,6 @@ def get_module_from_module_name(module_name): def get_vendor_version_from_module(module_name): # type: (str) -> str - module = get_module_from_module_name(module_name) version = getattr(module, '__version__', None) @@ -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]) @@ -197,6 +193,7 @@ class DebugCommand(Command): ignore_require_venv = True def __init__(self, *args, **kw): + # type: (*Any, **Any) -> None super(DebugCommand, self).__init__(*args, **kw) cmd_opts = self.cmd_opts From fc6a3e203f7ac3e10fc4c35bae0a8038ce19d8af Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Wed, 15 Apr 2020 02:11:14 +0530 Subject: [PATCH 2/5] Added returncode check in completion tests --- tests/functional/test_completion.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/functional/test_completion.py b/tests/functional/test_completion.py index a3986811b6f..e00f0b03774 100644 --- a/tests/functional/test_completion.py +++ b/tests/functional/test_completion.py @@ -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) 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): @@ -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 From 35ea5a6d5f0d6b79d132e4a3ec3c37cc5c767aeb Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Mon, 20 Apr 2020 03:19:43 +0530 Subject: [PATCH 3/5] Remove Any type from run function --- src/pip/_internal/commands/completion.py | 2 +- src/pip/_internal/commands/debug.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pip/_internal/commands/completion.py b/src/pip/_internal/commands/completion.py index 66448bbf695..c62ce7b3ba3 100644 --- a/src/pip/_internal/commands/completion.py +++ b/src/pip/_internal/commands/completion.py @@ -84,7 +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 + # type: (Values, List[str]) -> int """Prints the completion code of the given shell""" shells = COMPLETION_SCRIPTS.keys() shell_options = ['--' + shell for shell in sorted(shells)] diff --git a/src/pip/_internal/commands/debug.py b/src/pip/_internal/commands/debug.py index 1a56db2fb19..341583ea7cf 100644 --- a/src/pip/_internal/commands/debug.py +++ b/src/pip/_internal/commands/debug.py @@ -202,7 +202,7 @@ def __init__(self, *args, **kw): self.parser.config.load() def run(self, options, args): - # type: (Values, List[Any]) -> int + # type: (Values, List[str]) -> int logger.warning( "This command is only meant for debugging. " "Do not use this with automation for parsing and getting these " From 78c0a7192aae3cc61060ca91e4276626bdc6cd86 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Fri, 15 May 2020 16:55:58 +0530 Subject: [PATCH 4/5] Change return type annotation for commands.debug.get_vendor_version_from_module --- src/pip/_internal/commands/debug.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/commands/debug.py b/src/pip/_internal/commands/debug.py index 341583ea7cf..f8d5817c032 100644 --- a/src/pip/_internal/commands/debug.py +++ b/src/pip/_internal/commands/debug.py @@ -80,7 +80,7 @@ def get_module_from_module_name(module_name): def get_vendor_version_from_module(module_name): - # type: (str) -> str + # type: (str) -> Optional[str] module = get_module_from_module_name(module_name) version = getattr(module, '__version__', None) From c83c2804caef4460094bad20b8ccf287dff58d36 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Fri, 15 May 2020 16:57:31 +0530 Subject: [PATCH 5/5] Always return SUCCESS from commands.completion.run --- src/pip/_internal/commands/completion.py | 4 ++-- tests/functional/test_completion.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/pip/_internal/commands/completion.py b/src/pip/_internal/commands/completion.py index c62ce7b3ba3..70d33243fcd 100644 --- a/src/pip/_internal/commands/completion.py +++ b/src/pip/_internal/commands/completion.py @@ -4,7 +4,7 @@ import textwrap from pip._internal.cli.base_command import Command -from pip._internal.cli.status_codes import ERROR, SUCCESS +from pip._internal.cli.status_codes import SUCCESS from pip._internal.utils.misc import get_prog from pip._internal.utils.typing import MYPY_CHECK_RUNNING @@ -99,4 +99,4 @@ def run(self, options, args): sys.stderr.write( 'ERROR: You must pass {}\n' .format(' or '.join(shell_options)) ) - return ERROR + return SUCCESS diff --git a/tests/functional/test_completion.py b/tests/functional/test_completion.py index e00f0b03774..a3986811b6f 100644 --- a/tests/functional/test_completion.py +++ b/tests/functional/test_completion.py @@ -107,10 +107,9 @@ def test_completion_alone(autocomplete_script): """ Test getting completion for none shell, just pip completion """ - result = autocomplete_script.pip('completion', expect_error=True) + result = autocomplete_script.pip('completion', allow_stderr_error=True) 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): @@ -315,4 +314,3 @@ 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