From 0e8093ec5cc574c8055474a239f2867f62b885d4 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Sat, 11 Apr 2020 13:24:27 +0530 Subject: [PATCH 1/3] Type annotations for hash, show and wheel in commands --- ...F3EC962-957A-4DB8-A849-2E7179F875A9.trivial | 0 src/pip/_internal/commands/hash.py | 17 ++++++++++++----- src/pip/_internal/commands/show.py | 18 +++++++++++++----- src/pip/_internal/commands/wheel.py | 14 ++++++++------ 4 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 news/BF3EC962-957A-4DB8-A849-2E7179F875A9.trivial diff --git a/news/BF3EC962-957A-4DB8-A849-2E7179F875A9.trivial b/news/BF3EC962-957A-4DB8-A849-2E7179F875A9.trivial new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_internal/commands/hash.py b/src/pip/_internal/commands/hash.py index f26686156ee..9bb6e9e032a 100644 --- a/src/pip/_internal/commands/hash.py +++ b/src/pip/_internal/commands/hash.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 hashlib @@ -8,9 +5,14 @@ import sys from pip._internal.cli.base_command import Command -from pip._internal.cli.status_codes import ERROR +from pip._internal.cli.status_codes import ERROR, SUCCESS from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES from pip._internal.utils.misc import read_chunks, write_output +from pip._internal.utils.typing import MYPY_CHECK_RUNNING + +if MYPY_CHECK_RUNNING: + from optparse import Values + from typing import Any, List, Dict logger = logging.getLogger(__name__) @@ -27,7 +29,9 @@ class HashCommand(Command): ignore_require_venv = True def __init__(self, *args, **kw): - super(HashCommand, self).__init__(*args, **kw) + # type: (List[Any], Dict[Any, Any]) -> None + # https://github.com/python/mypy/issues/4335 + super(HashCommand, self).__init__(*args, **kw) # type: ignore self.cmd_opts.add_option( '-a', '--algorithm', dest='algorithm', @@ -39,6 +43,7 @@ def __init__(self, *args, **kw): self.parser.insert_option_group(0, self.cmd_opts) def run(self, options, args): + # type: (Values, List[Any]) -> int if not args: self.parser.print_usage(sys.stderr) return ERROR @@ -47,9 +52,11 @@ def run(self, options, args): for path in args: write_output('%s:\n--hash=%s:%s', path, algorithm, _hash_of_file(path, algorithm)) + return SUCCESS def _hash_of_file(path, algorithm): + # type: (str, str) -> str """Return the hash digest of a file.""" with open(path, 'rb') as archive: hash = hashlib.new(algorithm) diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py index a61294ba7bb..436d607391c 100644 --- a/src/pip/_internal/commands/show.py +++ b/src/pip/_internal/commands/show.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 logging @@ -13,6 +10,11 @@ from pip._internal.cli.base_command import Command from pip._internal.cli.status_codes import ERROR, SUCCESS from pip._internal.utils.misc import write_output +from pip._internal.utils.typing import MYPY_CHECK_RUNNING + +if MYPY_CHECK_RUNNING: + from optparse import Values + from typing import Any, List, Dict, Iterator logger = logging.getLogger(__name__) @@ -29,7 +31,9 @@ class ShowCommand(Command): ignore_require_venv = True def __init__(self, *args, **kw): - super(ShowCommand, self).__init__(*args, **kw) + # type: (List[Any], Dict[Any, Any]) -> None + # https://github.com/python/mypy/issues/4335 + super(ShowCommand, self).__init__(*args, **kw) # type: ignore self.cmd_opts.add_option( '-f', '--files', dest='files', @@ -40,6 +44,7 @@ def __init__(self, *args, **kw): self.parser.insert_option_group(0, self.cmd_opts) def run(self, options, args): + # type: (Values, List[Any]) -> int if not args: logger.warning('ERROR: Please provide a package name or names.') return ERROR @@ -53,6 +58,7 @@ def run(self, options, args): def search_packages_info(query): + # type: (List[Any]) -> Iterator[Dict[str, Any]] """ Gather details from installed distributions. Print distribution name, version, location, and installed files. Installed files requires a @@ -71,6 +77,7 @@ def search_packages_info(query): logger.warning('Package(s) not found: %s', ', '.join(missing)) def get_requiring_packages(package_name): + # type: (str) -> List[str] canonical_name = canonicalize_name(package_name) return [ pkg.project_name for pkg in pkg_resources.working_set @@ -88,7 +95,7 @@ def get_requiring_packages(package_name): 'required_by': get_requiring_packages(dist.project_name) } file_list = None - metadata = None + metadata = '' if isinstance(dist, pkg_resources.DistInfoDistribution): # RECORDs should be part of .dist-info metadatas if dist.has_metadata('RECORD'): @@ -141,6 +148,7 @@ def get_requiring_packages(package_name): def print_results(distributions, list_files=False, verbose=False): + # type: (Iterator[Dict[str, Any]], bool, bool) -> bool """ Print the information from installed distributions found. """ diff --git a/src/pip/_internal/commands/wheel.py b/src/pip/_internal/commands/wheel.py index 48f3bfa29ca..bf17d9ac854 100644 --- a/src/pip/_internal/commands/wheel.py +++ b/src/pip/_internal/commands/wheel.py @@ -1,8 +1,5 @@ # -*- coding: utf-8 -*- -# The following comment should be removed at some point in the future. -# mypy: disallow-untyped-defs=False - from __future__ import absolute_import import logging @@ -12,6 +9,7 @@ from pip._internal.cache import WheelCache from pip._internal.cli import cmdoptions from pip._internal.cli.req_command import RequirementCommand, with_cleanup +from pip._internal.cli.status_codes import SUCCESS from pip._internal.exceptions import CommandError from pip._internal.req.req_tracker import get_requirement_tracker from pip._internal.utils.misc import ensure_dir, normalize_path @@ -21,7 +19,7 @@ if MYPY_CHECK_RUNNING: from optparse import Values - from typing import Any, List + from typing import Any, List, Dict logger = logging.getLogger(__name__) @@ -50,7 +48,9 @@ class WheelCommand(RequirementCommand): %prog [options] ...""" def __init__(self, *args, **kw): - super(WheelCommand, self).__init__(*args, **kw) + # type: (List[Any], Dict[Any, Any]) -> None + # https://github.com/python/mypy/issues/4335 + super(WheelCommand, self).__init__(*args, **kw) # type: ignore cmd_opts = self.cmd_opts @@ -112,7 +112,7 @@ def __init__(self, *args, **kw): @with_cleanup def run(self, options, args): - # type: (Values, List[Any]) -> None + # type: (Values, List[Any]) -> int cmdoptions.check_install_build_global(options) session = self.get_default_session(options) @@ -188,3 +188,5 @@ def run(self, options, args): raise CommandError( "Failed to build one or more wheels" ) + + return SUCCESS From 3b35b416e95cffc6695e16801a17f2b8a61f7417 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Tue, 14 Apr 2020 12:06:21 +0530 Subject: [PATCH 2/3] Changed type of variadic args to Any and removed unneeded type ignore --- src/pip/_internal/commands/hash.py | 9 ++++----- src/pip/_internal/commands/show.py | 15 +++++++++------ src/pip/_internal/commands/wheel.py | 9 ++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/pip/_internal/commands/hash.py b/src/pip/_internal/commands/hash.py index 9bb6e9e032a..aab4a3dc2fe 100644 --- a/src/pip/_internal/commands/hash.py +++ b/src/pip/_internal/commands/hash.py @@ -12,7 +12,7 @@ if MYPY_CHECK_RUNNING: from optparse import Values - from typing import Any, List, Dict + from typing import Any, List logger = logging.getLogger(__name__) @@ -29,9 +29,8 @@ class HashCommand(Command): ignore_require_venv = True def __init__(self, *args, **kw): - # type: (List[Any], Dict[Any, Any]) -> None - # https://github.com/python/mypy/issues/4335 - super(HashCommand, self).__init__(*args, **kw) # type: ignore + # type: (*Any, **Any) -> None + super(HashCommand, self).__init__(*args, **kw) self.cmd_opts.add_option( '-a', '--algorithm', dest='algorithm', @@ -43,7 +42,7 @@ def __init__(self, *args, **kw): self.parser.insert_option_group(0, self.cmd_opts) def run(self, options, args): - # type: (Values, List[Any]) -> int + # type: (Values, List[str]) -> int if not args: self.parser.print_usage(sys.stderr) return ERROR diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py index 436d607391c..f7522df9088 100644 --- a/src/pip/_internal/commands/show.py +++ b/src/pip/_internal/commands/show.py @@ -31,9 +31,8 @@ class ShowCommand(Command): ignore_require_venv = True def __init__(self, *args, **kw): - # type: (List[Any], Dict[Any, Any]) -> None - # https://github.com/python/mypy/issues/4335 - super(ShowCommand, self).__init__(*args, **kw) # type: ignore + # type: (*Any, **Any) -> None + super(ShowCommand, self).__init__(*args, **kw) self.cmd_opts.add_option( '-f', '--files', dest='files', @@ -44,7 +43,7 @@ def __init__(self, *args, **kw): self.parser.insert_option_group(0, self.cmd_opts) def run(self, options, args): - # type: (Values, List[Any]) -> int + # type: (Values, List[str]) -> int if not args: logger.warning('ERROR: Please provide a package name or names.') return ERROR @@ -58,7 +57,7 @@ def run(self, options, args): def search_packages_info(query): - # type: (List[Any]) -> Iterator[Dict[str, Any]] + # type: (List[str]) -> Iterator[Dict[str, str]] """ Gather details from installed distributions. Print distribution name, version, location, and installed files. Installed files requires a @@ -95,6 +94,10 @@ def get_requiring_packages(package_name): 'required_by': get_requiring_packages(dist.project_name) } file_list = None + # Set metadata to empty string to avoid metadata being typed as + # Optional[Any] in function calls using metadata below + # and since dist.get_metadata returns us a str, the default + # value of empty string should be valid metadata = '' if isinstance(dist, pkg_resources.DistInfoDistribution): # RECORDs should be part of .dist-info metadatas @@ -148,7 +151,7 @@ def get_requiring_packages(package_name): def print_results(distributions, list_files=False, verbose=False): - # type: (Iterator[Dict[str, Any]], bool, bool) -> bool + # type: (Iterator[Dict[str, str]], bool, bool) -> bool """ Print the information from installed distributions found. """ diff --git a/src/pip/_internal/commands/wheel.py b/src/pip/_internal/commands/wheel.py index bf17d9ac854..f028d681f7b 100644 --- a/src/pip/_internal/commands/wheel.py +++ b/src/pip/_internal/commands/wheel.py @@ -19,7 +19,7 @@ if MYPY_CHECK_RUNNING: from optparse import Values - from typing import Any, List, Dict + from typing import Any, List logger = logging.getLogger(__name__) @@ -48,9 +48,8 @@ class WheelCommand(RequirementCommand): %prog [options] ...""" def __init__(self, *args, **kw): - # type: (List[Any], Dict[Any, Any]) -> None - # https://github.com/python/mypy/issues/4335 - super(WheelCommand, self).__init__(*args, **kw) # type: ignore + # type: (*Any, **Any) -> None + super(WheelCommand, self).__init__(*args, **kw) cmd_opts = self.cmd_opts @@ -112,7 +111,7 @@ def __init__(self, *args, **kw): @with_cleanup def run(self, options, args): - # type: (Values, List[Any]) -> int + # type: (Values, List[str]) -> int cmdoptions.check_install_build_global(options) session = self.get_default_session(options) From 8ce6b88077dafc49ba030379aec609fb60e8b0ec Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Sun, 17 May 2020 15:08:19 +0530 Subject: [PATCH 3/3] Remove comment addressing metadata initial value --- src/pip/_internal/commands/show.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/pip/_internal/commands/show.py b/src/pip/_internal/commands/show.py index f7522df9088..8d9fa13fd91 100644 --- a/src/pip/_internal/commands/show.py +++ b/src/pip/_internal/commands/show.py @@ -94,10 +94,6 @@ def get_requiring_packages(package_name): 'required_by': get_requiring_packages(dist.project_name) } file_list = None - # Set metadata to empty string to avoid metadata being typed as - # Optional[Any] in function calls using metadata below - # and since dist.get_metadata returns us a str, the default - # value of empty string should be valid metadata = '' if isinstance(dist, pkg_resources.DistInfoDistribution): # RECORDs should be part of .dist-info metadatas