Skip to content

Commit 738723c

Browse files
committed
refactor(links_sources): use cached_property
1 parent bc2753b commit 738723c

File tree

6 files changed

+56
-36
lines changed

6 files changed

+56
-36
lines changed

poetry.lock

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ python = "^3.7"
4646

4747
poetry-core = "^1.1.0"
4848
poetry-plugin-export = "^1.0.6"
49+
"backports.cached-property" = { version = "^1.0.2", python = "<3.8" }
4950
cachecontrol = { version = "^0.12.9", extras = ["filecache"] }
5051
cachy = "^0.3.0"
5152
cleo = "^1.0.0a5"

src/poetry/repositories/link_sources/base.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import functools
43
import logging
54
import re
65

@@ -10,6 +9,7 @@
109
from poetry.core.packages.package import Package
1110
from poetry.core.semver.version import Version
1211

12+
from poetry.utils._compat import cached_property
1313
from poetry.utils.patterns import sdist_file_re
1414
from poetry.utils.patterns import wheel_file_re
1515

@@ -40,9 +40,6 @@ class LinkSource:
4040

4141
def __init__(self, url: str) -> None:
4242
self._url = url
43-
self._get_link_cache_wrapper = functools.lru_cache(maxsize=1)(
44-
self._get_link_cache
45-
)
4643

4744
@property
4845
def url(self) -> str:
@@ -121,13 +118,8 @@ def yanked(self, name: NormalizedName, version: Version) -> str | bool:
121118
return "\n".join(sorted(reasons))
122119
return True
123120

124-
@property
121+
@cached_property
125122
def _link_cache(
126123
self,
127-
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
128-
return self._get_link_cache_wrapper()
129-
130-
def _get_link_cache(
131-
self,
132124
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
133125
raise NotImplementedError()

src/poetry/repositories/link_sources/html.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from poetry.core.packages.utils.link import Link
1111

1212
from poetry.repositories.link_sources.base import LinkSource
13+
from poetry.utils._compat import cached_property
1314

1415

1516
if TYPE_CHECKING:
@@ -28,7 +29,8 @@ def __init__(self, url: str, content: str) -> None:
2829

2930
self._parsed = html5lib.parse(content, namespaceHTMLElements=False)
3031

31-
def _get_link_cache(
32+
@cached_property
33+
def _link_cache(
3234
self,
3335
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
3436
links: defaultdict[

src/poetry/utils/_compat.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
else:
1414
from importlib import metadata
1515

16+
if sys.version_info < (3, 8):
17+
# compatibility for python <3.8
18+
from backports.cached_property import cached_property
19+
else:
20+
from functools import cached_property
21+
1622
WINDOWS = sys.platform == "win32"
1723

1824

@@ -53,4 +59,12 @@ def list_to_shell_command(cmd: list[str]) -> str:
5359
)
5460

5561

56-
__all__ = ["WINDOWS", "decode", "encode", "list_to_shell_command", "metadata", "to_str"]
62+
__all__ = [
63+
"WINDOWS",
64+
"cached_property",
65+
"decode",
66+
"encode",
67+
"list_to_shell_command",
68+
"metadata",
69+
"to_str",
70+
]

tests/repositories/link_sources/test_base.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
from collections import defaultdict
44
from typing import TYPE_CHECKING
5+
from unittest.mock import PropertyMock
56

67
import pytest
78

8-
from packaging.utils import NormalizedName
99
from packaging.utils import canonicalize_name
1010
from poetry.core.packages.package import Package
1111
from poetry.core.packages.utils.link import Link
@@ -23,28 +23,27 @@
2323
@pytest.fixture
2424
def link_source(mocker: MockerFixture) -> LinkSource:
2525
url = "https://example.org"
26-
27-
class LinkSourceMock(LinkSource):
28-
def _get_link_cache(
29-
self,
30-
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
31-
return defaultdict(
32-
lambda: defaultdict(list),
33-
{
34-
canonicalize_name("demo"): defaultdict(
35-
list,
36-
{
37-
Version.parse("0.1.0"): [
38-
Link(f"{url}/demo-0.1.0.tar.gz"),
39-
Link(f"{url}/demo-0.1.0-py2.py3-none-any.whl"),
40-
],
41-
Version.parse("0.1.1"): [Link(f"{url}/demo-0.1.1.tar.gz")],
42-
},
43-
),
44-
},
45-
)
46-
47-
return LinkSourceMock(url)
26+
link_source = LinkSource(url)
27+
mocker.patch(
28+
f"{LinkSource.__module__}.{LinkSource.__qualname__}._link_cache",
29+
new_callable=PropertyMock,
30+
return_value=defaultdict(
31+
lambda: defaultdict(list),
32+
{
33+
canonicalize_name("demo"): defaultdict(
34+
list,
35+
{
36+
Version.parse("0.1.0"): [
37+
Link(f"{url}/demo-0.1.0.tar.gz"),
38+
Link(f"{url}/demo-0.1.0-py2.py3-none-any.whl"),
39+
],
40+
Version.parse("0.1.1"): [Link(f"{url}/demo-0.1.1.tar.gz")],
41+
},
42+
),
43+
},
44+
),
45+
)
46+
return link_source
4847

4948

5049
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)