Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADCM-6143: Change cached_property to async_cached_property #18

Merged
merged 25 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
14a2004
ADCM-6115: Implement Service object
DanBalalan Nov 19, 2024
358a9ac
ADCM-6115: add dummy `cluster` cached property
DanBalalan Nov 19, 2024
fce0316
ADCM-6115: review fixes
DanBalalan Nov 19, 2024
79dc14f
ADCM-6115: remove todo
DanBalalan Nov 20, 2024
20a71c0
ADCM-6117: Implement Component object
DanBalalan Nov 20, 2024
d85f94e
Merge remote-tracking branch 'refs/remotes/origin/feature/ADCM-6064' …
DanBalalan Nov 20, 2024
3437117
ADCM-6115: after merge fixes
DanBalalan Nov 20, 2024
e5fd846
ADCM-6115: after merge fixes
DanBalalan Nov 20, 2024
262be35
ADCM-6115: after merge fixes
DanBalalan Nov 20, 2024
423adcc
ADCM-6115: after merge fixes
DanBalalan Nov 20, 2024
a9fc7bf
ADCM-6115: remove todo
DanBalalan Nov 20, 2024
ec8e0b5
Merge branch 'refs/heads/ADCM-6115' into ADCM-6117
DanBalalan Nov 20, 2024
5c455fd
ADCM-6117: fix name, display_name of Component
DanBalalan Nov 20, 2024
e66f9c7
ADCM-6117: review fixes
DanBalalan Nov 21, 2024
0e51382
Merge remote-tracking branch 'refs/remotes/origin/feature/ADCM-6064' …
DanBalalan Nov 21, 2024
510048f
ADCM-6133: Implement node for getting hosts
DanBalalan Nov 21, 2024
81c6d58
ADCM-6133: fix some cached_properties
DanBalalan Nov 22, 2024
56ba4a7
Revert "ADCM-6133: fix some cached_properties"
DanBalalan Nov 22, 2024
e5a7279
Merge remote-tracking branch 'refs/remotes/origin/feature/ADCM-6064' …
DanBalalan Nov 22, 2024
67796a3
ADCM-6133: restore poetry.lock and pyproject.toml
DanBalalan Nov 22, 2024
6b03292
ADCM-6133: review fixes
DanBalalan Nov 22, 2024
e6cf4b7
ADCM-6133: review fixes
DanBalalan Nov 22, 2024
2f81263
ADCM-6133: review fixes
DanBalalan Nov 25, 2024
6a53183
ADCM-6143: Change `cached_property` to `async_cached_property` for ex…
DanBalalan Nov 25, 2024
2db4676
Merge remote-tracking branch 'refs/remotes/origin/feature/ADCM-6064' …
DanBalalan Nov 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion adcm_aio_client/core/objects/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from functools import cached_property
from typing import Any, Self

from asyncstdlib.functools import CachedProperty

from adcm_aio_client.core.requesters import Requester
from adcm_aio_client.core.types import AwareOfOwnPath, Endpoint, WithRequester

Expand All @@ -18,7 +20,7 @@ def __init_subclass__(cls: type[Self]) -> None:
for name in dir(cls):
# None is for declared, but unset values
attr = getattr(cls, name, None)
if isinstance(attr, cached_property): # TODO: asyncstdlib.functools.CachedProperty
if isinstance(attr, (cached_property, CachedProperty)):
cls._delete_on_refresh.append(name)

def __init__(self: Self, requester: Requester, data: dict[str, Any]) -> None:
Expand Down
12 changes: 7 additions & 5 deletions adcm_aio_client/core/objects/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from typing import Iterable, Self
import asyncio

from asyncstdlib.functools import cached_property as async_cached_property

from adcm_aio_client.core.errors import NotFoundError, OperationError, ResponseError
from adcm_aio_client.core.objects._accessors import (
PaginatedAccessor,
Expand Down Expand Up @@ -29,7 +31,7 @@ class ADCM(InteractiveObject, WithActions, WithConfig):
def id(self: Self) -> int:
return 1

@cached_property
@async_cached_property
async def version(self: Self) -> str:
# TODO: override root_path for being without /api/v2
response = await self._requester.get("versions")
Expand Down Expand Up @@ -68,7 +70,7 @@ def description(self: Self) -> str:
# todo think how such properties will be invalidated when data is updated
# during `refresh()` / `reread()` calls.
# See cache invalidation or alternatives in documentation for `cached_property`
@cached_property # TODO: replace with asyncstdlib.functools.cached_property
@async_cached_property
async def bundle(self: Self) -> Bundle:
prototype_id = self._data["prototype"]["id"]
response = await self._requester.get("prototypes", prototype_id)
Expand Down Expand Up @@ -162,7 +164,7 @@ def name(self: Self) -> str:
def display_name(self: Self) -> str:
return self._data["displayName"]

@cached_property # TODO: replace with asyncstdlib.functools.cached_property
@async_cached_property
async def constraint(self: Self) -> list[int | str]:
response = (await self._requester.get(*self.cluster.get_own_path(), "mapping", "components")).as_list()
for component in response:
Expand Down Expand Up @@ -240,13 +242,13 @@ async def get_status(self: Self) -> ADCMEntityStatus:
response = await self._requester.get(*self.get_own_path())
return ADCMEntityStatus(response.as_dict()["status"])

@cached_property # TODO: replace with asyncstdlib.functools.cached_property
@async_cached_property
async def cluster(self: Self) -> Cluster | None:
if not self._data["cluster"]:
return None
return await Cluster.with_id(requester=self._requester, object_id=self._data["cluster"]["id"])

@cached_property # TODO: replace with asyncstdlib.functools.cached_property
@async_cached_property
async def hostprovider(self: Self) -> HostProvider:
return await HostProvider.with_id(requester=self._requester, object_id=self._data["hostprovider"]["id"])

Expand Down
18 changes: 17 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 @@ -9,6 +9,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
httpx = "^0.27.2"
asyncstdlib = "^3.13.0"

[tool.poetry.group.dev]
optional = true
Expand Down
34 changes: 34 additions & 0 deletions tests/integration/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Self

from asyncstdlib.functools import cached_property as async_cached_property
import pytest

pytestmark = [pytest.mark.asyncio]


class Dummy:
def __init__(self: Self) -> None:
self.counter = 0

@async_cached_property
async def func(self: Self) -> int:
self.counter += 1

return self.counter


async def test_async_cached_property() -> None:
obj = Dummy()
assert "func" not in obj.__dict__, "`func` key should not be cached yet"

res = await obj.func
assert res == 1
assert "func" in obj.__dict__, "`func` key should be cached"

res = await obj.func
assert res == 1, "Cached value must be used"

delattr(obj, "func")
res = await obj.func
assert res == 2, "Expected to execute func() again, increasing the counter"
assert "func" in obj.__dict__