Skip to content

Commit

Permalink
refactor(links_sources): use cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Sep 8, 2022
1 parent bc2753b commit f3065b9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 35 deletions.
14 changes: 13 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ python = "^3.7"

poetry-core = "^1.1.0"
poetry-plugin-export = "^1.0.6"
"backports.cached-property" = { version = "^1.0.2", python = "<3.8" }
cachecontrol = { version = "^0.12.9", extras = ["filecache"] }
cachy = "^0.3.0"
cleo = "^1.0.0a5"
Expand Down
9 changes: 0 additions & 9 deletions src/poetry/repositories/link_sources/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import functools
import logging
import re

Expand Down Expand Up @@ -40,9 +39,6 @@ class LinkSource:

def __init__(self, url: str) -> None:
self._url = url
self._get_link_cache_wrapper = functools.lru_cache(maxsize=1)(
self._get_link_cache
)

@property
def url(self) -> str:
Expand Down Expand Up @@ -124,10 +120,5 @@ def yanked(self, name: NormalizedName, version: Version) -> str | bool:
@property
def _link_cache(
self,
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
return self._get_link_cache_wrapper()

def _get_link_cache(
self,
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
raise NotImplementedError()
4 changes: 3 additions & 1 deletion src/poetry/repositories/link_sources/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from poetry.core.packages.utils.link import Link

from poetry.repositories.link_sources.base import LinkSource
from poetry.utils._compat import cached_property


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

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

def _get_link_cache(
@cached_property
def _link_cache(
self,
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
links: defaultdict[
Expand Down
16 changes: 15 additions & 1 deletion src/poetry/utils/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
else:
from importlib import metadata

if sys.version_info < (3, 8):
# compatibility for python <3.8
from backports.cached_property import cached_property
else:
from functools import cached_property

WINDOWS = sys.platform == "win32"


Expand Down Expand Up @@ -53,4 +59,12 @@ def list_to_shell_command(cmd: list[str]) -> str:
)


__all__ = ["WINDOWS", "decode", "encode", "list_to_shell_command", "metadata", "to_str"]
__all__ = [
"WINDOWS",
"cached_property",
"decode",
"encode",
"list_to_shell_command",
"metadata",
"to_str",
]
45 changes: 22 additions & 23 deletions tests/repositories/link_sources/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from collections import defaultdict
from typing import TYPE_CHECKING
from unittest.mock import PropertyMock

import pytest

from packaging.utils import NormalizedName
from packaging.utils import canonicalize_name
from poetry.core.packages.package import Package
from poetry.core.packages.utils.link import Link
Expand All @@ -23,28 +23,27 @@
@pytest.fixture
def link_source(mocker: MockerFixture) -> LinkSource:
url = "https://example.org"

class LinkSourceMock(LinkSource):
def _get_link_cache(
self,
) -> defaultdict[NormalizedName, defaultdict[Version, list[Link]]]:
return defaultdict(
lambda: defaultdict(list),
{
canonicalize_name("demo"): defaultdict(
list,
{
Version.parse("0.1.0"): [
Link(f"{url}/demo-0.1.0.tar.gz"),
Link(f"{url}/demo-0.1.0-py2.py3-none-any.whl"),
],
Version.parse("0.1.1"): [Link(f"{url}/demo-0.1.1.tar.gz")],
},
),
},
)

return LinkSourceMock(url)
link_source = LinkSource(url)
mocker.patch(
f"{LinkSource.__module__}.{LinkSource.__qualname__}._link_cache",
new_callable=PropertyMock,
return_value=defaultdict(
lambda: defaultdict(list),
{
canonicalize_name("demo"): defaultdict(
list,
{
Version.parse("0.1.0"): [
Link(f"{url}/demo-0.1.0.tar.gz"),
Link(f"{url}/demo-0.1.0-py2.py3-none-any.whl"),
],
Version.parse("0.1.1"): [Link(f"{url}/demo-0.1.1.tar.gz")],
},
),
},
),
)
return link_source


@pytest.mark.parametrize(
Expand Down

0 comments on commit f3065b9

Please sign in to comment.