Skip to content

Commit e689113

Browse files
DiddiLeijauranusjr
andauthored
Complete the annotations: pip/_internal/utils (#10159)
Co-authored-by: Tzu-ping Chung <[email protected]>
1 parent df98167 commit e689113

File tree

11 files changed

+59
-82
lines changed

11 files changed

+59
-82
lines changed

news/10159.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Complete the type annotations from ``pip/_internal/utils``.

src/pip/_internal/utils/appdirs.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
from pip._vendor import appdirs as _appdirs
1313

1414

15-
def user_cache_dir(appname):
16-
# type: (str) -> str
15+
def user_cache_dir(appname: str) -> str:
1716
return _appdirs.user_cache_dir(appname, appauthor=False)
1817

1918

20-
def user_config_dir(appname, roaming=True):
21-
# type: (str, bool) -> str
19+
def user_config_dir(appname: str, roaming: bool = True) -> str:
2220
path = _appdirs.user_config_dir(appname, appauthor=False, roaming=roaming)
2321
if _appdirs.system == "darwin" and not os.path.isdir(path):
2422
path = os.path.expanduser("~/.config/")
@@ -29,8 +27,7 @@ def user_config_dir(appname, roaming=True):
2927

3028
# for the discussion regarding site_config_dir locations
3129
# see <https://github.com/pypa/pip/issues/1733>
32-
def site_config_dirs(appname):
33-
# type: (str) -> List[str]
30+
def site_config_dirs(appname: str) -> List[str]:
3431
dirval = _appdirs.site_config_dir(appname, appauthor=False, multipath=True)
3532
if _appdirs.system not in ["win32", "darwin"]:
3633
# always look in /etc directly as well

src/pip/_internal/utils/compat.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14-
def has_tls():
15-
# type: () -> bool
14+
def has_tls() -> bool:
1615
try:
1716
import _ssl # noqa: F401 # ignore unused
1817

@@ -25,8 +24,7 @@ def has_tls():
2524
return IS_PYOPENSSL
2625

2726

28-
def get_path_uid(path):
29-
# type: (str) -> int
27+
def get_path_uid(path: str) -> int:
3028
"""
3129
Return path's uid.
3230

src/pip/_internal/utils/compatibility_tags.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
_osx_arch_pat = re.compile(r"(.+)_(\d+)_(\d+)_(.+)")
2222

2323

24-
def version_info_to_nodot(version_info):
25-
# type: (Tuple[int, ...]) -> str
24+
def version_info_to_nodot(version_info: Tuple[int, ...]) -> str:
2625
# Only use up to the first two numbers.
2726
return "".join(map(str, version_info[:2]))
2827

2928

30-
def _mac_platforms(arch):
31-
# type: (str) -> List[str]
29+
def _mac_platforms(arch: str) -> List[str]:
3230
match = _osx_arch_pat.match(arch)
3331
if match:
3432
name, major, minor, actual_arch = match.groups()
@@ -48,8 +46,7 @@ def _mac_platforms(arch):
4846
return arches
4947

5048

51-
def _custom_manylinux_platforms(arch):
52-
# type: (str) -> List[str]
49+
def _custom_manylinux_platforms(arch: str) -> List[str]:
5350
arches = [arch]
5451
arch_prefix, arch_sep, arch_suffix = arch.partition("_")
5552
if arch_prefix == "manylinux2014":
@@ -70,8 +67,7 @@ def _custom_manylinux_platforms(arch):
7067
return arches
7168

7269

73-
def _get_custom_platforms(arch):
74-
# type: (str) -> List[str]
70+
def _get_custom_platforms(arch: str) -> List[str]:
7571
arch_prefix, arch_sep, arch_suffix = arch.partition("_")
7672
if arch.startswith("macosx"):
7773
arches = _mac_platforms(arch)
@@ -82,8 +78,7 @@ def _get_custom_platforms(arch):
8278
return arches
8379

8480

85-
def _expand_allowed_platforms(platforms):
86-
# type: (Optional[List[str]]) -> Optional[List[str]]
81+
def _expand_allowed_platforms(platforms: Optional[List[str]]) -> Optional[List[str]]:
8782
if not platforms:
8883
return None
8984

@@ -100,16 +95,16 @@ def _expand_allowed_platforms(platforms):
10095
return result
10196

10297

103-
def _get_python_version(version):
104-
# type: (str) -> PythonVersion
98+
def _get_python_version(version: str) -> "PythonVersion":
10599
if len(version) > 1:
106100
return int(version[0]), int(version[1:])
107101
else:
108102
return (int(version[0]),)
109103

110104

111-
def _get_custom_interpreter(implementation=None, version=None):
112-
# type: (Optional[str], Optional[str]) -> str
105+
def _get_custom_interpreter(
106+
implementation: Optional[str] = None, version: Optional[str] = None
107+
) -> str:
113108
if implementation is None:
114109
implementation = interpreter_name()
115110
if version is None:
@@ -118,12 +113,11 @@ def _get_custom_interpreter(implementation=None, version=None):
118113

119114

120115
def get_supported(
121-
version=None, # type: Optional[str]
122-
platforms=None, # type: Optional[List[str]]
123-
impl=None, # type: Optional[str]
124-
abis=None, # type: Optional[List[str]]
125-
):
126-
# type: (...) -> List[Tag]
116+
version: Optional[str] = None,
117+
platforms: Optional[List[str]] = None,
118+
impl: Optional[str] = None,
119+
abis: Optional[List[str]] = None,
120+
) -> List[Tag]:
127121
"""Return a list of supported tags for each version specified in
128122
`versions`.
129123
@@ -136,9 +130,9 @@ def get_supported(
136130
:param abis: specify a list of abis you want valid
137131
tags for, or None. If None, use the local interpreter abi.
138132
"""
139-
supported = [] # type: List[Tag]
133+
supported: List[Tag] = []
140134

141-
python_version = None # type: Optional[PythonVersion]
135+
python_version: Optional["PythonVersion"] = None
142136
if version is not None:
143137
python_version = _get_python_version(version)
144138

src/pip/_internal/utils/datetime.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import datetime
55

66

7-
def today_is_later_than(year, month, day):
8-
# type: (int, int, int) -> bool
7+
def today_is_later_than(year: int, month: int, day: int) -> bool:
98
today = datetime.date.today()
109
given = datetime.date(year, month, day)
1110

src/pip/_internal/utils/deprecation.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ class PipDeprecationWarning(Warning):
1717
pass
1818

1919

20-
_original_showwarning = None # type: Any
20+
_original_showwarning: Any = None
2121

2222

2323
# Warnings <-> Logging Integration
2424
def _showwarning(
25-
message, # type: Union[Warning, str]
26-
category, # type: Type[Warning]
27-
filename, # type: str
28-
lineno, # type: int
29-
file=None, # type: Optional[TextIO]
30-
line=None, # type: Optional[str]
31-
):
32-
# type: (...) -> None
25+
message: Union[Warning, str],
26+
category: Type[Warning],
27+
filename: str,
28+
lineno: int,
29+
file: Optional[TextIO] = None,
30+
line: Optional[str] = None,
31+
) -> None:
3332
if file is not None:
3433
if _original_showwarning is not None:
3534
_original_showwarning(message, category, filename, lineno, file, line)
@@ -42,8 +41,7 @@ def _showwarning(
4241
_original_showwarning(message, category, filename, lineno, file, line)
4342

4443

45-
def install_warning_logger():
46-
# type: () -> None
44+
def install_warning_logger() -> None:
4745
# Enable our Deprecation Warnings
4846
warnings.simplefilter("default", PipDeprecationWarning, append=True)
4947

@@ -54,8 +52,12 @@ def install_warning_logger():
5452
warnings.showwarning = _showwarning
5553

5654

57-
def deprecated(reason, replacement, gone_in, issue=None):
58-
# type: (str, Optional[str], Optional[str], Optional[int]) -> None
55+
def deprecated(
56+
reason: str,
57+
replacement: Optional[str],
58+
gone_in: Optional[str],
59+
issue: Optional[int] = None,
60+
) -> None:
5961
"""Helper to deprecate existing functionality.
6062
6163
reason:

src/pip/_internal/utils/direct_url_helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from pip._internal.vcs import vcs
66

77

8-
def direct_url_as_pep440_direct_reference(direct_url, name):
9-
# type: (DirectUrl, str) -> str
8+
def direct_url_as_pep440_direct_reference(direct_url: DirectUrl, name: str) -> str:
109
"""Convert a DirectUrl to a pip requirement string."""
1110
direct_url.validate() # if invalid, this is a pip bug
1211
requirement = name + " @ "
@@ -29,8 +28,9 @@ def direct_url_as_pep440_direct_reference(direct_url, name):
2928
return requirement
3029

3130

32-
def direct_url_from_link(link, source_dir=None, link_is_in_wheel_cache=False):
33-
# type: (Link, Optional[str], bool) -> DirectUrl
31+
def direct_url_from_link(
32+
link: Link, source_dir: Optional[str] = None, link_is_in_wheel_cache: bool = False
33+
) -> DirectUrl:
3434
if link.is_vcs:
3535
vcs_backend = vcs.get_backend_for_scheme(link.scheme)
3636
assert vcs_backend

src/pip/_internal/utils/distutils_args.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
_distutils_getopt = FancyGetopt(_options) # type: ignore
2323

2424

25-
def parse_distutils_args(args):
26-
# type: (List[str]) -> Dict[str, str]
25+
def parse_distutils_args(args: List[str]) -> Dict[str, str]:
2726
"""Parse provided arguments, returning an object that has the
2827
matched arguments.
2928

src/pip/_internal/utils/encoding.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
import sys
55
from typing import List, Tuple
66

7-
BOMS = [
7+
BOMS: List[Tuple[bytes, str]] = [
88
(codecs.BOM_UTF8, "utf-8"),
99
(codecs.BOM_UTF16, "utf-16"),
1010
(codecs.BOM_UTF16_BE, "utf-16-be"),
1111
(codecs.BOM_UTF16_LE, "utf-16-le"),
1212
(codecs.BOM_UTF32, "utf-32"),
1313
(codecs.BOM_UTF32_BE, "utf-32-be"),
1414
(codecs.BOM_UTF32_LE, "utf-32-le"),
15-
] # type: List[Tuple[bytes, str]]
15+
]
1616

1717
ENCODING_RE = re.compile(br"coding[:=]\s*([-\w.]+)")
1818

1919

20-
def auto_decode(data):
21-
# type: (bytes) -> str
20+
def auto_decode(data: bytes) -> str:
2221
"""Check a bytes string for a BOM to correctly detect the encoding
2322
2423
Fallback to locale.getpreferredencoding(False) like open() on Python3"""

src/pip/_internal/utils/entrypoints.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from pip._internal.cli.main import main
55

66

7-
def _wrapper(args=None):
8-
# type: (Optional[List[str]]) -> int
7+
def _wrapper(args: Optional[List[str]] = None) -> int:
98
"""Central wrapper for all old entrypoints.
109
1110
Historically pip has had several entrypoints defined. Because of issues

0 commit comments

Comments
 (0)