diff options
author | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
---|---|---|
committer | S. Solomon Darnell | 2025-03-28 21:52:21 -0500 |
commit | 4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch) | |
tree | ee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/propcache/_helpers_py.py | |
parent | cc961e04ba734dd72309fb548a2f97d67d578813 (diff) | |
download | gn-ai-master.tar.gz |
Diffstat (limited to '.venv/lib/python3.12/site-packages/propcache/_helpers_py.py')
-rw-r--r-- | .venv/lib/python3.12/site-packages/propcache/_helpers_py.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/propcache/_helpers_py.py b/.venv/lib/python3.12/site-packages/propcache/_helpers_py.py new file mode 100644 index 00000000..2f3e6880 --- /dev/null +++ b/.venv/lib/python3.12/site-packages/propcache/_helpers_py.py @@ -0,0 +1,56 @@ +"""Various helper functions.""" + +import sys +from functools import cached_property +from typing import Any, Callable, Generic, Optional, Protocol, TypeVar, Union, overload + +__all__ = ("under_cached_property", "cached_property") + + +if sys.version_info >= (3, 11): + from typing import Self +else: + Self = Any + +_T = TypeVar("_T") + + +class _TSelf(Protocol, Generic[_T]): + _cache: dict[str, _T] + + +class under_cached_property(Generic[_T]): + """Use as a class method decorator. + + It operates almost exactly like + the Python `@property` decorator, but it puts the result of the + method it decorates into the instance dict after the first call, + effectively replacing the function it decorates with an instance + variable. It is, in Python parlance, a data descriptor. + """ + + def __init__(self, wrapped: Callable[..., _T]) -> None: + self.wrapped = wrapped + self.__doc__ = wrapped.__doc__ + self.name = wrapped.__name__ + + @overload + def __get__(self, inst: None, owner: Optional[type[object]] = None) -> Self: ... + + @overload + def __get__(self, inst: _TSelf[_T], owner: Optional[type[object]] = None) -> _T: ... + + def __get__( + self, inst: Optional[_TSelf[_T]], owner: Optional[type[object]] = None + ) -> Union[_T, Self]: + if inst is None: + return self + try: + return inst._cache[self.name] + except KeyError: + val = self.wrapped(inst) + inst._cache[self.name] = val + return val + + def __set__(self, inst: _TSelf[_T], value: _T) -> None: + raise AttributeError("cached property is read-only") |