Skip to content

Commit

Permalink
ResourcesT: use weak reference for client property
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Nov 25, 2023
1 parent 019623e commit bd74c43
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion zammadoo/resources.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import weakref
from dataclasses import asdict
from functools import cached_property, partial
from typing import (
Expand Down Expand Up @@ -39,7 +40,7 @@ class ResourcesT(Generic[_T_co]):
"""

def __init__(self, client: "Client", endpoint: str):
self.client = client
self._client = weakref.ref(client)
self.endpoint: str = endpoint
self.cache = LruCache["JsonDict"](
max_size=self.DEFAULT_CACHE_SIZE
Expand All @@ -57,6 +58,12 @@ def __call__(self, rid: int, *, info: Optional["JsonDict"] = None) -> _T_co:
def __repr__(self):
return f"<{self.__class__.__qualname__} {self.url!r}>"

@property
def client(self) -> "Client":
client = self._client()
assert client is not None, "missing client reference"
return client

@cached_property
def url(self) -> str:
"""the resource's API URL"""
Expand Down

0 comments on commit bd74c43

Please sign in to comment.