Skip to content

Commit 23eb69f

Browse files
authored
Complete type annotations in pip/_internal/locations (#10127)
1 parent 72e1ff3 commit 23eb69f

File tree

5 files changed

+51
-67
lines changed

5 files changed

+51
-67
lines changed

news/10127.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Converted type commentaries into annotations in ``pip/_internal/locations``.

src/pip/_internal/locations/__init__.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ def _log_context(
6969

7070

7171
def get_scheme(
72-
dist_name, # type: str
73-
user=False, # type: bool
74-
home=None, # type: Optional[str]
75-
root=None, # type: Optional[str]
76-
isolated=False, # type: bool
77-
prefix=None, # type: Optional[str]
78-
):
79-
# type: (...) -> Scheme
72+
dist_name: str,
73+
user: bool = False,
74+
home: Optional[str] = None,
75+
root: Optional[str] = None,
76+
isolated: bool = False,
77+
prefix: Optional[str] = None,
78+
) -> Scheme:
8079
old = _distutils.get_scheme(
8180
dist_name,
8281
user=user,
@@ -124,22 +123,19 @@ def get_scheme(
124123
return old
125124

126125

127-
def get_bin_prefix():
128-
# type: () -> str
126+
def get_bin_prefix() -> str:
129127
old = _distutils.get_bin_prefix()
130128
new = _sysconfig.get_bin_prefix()
131129
if _warn_if_mismatch(pathlib.Path(old), pathlib.Path(new), key="bin_prefix"):
132130
_log_context()
133131
return old
134132

135133

136-
def get_bin_user():
137-
# type: () -> str
134+
def get_bin_user() -> str:
138135
return _sysconfig.get_scheme("", user=True).scripts
139136

140137

141-
def get_purelib():
142-
# type: () -> str
138+
def get_purelib() -> str:
143139
"""Return the default pure-Python lib location."""
144140
old = _distutils.get_purelib()
145141
new = _sysconfig.get_purelib()
@@ -148,8 +144,7 @@ def get_purelib():
148144
return old
149145

150146

151-
def get_platlib():
152-
# type: () -> str
147+
def get_platlib() -> str:
153148
"""Return the default platform-shared lib location."""
154149
old = _distutils.get_platlib()
155150
new = _sysconfig.get_platlib()
@@ -158,8 +153,7 @@ def get_platlib():
158153
return old
159154

160155

161-
def get_prefixed_libs(prefix):
162-
# type: (str) -> List[str]
156+
def get_prefixed_libs(prefix: str) -> List[str]:
163157
"""Return the lib locations under ``prefix``."""
164158
old_pure, old_plat = _distutils.get_prefixed_libs(prefix)
165159
new_pure, new_plat = _sysconfig.get_prefixed_libs(prefix)

src/pip/_internal/locations/_distutils.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@
1919

2020

2121
def _distutils_scheme(
22-
dist_name, user=False, home=None, root=None, isolated=False, prefix=None
23-
):
24-
# type:(str, bool, str, str, bool, str) -> Dict[str, str]
22+
dist_name: str,
23+
user: bool = False,
24+
home: str = None,
25+
root: str = None,
26+
isolated: bool = False,
27+
prefix: str = None,
28+
) -> Dict[str, str]:
2529
"""
2630
Return a distutils install scheme
2731
"""
2832
from distutils.dist import Distribution
2933

30-
dist_args = {"name": dist_name} # type: Dict[str, Union[str, List[str]]]
34+
dist_args: Dict[str, Union[str, List[str]]] = {"name": dist_name}
3135
if isolated:
3236
dist_args["script_args"] = ["--no-user-cfg"]
3337

3438
d = Distribution(dist_args)
3539
d.parse_config_files()
36-
obj = None # type: Optional[DistutilsCommand]
40+
obj: Optional[DistutilsCommand] = None
3741
obj = d.get_command_obj("install", create=True)
3842
assert obj is not None
3943
i = cast(distutils_install_command, obj)
@@ -82,14 +86,13 @@ def _distutils_scheme(
8286

8387

8488
def get_scheme(
85-
dist_name, # type: str
86-
user=False, # type: bool
87-
home=None, # type: Optional[str]
88-
root=None, # type: Optional[str]
89-
isolated=False, # type: bool
90-
prefix=None, # type: Optional[str]
91-
):
92-
# type: (...) -> Scheme
89+
dist_name: str,
90+
user: bool = False,
91+
home: Optional[str] = None,
92+
root: Optional[str] = None,
93+
isolated: bool = False,
94+
prefix: Optional[str] = None,
95+
) -> Scheme:
9396
"""
9497
Get the "scheme" corresponding to the input parameters. The distutils
9598
documentation provides the context for the available schemes:
@@ -117,8 +120,7 @@ def get_scheme(
117120
)
118121

119122

120-
def get_bin_prefix():
121-
# type: () -> str
123+
def get_bin_prefix() -> str:
122124
if WINDOWS:
123125
bin_py = os.path.join(sys.prefix, "Scripts")
124126
# buildout uses 'bin' on Windows too?
@@ -132,18 +134,15 @@ def get_bin_prefix():
132134
return os.path.join(sys.prefix, "bin")
133135

134136

135-
def get_purelib():
136-
# type: () -> str
137+
def get_purelib() -> str:
137138
return get_python_lib(plat_specific=False)
138139

139140

140-
def get_platlib():
141-
# type: () -> str
141+
def get_platlib() -> str:
142142
return get_python_lib(plat_specific=True)
143143

144144

145-
def get_prefixed_libs(prefix):
146-
# type: (str) -> Tuple[str, str]
145+
def get_prefixed_libs(prefix: str) -> Tuple[str, str]:
147146
return (
148147
get_python_lib(plat_specific=False, prefix=prefix),
149148
get_python_lib(plat_specific=True, prefix=prefix),

src/pip/_internal/locations/_sysconfig.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
_AVAILABLE_SCHEMES = set(sysconfig.get_scheme_names())
2626

2727

28-
def _infer_prefix():
29-
# type: () -> str
28+
def _infer_prefix() -> str:
3029
"""Try to find a prefix scheme for the current platform.
3130
3231
This tries:
@@ -51,8 +50,7 @@ def _infer_prefix():
5150
return "posix_prefix"
5251

5352

54-
def _infer_user():
55-
# type: () -> str
53+
def _infer_user() -> str:
5654
"""Try to find a user scheme for the current platform."""
5755
suffixed = f"{os.name}_user"
5856
if suffixed in _AVAILABLE_SCHEMES:
@@ -62,8 +60,7 @@ def _infer_user():
6260
return "posix_user"
6361

6462

65-
def _infer_home():
66-
# type: () -> str
63+
def _infer_home() -> str:
6764
"""Try to find a home for the current platform."""
6865
suffixed = f"{os.name}_home"
6966
if suffixed in _AVAILABLE_SCHEMES:
@@ -85,14 +82,13 @@ def _infer_home():
8582

8683

8784
def get_scheme(
88-
dist_name, # type: str
89-
user=False, # type: bool
90-
home=None, # type: typing.Optional[str]
91-
root=None, # type: typing.Optional[str]
92-
isolated=False, # type: bool
93-
prefix=None, # type: typing.Optional[str]
94-
):
95-
# type: (...) -> Scheme
85+
dist_name: str,
86+
user: bool = False,
87+
home: typing.Optional[str] = None,
88+
root: typing.Optional[str] = None,
89+
isolated: bool = False,
90+
prefix: typing.Optional[str] = None,
91+
) -> Scheme:
9692
"""
9793
Get the "scheme" corresponding to the input parameters.
9894
@@ -156,25 +152,21 @@ def get_scheme(
156152
return scheme
157153

158154

159-
def get_bin_prefix():
160-
# type: () -> str
155+
def get_bin_prefix() -> str:
161156
# Forcing to use /usr/local/bin for standard macOS framework installs.
162157
if sys.platform[:6] == "darwin" and sys.prefix[:16] == "/System/Library/":
163158
return "/usr/local/bin"
164159
return sysconfig.get_paths()["scripts"]
165160

166161

167-
def get_purelib():
168-
# type: () -> str
162+
def get_purelib() -> str:
169163
return sysconfig.get_paths()["purelib"]
170164

171165

172-
def get_platlib():
173-
# type: () -> str
166+
def get_platlib() -> str:
174167
return sysconfig.get_paths()["platlib"]
175168

176169

177-
def get_prefixed_libs(prefix):
178-
# type: (str) -> typing.Tuple[str, str]
170+
def get_prefixed_libs(prefix: str) -> typing.Tuple[str, str]:
179171
paths = sysconfig.get_paths(vars={"base": prefix, "platbase": prefix})
180172
return (paths["purelib"], paths["platlib"])

src/pip/_internal/locations/base.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@
1111
USER_CACHE_DIR = appdirs.user_cache_dir("pip")
1212

1313
# FIXME doesn't account for venv linked to global site-packages
14-
site_packages = sysconfig.get_path("purelib") # type: typing.Optional[str]
14+
site_packages: typing.Optional[str] = sysconfig.get_path("purelib")
1515

1616

17-
def get_major_minor_version():
18-
# type: () -> str
17+
def get_major_minor_version() -> str:
1918
"""
2019
Return the major-minor version of the current Python as a string, e.g.
2120
"3.7" or "3.10".
2221
"""
2322
return "{}.{}".format(*sys.version_info)
2423

2524

26-
def get_src_prefix():
27-
# type: () -> str
25+
def get_src_prefix() -> str:
2826
if running_under_virtualenv():
2927
src_prefix = os.path.join(sys.prefix, "src")
3028
else:
@@ -43,6 +41,6 @@ def get_src_prefix():
4341
try:
4442
# Use getusersitepackages if this is present, as it ensures that the
4543
# value is initialised properly.
46-
user_site = site.getusersitepackages() # type: typing.Optional[str]
44+
user_site: typing.Optional[str] = site.getusersitepackages()
4745
except AttributeError:
4846
user_site = site.USER_SITE

0 commit comments

Comments
 (0)