Skip to content

Commit f518958

Browse files
authored
Merge pull request #10194 from harupy/type-annotations-internal
2 parents b9f8295 + 49afd8c commit f518958

File tree

4 files changed

+131
-191
lines changed

4 files changed

+131
-191
lines changed

src/pip/_internal/build_env.py

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232

3333
class _Prefix:
3434

35-
def __init__(self, path):
36-
# type: (str) -> None
35+
def __init__(self, path: str) -> None:
3736
self.path = path
3837
self.setup = False
3938
self.bin_dir = get_paths(
@@ -73,8 +72,7 @@ class BuildEnvironment:
7372
"""Creates and manages an isolated environment to install build deps
7473
"""
7574

76-
def __init__(self):
77-
# type: () -> None
75+
def __init__(self) -> None:
7876
temp_dir = TempDirectory(
7977
kind=tempdir_kinds.BUILD_ENV, globally_managed=True
8078
)
@@ -84,8 +82,8 @@ def __init__(self):
8482
for name in ('normal', 'overlay')
8583
)
8684

87-
self._bin_dirs = [] # type: List[str]
88-
self._lib_dirs = [] # type: List[str]
85+
self._bin_dirs: List[str] = []
86+
self._lib_dirs: List[str] = []
8987
for prefix in reversed(list(self._prefixes.values())):
9088
self._bin_dirs.append(prefix.bin_dir)
9189
self._lib_dirs.extend(prefix.lib_dirs)
@@ -127,8 +125,7 @@ def __init__(self):
127125
'''
128126
).format(system_sites=system_sites, lib_dirs=self._lib_dirs))
129127

130-
def __enter__(self):
131-
# type: () -> None
128+
def __enter__(self) -> None:
132129
self._save_env = {
133130
name: os.environ.get(name, None)
134131
for name in ('PATH', 'PYTHONNOUSERSITE', 'PYTHONPATH')
@@ -149,19 +146,19 @@ def __enter__(self):
149146

150147
def __exit__(
151148
self,
152-
exc_type, # type: Optional[Type[BaseException]]
153-
exc_val, # type: Optional[BaseException]
154-
exc_tb # type: Optional[TracebackType]
155-
):
156-
# type: (...) -> None
149+
exc_type: Optional[Type[BaseException]],
150+
exc_val: Optional[BaseException],
151+
exc_tb: Optional[TracebackType]
152+
) -> None:
157153
for varname, old_value in self._save_env.items():
158154
if old_value is None:
159155
os.environ.pop(varname, None)
160156
else:
161157
os.environ[varname] = old_value
162158

163-
def check_requirements(self, reqs):
164-
# type: (Iterable[str]) -> Tuple[Set[Tuple[str, str]], Set[str]]
159+
def check_requirements(
160+
self, reqs: Iterable[str]
161+
) -> Tuple[Set[Tuple[str, str]], Set[str]]:
165162
"""Return 2 sets:
166163
- conflicting requirements: set of (installed, wanted) reqs tuples
167164
- missing requirements: set of reqs
@@ -187,12 +184,11 @@ def check_requirements(self, reqs):
187184

188185
def install_requirements(
189186
self,
190-
finder, # type: PackageFinder
191-
requirements, # type: Iterable[str]
192-
prefix_as_string, # type: str
193-
message # type: str
194-
):
195-
# type: (...) -> None
187+
finder: "PackageFinder",
188+
requirements: Iterable[str],
189+
prefix_as_string: str,
190+
message: str
191+
) -> None:
196192
prefix = self._prefixes[prefix_as_string]
197193
assert not prefix.setup
198194
prefix.setup = True
@@ -223,11 +219,11 @@ def _install_requirements(
223219
prefix: _Prefix,
224220
message: str,
225221
) -> None:
226-
args = [
222+
args: List[str] = [
227223
sys.executable, pip_runnable, 'install',
228224
'--ignore-installed', '--no-user', '--prefix', prefix.path,
229225
'--no-warn-script-location',
230-
] # type: List[str]
226+
]
231227
if logger.getEffectiveLevel() <= logging.DEBUG:
232228
args.append('-v')
233229
for format_control in ('no_binary', 'only_binary'):
@@ -262,33 +258,28 @@ class NoOpBuildEnvironment(BuildEnvironment):
262258
"""A no-op drop-in replacement for BuildEnvironment
263259
"""
264260

265-
def __init__(self):
266-
# type: () -> None
261+
def __init__(self) -> None:
267262
pass
268263

269-
def __enter__(self):
270-
# type: () -> None
264+
def __enter__(self) -> None:
271265
pass
272266

273267
def __exit__(
274268
self,
275-
exc_type, # type: Optional[Type[BaseException]]
276-
exc_val, # type: Optional[BaseException]
277-
exc_tb # type: Optional[TracebackType]
278-
):
279-
# type: (...) -> None
269+
exc_type: Optional[Type[BaseException]],
270+
exc_val: Optional[BaseException],
271+
exc_tb: Optional[TracebackType]
272+
) -> None:
280273
pass
281274

282-
def cleanup(self):
283-
# type: () -> None
275+
def cleanup(self) -> None:
284276
pass
285277

286278
def install_requirements(
287279
self,
288-
finder, # type: PackageFinder
289-
requirements, # type: Iterable[str]
290-
prefix_as_string, # type: str
291-
message # type: str
292-
):
293-
# type: (...) -> None
280+
finder: "PackageFinder",
281+
requirements: Iterable[str],
282+
prefix_as_string: str,
283+
message: str
284+
) -> None:
294285
raise NotImplementedError()

src/pip/_internal/cache.py

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
logger = logging.getLogger(__name__)
2121

2222

23-
def _hash_dict(d):
24-
# type: (Dict[str, str]) -> str
23+
def _hash_dict(d: Dict[str, str]) -> str:
2524
"""Return a stable sha224 of a dictionary."""
2625
s = json.dumps(d, sort_keys=True, separators=(",", ":"), ensure_ascii=True)
2726
return hashlib.sha224(s.encode("ascii")).hexdigest()
@@ -38,8 +37,9 @@ class Cache:
3837
('binary' and 'source' are the only allowed values)
3938
"""
4039

41-
def __init__(self, cache_dir, format_control, allowed_formats):
42-
# type: (str, FormatControl, Set[str]) -> None
40+
def __init__(
41+
self, cache_dir: str, format_control: FormatControl, allowed_formats: Set[str]
42+
) -> None:
4343
super().__init__()
4444
assert not cache_dir or os.path.isabs(cache_dir)
4545
self.cache_dir = cache_dir or None
@@ -49,8 +49,7 @@ def __init__(self, cache_dir, format_control, allowed_formats):
4949
_valid_formats = {"source", "binary"}
5050
assert self.allowed_formats.union(_valid_formats) == _valid_formats
5151

52-
def _get_cache_path_parts(self, link):
53-
# type: (Link) -> List[str]
52+
def _get_cache_path_parts(self, link: Link) -> List[str]:
5453
"""Get parts of part that must be os.path.joined with cache_dir
5554
"""
5655

@@ -84,8 +83,7 @@ def _get_cache_path_parts(self, link):
8483

8584
return parts
8685

87-
def _get_candidates(self, link, canonical_package_name):
88-
# type: (Link, str) -> List[Any]
86+
def _get_candidates(self, link: Link, canonical_package_name: str) -> List[Any]:
8987
can_not_cache = (
9088
not self.cache_dir or
9189
not canonical_package_name or
@@ -107,19 +105,17 @@ def _get_candidates(self, link, canonical_package_name):
107105
candidates.append((candidate, path))
108106
return candidates
109107

110-
def get_path_for_link(self, link):
111-
# type: (Link) -> str
108+
def get_path_for_link(self, link: Link) -> str:
112109
"""Return a directory to store cached items in for link.
113110
"""
114111
raise NotImplementedError()
115112

116113
def get(
117114
self,
118-
link, # type: Link
119-
package_name, # type: Optional[str]
120-
supported_tags, # type: List[Tag]
121-
):
122-
# type: (...) -> Link
115+
link: Link,
116+
package_name: Optional[str],
117+
supported_tags: List[Tag],
118+
) -> Link:
123119
"""Returns a link to a cached item if it exists, otherwise returns the
124120
passed link.
125121
"""
@@ -130,12 +126,10 @@ class SimpleWheelCache(Cache):
130126
"""A cache of wheels for future installs.
131127
"""
132128

133-
def __init__(self, cache_dir, format_control):
134-
# type: (str, FormatControl) -> None
129+
def __init__(self, cache_dir: str, format_control: FormatControl) -> None:
135130
super().__init__(cache_dir, format_control, {"binary"})
136131

137-
def get_path_for_link(self, link):
138-
# type: (Link) -> str
132+
def get_path_for_link(self, link: Link) -> str:
139133
"""Return a directory to store cached wheels for link
140134
141135
Because there are M wheels for any one sdist, we provide a directory
@@ -157,11 +151,10 @@ def get_path_for_link(self, link):
157151

158152
def get(
159153
self,
160-
link, # type: Link
161-
package_name, # type: Optional[str]
162-
supported_tags, # type: List[Tag]
163-
):
164-
# type: (...) -> Link
154+
link: Link,
155+
package_name: Optional[str],
156+
supported_tags: List[Tag],
157+
) -> Link:
165158
candidates = []
166159

167160
if not package_name:
@@ -204,8 +197,7 @@ class EphemWheelCache(SimpleWheelCache):
204197
"""A SimpleWheelCache that creates it's own temporary cache directory
205198
"""
206199

207-
def __init__(self, format_control):
208-
# type: (FormatControl) -> None
200+
def __init__(self, format_control: FormatControl) -> None:
209201
self._temp_dir = TempDirectory(
210202
kind=tempdir_kinds.EPHEM_WHEEL_CACHE,
211203
globally_managed=True,
@@ -217,8 +209,8 @@ def __init__(self, format_control):
217209
class CacheEntry:
218210
def __init__(
219211
self,
220-
link, # type: Link
221-
persistent, # type: bool
212+
link: Link,
213+
persistent: bool,
222214
):
223215
self.link = link
224216
self.persistent = persistent
@@ -231,39 +223,34 @@ class WheelCache(Cache):
231223
when a certain link is not found in the simple wheel cache first.
232224
"""
233225

234-
def __init__(self, cache_dir, format_control):
235-
# type: (str, FormatControl) -> None
226+
def __init__(self, cache_dir: str, format_control: FormatControl) -> None:
236227
super().__init__(cache_dir, format_control, {'binary'})
237228
self._wheel_cache = SimpleWheelCache(cache_dir, format_control)
238229
self._ephem_cache = EphemWheelCache(format_control)
239230

240-
def get_path_for_link(self, link):
241-
# type: (Link) -> str
231+
def get_path_for_link(self, link: Link) -> str:
242232
return self._wheel_cache.get_path_for_link(link)
243233

244-
def get_ephem_path_for_link(self, link):
245-
# type: (Link) -> str
234+
def get_ephem_path_for_link(self, link: Link) -> str:
246235
return self._ephem_cache.get_path_for_link(link)
247236

248237
def get(
249238
self,
250-
link, # type: Link
251-
package_name, # type: Optional[str]
252-
supported_tags, # type: List[Tag]
253-
):
254-
# type: (...) -> Link
239+
link: Link,
240+
package_name: Optional[str],
241+
supported_tags: List[Tag],
242+
) -> Link:
255243
cache_entry = self.get_cache_entry(link, package_name, supported_tags)
256244
if cache_entry is None:
257245
return link
258246
return cache_entry.link
259247

260248
def get_cache_entry(
261249
self,
262-
link, # type: Link
263-
package_name, # type: Optional[str]
264-
supported_tags, # type: List[Tag]
265-
):
266-
# type: (...) -> Optional[CacheEntry]
250+
link: Link,
251+
package_name: Optional[str],
252+
supported_tags: List[Tag],
253+
) -> Optional[CacheEntry]:
267254
"""Returns a CacheEntry with a link to a cached item if it exists or
268255
None. The cache entry indicates if the item was found in the persistent
269256
or ephemeral cache.

0 commit comments

Comments
 (0)