Skip to content

Commit 6ab9455

Browse files
committed
Type annotations for hash, show and wheel in commands
1 parent 4416e88 commit 6ab9455

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

news/BF3EC962-957A-4DB8-A849-2E7179F875A9.trivial

Whitespace-only changes.

src/pip/_internal/commands/hash.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
from __future__ import absolute_import
52

63
import hashlib
74
import logging
85
import sys
96

107
from pip._internal.cli.base_command import Command
11-
from pip._internal.cli.status_codes import ERROR
8+
from pip._internal.cli.status_codes import ERROR, SUCCESS
129
from pip._internal.utils.hashes import FAVORITE_HASH, STRONG_HASHES
1310
from pip._internal.utils.misc import read_chunks, write_output
11+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
12+
13+
if MYPY_CHECK_RUNNING:
14+
from optparse import Values
15+
from typing import Any, List, Dict
1416

1517
logger = logging.getLogger(__name__)
1618

@@ -27,7 +29,9 @@ class HashCommand(Command):
2729
ignore_require_venv = True
2830

2931
def __init__(self, *args, **kw):
30-
super(HashCommand, self).__init__(*args, **kw)
32+
# type: (List[Any], Dict[Any, Any]) -> None
33+
# https://github.com/python/mypy/issues/4335
34+
super(HashCommand, self).__init__(*args, **kw) # type: ignore
3135
self.cmd_opts.add_option(
3236
'-a', '--algorithm',
3337
dest='algorithm',
@@ -39,6 +43,7 @@ def __init__(self, *args, **kw):
3943
self.parser.insert_option_group(0, self.cmd_opts)
4044

4145
def run(self, options, args):
46+
# type: (Values, List[Any]) -> int
4247
if not args:
4348
self.parser.print_usage(sys.stderr)
4449
return ERROR
@@ -47,9 +52,11 @@ def run(self, options, args):
4752
for path in args:
4853
write_output('%s:\n--hash=%s:%s',
4954
path, algorithm, _hash_of_file(path, algorithm))
55+
return SUCCESS
5056

5157

5258
def _hash_of_file(path, algorithm):
59+
# type: (str, str) -> str
5360
"""Return the hash digest of a file."""
5461
with open(path, 'rb') as archive:
5562
hash = hashlib.new(algorithm)

src/pip/_internal/commands/show.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
# The following comment should be removed at some point in the future.
2-
# mypy: disallow-untyped-defs=False
3-
41
from __future__ import absolute_import
52

63
import logging
@@ -13,6 +10,11 @@
1310
from pip._internal.cli.base_command import Command
1411
from pip._internal.cli.status_codes import ERROR, SUCCESS
1512
from pip._internal.utils.misc import write_output
13+
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
14+
15+
if MYPY_CHECK_RUNNING:
16+
from optparse import Values
17+
from typing import Any, List, Dict, Iterator
1618

1719
logger = logging.getLogger(__name__)
1820

@@ -29,7 +31,9 @@ class ShowCommand(Command):
2931
ignore_require_venv = True
3032

3133
def __init__(self, *args, **kw):
32-
super(ShowCommand, self).__init__(*args, **kw)
34+
# type: (List[Any], Dict[Any, Any]) -> None
35+
# https://github.com/python/mypy/issues/4335
36+
super(ShowCommand, self).__init__(*args, **kw) # type: ignore
3337
self.cmd_opts.add_option(
3438
'-f', '--files',
3539
dest='files',
@@ -40,6 +44,7 @@ def __init__(self, *args, **kw):
4044
self.parser.insert_option_group(0, self.cmd_opts)
4145

4246
def run(self, options, args):
47+
# type: (Values, List[Any]) -> int
4348
if not args:
4449
logger.warning('ERROR: Please provide a package name or names.')
4550
return ERROR
@@ -53,6 +58,7 @@ def run(self, options, args):
5358

5459

5560
def search_packages_info(query):
61+
# type: (List[Any]) -> Iterator[Dict[str, Any]]
5662
"""
5763
Gather details from installed distributions. Print distribution name,
5864
version, location, and installed files. Installed files requires a
@@ -71,6 +77,7 @@ def search_packages_info(query):
7177
logger.warning('Package(s) not found: %s', ', '.join(missing))
7278

7379
def get_requiring_packages(package_name):
80+
# type: (str) -> List[str]
7481
canonical_name = canonicalize_name(package_name)
7582
return [
7683
pkg.project_name for pkg in pkg_resources.working_set
@@ -88,7 +95,7 @@ def get_requiring_packages(package_name):
8895
'required_by': get_requiring_packages(dist.project_name)
8996
}
9097
file_list = None
91-
metadata = None
98+
metadata = ''
9299
if isinstance(dist, pkg_resources.DistInfoDistribution):
93100
# RECORDs should be part of .dist-info metadatas
94101
if dist.has_metadata('RECORD'):
@@ -141,6 +148,7 @@ def get_requiring_packages(package_name):
141148

142149

143150
def print_results(distributions, list_files=False, verbose=False):
151+
# type: (Iterator[Dict[str, Any]], bool, bool) -> bool
144152
"""
145153
Print the information from installed distributions found.
146154
"""

src/pip/_internal/commands/wheel.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
# The following comment should be removed at some point in the future.
4-
# mypy: disallow-untyped-defs=False
5-
63
from __future__ import absolute_import
74

85
import logging
@@ -21,7 +18,7 @@
2118

2219
if MYPY_CHECK_RUNNING:
2320
from optparse import Values
24-
from typing import Any, List
21+
from typing import Any, List, Dict
2522

2623

2724
logger = logging.getLogger(__name__)
@@ -50,7 +47,9 @@ class WheelCommand(RequirementCommand):
5047
%prog [options] <archive url/path> ..."""
5148

5249
def __init__(self, *args, **kw):
53-
super(WheelCommand, self).__init__(*args, **kw)
50+
# type: (List[Any], Dict[Any, Any]) -> None
51+
# https://github.com/python/mypy/issues/4335
52+
super(WheelCommand, self).__init__(*args, **kw) # type: ignore
5453

5554
cmd_opts = self.cmd_opts
5655

0 commit comments

Comments
 (0)