Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion minique/models/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import time
from collections.abc import Callable
from functools import cached_property
from typing import TYPE_CHECKING, Any

from minique import encoding
Expand All @@ -15,7 +16,6 @@
MissingJobData,
NoSuchJob,
)
from minique.utils import cached_property

if TYPE_CHECKING:
from minique.models.priority_queue import PriorityQueue
Expand Down
2 changes: 1 addition & 1 deletion minique/models/queue.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from functools import cached_property
from typing import TYPE_CHECKING, Any

from minique.consts import QUEUE_KEY_PREFIX
from minique.excs import NoSuchJob
from minique.utils import cached_property
from minique.utils.redis_list import read_list

if TYPE_CHECKING:
Expand Down
37 changes: 16 additions & 21 deletions minique/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random
from collections.abc import Callable, Iterator
import warnings
from collections.abc import Iterator
from contextlib import contextmanager
from importlib import import_module
from threading import local
Expand Down Expand Up @@ -32,26 +33,6 @@ def get_current_job() -> Optional["Job"]:
return getattr(_current_jobs, "current_job", None)


class cached_property: # TODO: remove when py3.8+ only # noqa: N801
"""A property that is only computed once per instance and then replaces
itself with an ordinary attribute. Deleting the attribute resets the
property.

Source: https://github.com/bottlepy/bottle/blob/0.11.5/bottle.py#L175
"""

def __init__(self, func: Callable[..., Any]) -> None:
self.__doc__ = getattr(func, "__doc__", "")
self.func = func

def __get__(self, obj: Any, cls: Any) -> Any:
if obj is None:
# We're being accessed from the class itself, not from an object
return self
value = obj.__dict__[self.func.__name__] = self.func(obj)
return value


consonants = "cdgkklmmnnprst"
vowels = "aeiou"

Expand All @@ -62,3 +43,17 @@ def get_random_pronounceable_string(length: int = 12) -> str:
s.append(random.choice(consonants))
s.append(random.choice(vowels) * random.choice((1, 1, 2)))
return "".join(s)[:length]


def __getattr__(name: str) -> Any:
if name == "cached_property":
warnings.warn(
"minique.utils.cached_property is deprecated, use functools.cached_property instead",
DeprecationWarning,
stacklevel=2,
)

from functools import cached_property

return cached_property
raise AttributeError(f"module {__name__} has no attribute {name}")
12 changes: 12 additions & 0 deletions minique_tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def test_cached_property_import(recwarn):
"""
Test that importing cached_property from minique.utils works,
but raises a DeprecationWarning.
"""
from functools import cached_property as stdlib_cached_property

from minique.utils import cached_property

assert cached_property is stdlib_cached_property
depwarn = next(w for w in recwarn if issubclass(w.category, DeprecationWarning))
assert "minique.utils.cached_property is deprecated" in str(depwarn.message)