Skip to content

Commit ce86dc8

Browse files
authored
Complete type annotations in pip/_internal/index (#10111)
1 parent cccf380 commit ce86dc8

File tree

3 files changed

+142
-194
lines changed

3 files changed

+142
-194
lines changed

news/10111.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/index``.

src/pip/_internal/index/collector.py

Lines changed: 47 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
ResponseHeaders = MutableMapping[str, str]
4747

4848

49-
def _match_vcs_scheme(url):
50-
# type: (str) -> Optional[str]
49+
def _match_vcs_scheme(url: str) -> Optional[str]:
5150
"""Look for VCS schemes in the URL.
5251
5352
Returns the matched VCS scheme, or None if there's no match.
@@ -59,15 +58,13 @@ def _match_vcs_scheme(url):
5958

6059

6160
class _NotHTML(Exception):
62-
def __init__(self, content_type, request_desc):
63-
# type: (str, str) -> None
61+
def __init__(self, content_type: str, request_desc: str) -> None:
6462
super().__init__(content_type, request_desc)
6563
self.content_type = content_type
6664
self.request_desc = request_desc
6765

6866

69-
def _ensure_html_header(response):
70-
# type: (Response) -> None
67+
def _ensure_html_header(response: Response) -> None:
7168
"""Check the Content-Type header to ensure the response contains HTML.
7269
7370
Raises `_NotHTML` if the content type is not text/html.
@@ -81,8 +78,7 @@ class _NotHTTP(Exception):
8178
pass
8279

8380

84-
def _ensure_html_response(url, session):
85-
# type: (str, PipSession) -> None
81+
def _ensure_html_response(url: str, session: PipSession) -> None:
8682
"""Send a HEAD request to the URL, and ensure the response contains HTML.
8783
8884
Raises `_NotHTTP` if the URL is not available for a HEAD request, or
@@ -98,8 +94,7 @@ def _ensure_html_response(url, session):
9894
_ensure_html_header(resp)
9995

10096

101-
def _get_html_response(url, session):
102-
# type: (str, PipSession) -> Response
97+
def _get_html_response(url: str, session: PipSession) -> Response:
10398
"""Access an HTML page with GET, and return the response.
10499
105100
This consists of three parts:
@@ -149,8 +144,7 @@ def _get_html_response(url, session):
149144
return resp
150145

151146

152-
def _get_encoding_from_headers(headers):
153-
# type: (ResponseHeaders) -> Optional[str]
147+
def _get_encoding_from_headers(headers: ResponseHeaders) -> Optional[str]:
154148
"""Determine if we have any encoding information in our headers.
155149
"""
156150
if headers and "Content-Type" in headers:
@@ -160,8 +154,7 @@ def _get_encoding_from_headers(headers):
160154
return None
161155

162156

163-
def _determine_base_url(document, page_url):
164-
# type: (HTMLElement, str) -> str
157+
def _determine_base_url(document: HTMLElement, page_url: str) -> str:
165158
"""Determine the HTML document's base URL.
166159
167160
This looks for a ``<base>`` tag in the HTML document. If present, its href
@@ -180,17 +173,15 @@ def _determine_base_url(document, page_url):
180173
return page_url
181174

182175

183-
def _clean_url_path_part(part):
184-
# type: (str) -> str
176+
def _clean_url_path_part(part: str) -> str:
185177
"""
186178
Clean a "part" of a URL path (i.e. after splitting on "@" characters).
187179
"""
188180
# We unquote prior to quoting to make sure nothing is double quoted.
189181
return urllib.parse.quote(urllib.parse.unquote(part))
190182

191183

192-
def _clean_file_url_path(part):
193-
# type: (str) -> str
184+
def _clean_file_url_path(part: str) -> str:
194185
"""
195186
Clean the first part of a URL path that corresponds to a local
196187
filesystem path (i.e. the first part after splitting on "@" characters).
@@ -207,8 +198,7 @@ def _clean_file_url_path(part):
207198
_reserved_chars_re = re.compile('(@|%2F)', re.IGNORECASE)
208199

209200

210-
def _clean_url_path(path, is_local_path):
211-
# type: (str, bool) -> str
201+
def _clean_url_path(path: str, is_local_path: bool) -> str:
212202
"""
213203
Clean the path portion of a URL.
214204
"""
@@ -230,8 +220,7 @@ def _clean_url_path(path, is_local_path):
230220
return ''.join(cleaned_parts)
231221

232222

233-
def _clean_link(url):
234-
# type: (str) -> str
223+
def _clean_link(url: str) -> str:
235224
"""
236225
Make sure a link is fully quoted.
237226
For example, if ' ' occurs in the URL, it will be replaced with "%20",
@@ -247,11 +236,10 @@ def _clean_link(url):
247236

248237

249238
def _create_link_from_element(
250-
anchor, # type: HTMLElement
251-
page_url, # type: str
252-
base_url, # type: str
253-
):
254-
# type: (...) -> Optional[Link]
239+
anchor: HTMLElement,
240+
page_url: str,
241+
base_url: str,
242+
) -> Optional[Link]:
255243
"""
256244
Convert an anchor element in a simple repository page to a Link.
257245
"""
@@ -278,39 +266,33 @@ def _create_link_from_element(
278266

279267

280268
class CacheablePageContent:
281-
def __init__(self, page):
282-
# type: (HTMLPage) -> None
269+
def __init__(self, page: "HTMLPage") -> None:
283270
assert page.cache_link_parsing
284271
self.page = page
285272

286-
def __eq__(self, other):
287-
# type: (object) -> bool
273+
def __eq__(self, other: object) -> bool:
288274
return (isinstance(other, type(self)) and
289275
self.page.url == other.page.url)
290276

291-
def __hash__(self):
292-
# type: () -> int
277+
def __hash__(self) -> int:
293278
return hash(self.page.url)
294279

295280

296281
def with_cached_html_pages(
297-
fn, # type: Callable[[HTMLPage], Iterable[Link]]
298-
):
299-
# type: (...) -> Callable[[HTMLPage], List[Link]]
282+
fn: Callable[["HTMLPage"], Iterable[Link]],
283+
) -> Callable[["HTMLPage"], List[Link]]:
300284
"""
301285
Given a function that parses an Iterable[Link] from an HTMLPage, cache the
302286
function's result (keyed by CacheablePageContent), unless the HTMLPage
303287
`page` has `page.cache_link_parsing == False`.
304288
"""
305289

306290
@functools.lru_cache(maxsize=None)
307-
def wrapper(cacheable_page):
308-
# type: (CacheablePageContent) -> List[Link]
291+
def wrapper(cacheable_page: CacheablePageContent) -> List[Link]:
309292
return list(fn(cacheable_page.page))
310293

311294
@functools.wraps(fn)
312-
def wrapper_wrapper(page):
313-
# type: (HTMLPage) -> List[Link]
295+
def wrapper_wrapper(page: "HTMLPage") -> List[Link]:
314296
if page.cache_link_parsing:
315297
return wrapper(CacheablePageContent(page))
316298
return list(fn(page))
@@ -319,8 +301,7 @@ def wrapper_wrapper(page):
319301

320302

321303
@with_cached_html_pages
322-
def parse_links(page):
323-
# type: (HTMLPage) -> Iterable[Link]
304+
def parse_links(page: "HTMLPage") -> Iterable[Link]:
324305
"""
325306
Parse an HTML document, and yield its anchor elements as Link objects.
326307
"""
@@ -348,12 +329,11 @@ class HTMLPage:
348329

349330
def __init__(
350331
self,
351-
content, # type: bytes
352-
encoding, # type: Optional[str]
353-
url, # type: str
354-
cache_link_parsing=True, # type: bool
355-
):
356-
# type: (...) -> None
332+
content: bytes,
333+
encoding: Optional[str],
334+
url: str,
335+
cache_link_parsing: bool = True,
336+
) -> None:
357337
"""
358338
:param encoding: the encoding to decode the given content.
359339
:param url: the URL from which the HTML was downloaded.
@@ -366,24 +346,21 @@ def __init__(
366346
self.url = url
367347
self.cache_link_parsing = cache_link_parsing
368348

369-
def __str__(self):
370-
# type: () -> str
349+
def __str__(self) -> str:
371350
return redact_auth_from_url(self.url)
372351

373352

374353
def _handle_get_page_fail(
375-
link, # type: Link
376-
reason, # type: Union[str, Exception]
377-
meth=None # type: Optional[Callable[..., None]]
378-
):
379-
# type: (...) -> None
354+
link: Link,
355+
reason: Union[str, Exception],
356+
meth: Optional[Callable[..., None]] = None
357+
) -> None:
380358
if meth is None:
381359
meth = logger.debug
382360
meth("Could not fetch URL %s: %s - skipping", link, reason)
383361

384362

385-
def _make_html_page(response, cache_link_parsing=True):
386-
# type: (Response, bool) -> HTMLPage
363+
def _make_html_page(response: Response, cache_link_parsing: bool = True) -> HTMLPage:
387364
encoding = _get_encoding_from_headers(response.headers)
388365
return HTMLPage(
389366
response.content,
@@ -392,8 +369,9 @@ def _make_html_page(response, cache_link_parsing=True):
392369
cache_link_parsing=cache_link_parsing)
393370

394371

395-
def _get_html_page(link, session=None):
396-
# type: (Link, Optional[PipSession]) -> Optional[HTMLPage]
372+
def _get_html_page(
373+
link: Link, session: Optional[PipSession] = None
374+
) -> Optional["HTMLPage"]:
397375
if session is None:
398376
raise TypeError(
399377
"_get_html_page() missing 1 required keyword argument: 'session'"
@@ -465,16 +443,18 @@ class LinkCollector:
465443

466444
def __init__(
467445
self,
468-
session, # type: PipSession
469-
search_scope, # type: SearchScope
470-
):
471-
# type: (...) -> None
446+
session: PipSession,
447+
search_scope: SearchScope,
448+
) -> None:
472449
self.search_scope = search_scope
473450
self.session = session
474451

475452
@classmethod
476-
def create(cls, session, options, suppress_no_index=False):
477-
# type: (PipSession, Values, bool) -> LinkCollector
453+
def create(
454+
cls, session: PipSession,
455+
options: Values,
456+
suppress_no_index: bool = False
457+
) -> "LinkCollector":
478458
"""
479459
:param session: The Session to use to make requests.
480460
:param suppress_no_index: Whether to ignore the --no-index option
@@ -500,12 +480,10 @@ def create(cls, session, options, suppress_no_index=False):
500480
return link_collector
501481

502482
@property
503-
def find_links(self):
504-
# type: () -> List[str]
483+
def find_links(self) -> List[str]:
505484
return self.search_scope.find_links
506485

507-
def fetch_page(self, location):
508-
# type: (Link) -> Optional[HTMLPage]
486+
def fetch_page(self, location: Link) -> Optional[HTMLPage]:
509487
"""
510488
Fetch an HTML page containing package links.
511489
"""

0 commit comments

Comments
 (0)