Skip to content

Commit ec89cad

Browse files
committed
Move lru_cache shim to utils.compat
1 parent 334f06e commit ec89cad

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

src/pip/_internal/index/collector.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
from pip._internal.models.link import Link
2121
from pip._internal.models.search_scope import SearchScope
22+
from pip._internal.utils.compat import lru_cache
2223
from pip._internal.utils.filetypes import ARCHIVE_EXTENSIONS
2324
from pip._internal.utils.misc import pairwise, redact_auth_from_url
2425
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
@@ -29,7 +30,7 @@
2930
from optparse import Values
3031
from typing import (
3132
Callable, Iterable, List, MutableMapping, Optional,
32-
Protocol, Sequence, Tuple, TypeVar, Union,
33+
Sequence, Tuple, Union,
3334
)
3435
import xml.etree.ElementTree
3536

@@ -40,31 +41,10 @@
4041
HTMLElement = xml.etree.ElementTree.Element
4142
ResponseHeaders = MutableMapping[str, str]
4243

43-
# Used in the @lru_cache polyfill.
44-
F = TypeVar('F')
45-
46-
class LruCache(Protocol):
47-
def __call__(self, maxsize=None):
48-
# type: (Optional[int]) -> Callable[[F], F]
49-
raise NotImplementedError
50-
5144

5245
logger = logging.getLogger(__name__)
5346

5447

55-
# Fallback to noop_lru_cache in Python 2
56-
# TODO: this can be removed when python 2 support is dropped!
57-
def noop_lru_cache(maxsize=None):
58-
# type: (Optional[int]) -> Callable[[F], F]
59-
def _wrapper(f):
60-
# type: (F) -> F
61-
return f
62-
return _wrapper
63-
64-
65-
_lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache
66-
67-
6848
def _match_vcs_scheme(url):
6949
# type: (str) -> Optional[str]
7050
"""Look for VCS schemes in the URL.
@@ -334,7 +314,7 @@ def with_cached_html_pages(
334314
`page` has `page.cache_link_parsing == False`.
335315
"""
336316

337-
@_lru_cache(maxsize=None)
317+
@lru_cache(maxsize=None)
338318
def wrapper(cacheable_page):
339319
# type: (CacheablePageContent) -> List[Link]
340320
return list(fn(cacheable_page.page))

src/pip/_internal/utils/compat.py

Lines changed: 25 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,17 @@
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, Optional, Protocol, Text, Tuple, TypeVar, Union,
24+
)
25+
26+
# Used in the @lru_cache polyfill.
27+
F = TypeVar('F')
28+
29+
class LruCache(Protocol):
30+
def __call__(self, maxsize=None):
31+
# type: (Optional[int]) -> Callable[[F], F]
32+
raise NotImplementedError
2233

2334
try:
2435
import ipaddress
@@ -34,6 +45,7 @@
3445
__all__ = [
3546
"ipaddress", "uses_pycache", "console_to_str",
3647
"get_path_uid", "stdlib_pkgs", "WINDOWS", "samefile", "get_terminal_size",
48+
"lru_cache",
3749
]
3850

3951

@@ -268,3 +280,15 @@ def ioctl_GWINSZ(fd):
268280
if not cr:
269281
cr = (os.environ.get('LINES', 25), os.environ.get('COLUMNS', 80))
270282
return int(cr[1]), int(cr[0])
283+
284+
285+
# Fallback to noop_lru_cache in Python 2
286+
def noop_lru_cache(maxsize=None):
287+
# type: (Optional[int]) -> Callable[[F], F]
288+
def _wrapper(f):
289+
# type: (F) -> F
290+
return f
291+
return _wrapper
292+
293+
294+
lru_cache = getattr(functools, "lru_cache", noop_lru_cache) # type: LruCache

0 commit comments

Comments
 (0)