Skip to content

Commit a8a0d07

Browse files
committed
Move lru_cache to utils for reuse
1 parent 955daa9 commit a8a0d07

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/pip/_internal/index/collector.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from pip._internal.models.link import Link
2222
from pip._internal.models.search_scope import SearchScope
2323
from pip._internal.network.utils import raise_for_status
24+
from pip._internal.utils.compat import lru_cache
2425
from pip._internal.utils.filetypes import ARCHIVE_EXTENSIONS
2526
from pip._internal.utils.misc import pairwise, redact_auth_from_url
2627
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -36,10 +37,8 @@
3637
List,
3738
MutableMapping,
3839
Optional,
39-
Protocol,
4040
Sequence,
4141
Tuple,
42-
TypeVar,
4342
Union,
4443
)
4544

@@ -50,31 +49,10 @@
5049
HTMLElement = xml.etree.ElementTree.Element
5150
ResponseHeaders = MutableMapping[str, str]
5251

53-
# Used in the @lru_cache polyfill.
54-
F = TypeVar('F')
55-
56-
class LruCache(Protocol):
57-
def __call__(self, maxsize=None):
58-
# type: (Optional[int]) -> Callable[[F], F]
59-
raise NotImplementedError
60-
6152

6253
logger = logging.getLogger(__name__)
6354

6455

65-
# Fallback to noop_lru_cache in Python 2
66-
# TODO: this can be removed when python 2 support is dropped!
67-
def noop_lru_cache(maxsize=None):
68-
# type: (Optional[int]) -> Callable[[F], F]
69-
def _wrapper(f):
70-
# type: (F) -> F
71-
return f
72-
return _wrapper
73-
74-
75-
_lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache
76-
77-
7856
def _match_vcs_scheme(url):
7957
# type: (str) -> Optional[str]
8058
"""Look for VCS schemes in the URL.
@@ -344,7 +322,7 @@ def with_cached_html_pages(
344322
`page` has `page.cache_link_parsing == False`.
345323
"""
346324

347-
@_lru_cache(maxsize=None)
325+
@lru_cache(maxsize=None)
348326
def wrapper(cacheable_page):
349327
# type: (CacheablePageContent) -> List[Link]
350328
return list(fn(cacheable_page.page))

src/pip/_internal/utils/compat.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import absolute_import, division
88

99
import codecs
10+
import functools
1011
import locale
1112
import logging
1213
import os
@@ -18,7 +19,23 @@
1819
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
1920

2021
if MYPY_CHECK_RUNNING:
21-
from typing import Optional, Text, Tuple, Union
22+
from typing import (
23+
Callable,
24+
Optional,
25+
Protocol,
26+
Text,
27+
Tuple,
28+
TypeVar,
29+
Union,
30+
)
31+
32+
# Used in the @lru_cache polyfill.
33+
F = TypeVar('F')
34+
35+
class LruCache(Protocol):
36+
def __call__(self, maxsize=None):
37+
# type: (Optional[int]) -> Callable[[F], F]
38+
raise NotImplementedError
2239

2340
try:
2441
import ipaddress
@@ -269,3 +286,16 @@ def ioctl_GWINSZ(fd):
269286
if not cr:
270287
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
271288
return int(cr[1]), int(cr[0])
289+
290+
291+
# Fallback to noop_lru_cache in Python 2
292+
# TODO: this can be removed when python 2 support is dropped!
293+
def noop_lru_cache(maxsize=None):
294+
# type: (Optional[int]) -> Callable[[F], F]
295+
def _wrapper(f):
296+
# type: (F) -> F
297+
return f
298+
return _wrapper
299+
300+
301+
lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache

0 commit comments

Comments
 (0)