first commit
This commit is contained in:
32
venv/lib/python3.12/site-packages/propcache/__init__.py
Normal file
32
venv/lib/python3.12/site-packages/propcache/__init__.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""propcache: An accelerated property cache for Python classes."""
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
_PUBLIC_API = ("cached_property", "under_cached_property")
|
||||
|
||||
__version__ = "0.4.1"
|
||||
__all__ = ()
|
||||
|
||||
# Imports have moved to `propcache.api` in 0.2.0+.
|
||||
# This module is now a facade for the API.
|
||||
if TYPE_CHECKING:
|
||||
from .api import cached_property as cached_property # noqa: F401
|
||||
from .api import under_cached_property as under_cached_property # noqa: F401
|
||||
|
||||
|
||||
def _import_facade(attr: str) -> object:
|
||||
"""Import the public API from the `api` module."""
|
||||
if attr in _PUBLIC_API:
|
||||
from . import api # pylint: disable=import-outside-toplevel
|
||||
|
||||
return getattr(api, attr)
|
||||
raise AttributeError(f"module '{__package__}' has no attribute '{attr}'")
|
||||
|
||||
|
||||
def _dir_facade() -> list[str]:
|
||||
"""Include the public API in the module's dir() output."""
|
||||
return [*_PUBLIC_API, *globals().keys()]
|
||||
|
||||
|
||||
__getattr__ = _import_facade
|
||||
__dir__ = _dir_facade
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
39
venv/lib/python3.12/site-packages/propcache/_helpers.py
Normal file
39
venv/lib/python3.12/site-packages/propcache/_helpers.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import os
|
||||
import sys
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
__all__ = ("cached_property", "under_cached_property")
|
||||
|
||||
|
||||
NO_EXTENSIONS = bool(os.environ.get("PROPCACHE_NO_EXTENSIONS")) # type: bool
|
||||
if sys.implementation.name != "cpython":
|
||||
NO_EXTENSIONS = True
|
||||
|
||||
|
||||
# isort: off
|
||||
if TYPE_CHECKING:
|
||||
from ._helpers_py import cached_property as cached_property_py
|
||||
from ._helpers_py import under_cached_property as under_cached_property_py
|
||||
|
||||
cached_property = cached_property_py
|
||||
under_cached_property = under_cached_property_py
|
||||
elif not NO_EXTENSIONS: # pragma: no branch
|
||||
try:
|
||||
from ._helpers_c import cached_property as cached_property_c # type: ignore[attr-defined, unused-ignore]
|
||||
from ._helpers_c import under_cached_property as under_cached_property_c # type: ignore[attr-defined, unused-ignore]
|
||||
|
||||
cached_property = cached_property_c
|
||||
under_cached_property = under_cached_property_c
|
||||
except ImportError: # pragma: no cover
|
||||
from ._helpers_py import cached_property as cached_property_py
|
||||
from ._helpers_py import under_cached_property as under_cached_property_py
|
||||
|
||||
cached_property = cached_property_py # type: ignore[assignment, misc]
|
||||
under_cached_property = under_cached_property_py
|
||||
else:
|
||||
from ._helpers_py import cached_property as cached_property_py
|
||||
from ._helpers_py import under_cached_property as under_cached_property_py
|
||||
|
||||
cached_property = cached_property_py # type: ignore[assignment, misc]
|
||||
under_cached_property = under_cached_property_py
|
||||
# isort: on
|
||||
Binary file not shown.
103
venv/lib/python3.12/site-packages/propcache/_helpers_c.pyx
Normal file
103
venv/lib/python3.12/site-packages/propcache/_helpers_c.pyx
Normal file
@@ -0,0 +1,103 @@
|
||||
# cython: language_level=3, freethreading_compatible=True
|
||||
from types import GenericAlias
|
||||
|
||||
from cpython.dict cimport PyDict_GetItem
|
||||
from cpython.object cimport PyObject
|
||||
|
||||
|
||||
cdef extern from "Python.h":
|
||||
# Call a callable Python object callable with exactly
|
||||
# 1 positional argument arg and no keyword arguments.
|
||||
# Return the result of the call on success, or raise
|
||||
# an exception and return NULL on failure.
|
||||
PyObject* PyObject_CallOneArg(
|
||||
object callable, object arg
|
||||
) except NULL
|
||||
int PyDict_SetItem(
|
||||
object dict, object key, PyObject* value
|
||||
) except -1
|
||||
void Py_DECREF(PyObject*)
|
||||
|
||||
|
||||
cdef class under_cached_property:
|
||||
"""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.
|
||||
|
||||
"""
|
||||
|
||||
cdef readonly object wrapped
|
||||
cdef object name
|
||||
|
||||
def __init__(self, object wrapped):
|
||||
self.wrapped = wrapped
|
||||
self.name = wrapped.__name__
|
||||
|
||||
@property
|
||||
def __doc__(self):
|
||||
return self.wrapped.__doc__
|
||||
|
||||
def __get__(self, object inst, owner):
|
||||
if inst is None:
|
||||
return self
|
||||
cdef dict cache = inst._cache
|
||||
cdef PyObject* val = PyDict_GetItem(cache, self.name)
|
||||
if val == NULL:
|
||||
val = PyObject_CallOneArg(self.wrapped, inst)
|
||||
PyDict_SetItem(cache, self.name, val)
|
||||
Py_DECREF(val)
|
||||
return <object>val
|
||||
|
||||
def __set__(self, inst, value):
|
||||
raise AttributeError("cached property is read-only")
|
||||
|
||||
__class_getitem__ = classmethod(GenericAlias)
|
||||
|
||||
|
||||
cdef class cached_property:
|
||||
"""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.
|
||||
|
||||
"""
|
||||
|
||||
cdef readonly object func
|
||||
cdef object name
|
||||
|
||||
def __init__(self, func):
|
||||
self.func = func
|
||||
self.name = None
|
||||
|
||||
@property
|
||||
def __doc__(self):
|
||||
return self.func.__doc__
|
||||
|
||||
def __set_name__(self, owner, object name):
|
||||
if self.name is None:
|
||||
self.name = name
|
||||
elif name != self.name:
|
||||
raise TypeError(
|
||||
"Cannot assign the same cached_property to two different names "
|
||||
f"({self.name!r} and {name!r})."
|
||||
)
|
||||
|
||||
def __get__(self, inst, owner):
|
||||
if inst is None:
|
||||
return self
|
||||
if self.name is None:
|
||||
raise TypeError(
|
||||
"Cannot use cached_property instance"
|
||||
" without calling __set_name__ on it.")
|
||||
cdef dict cache = inst.__dict__
|
||||
cdef PyObject* val = PyDict_GetItem(cache, self.name)
|
||||
if val is NULL:
|
||||
val = PyObject_CallOneArg(self.func, inst)
|
||||
PyDict_SetItem(cache, self.name, val)
|
||||
Py_DECREF(val)
|
||||
return <object>val
|
||||
|
||||
__class_getitem__ = classmethod(GenericAlias)
|
||||
62
venv/lib/python3.12/site-packages/propcache/_helpers_py.py
Normal file
62
venv/lib/python3.12/site-packages/propcache/_helpers_py.py
Normal file
@@ -0,0 +1,62 @@
|
||||
"""Various helper functions."""
|
||||
|
||||
import sys
|
||||
from collections.abc import Mapping
|
||||
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")
|
||||
# We use Mapping to make it possible to use TypedDict, but this isn't
|
||||
# technically type safe as we need to assign into the dict.
|
||||
_Cache = TypeVar("_Cache", bound=Mapping[str, Any])
|
||||
|
||||
|
||||
class _CacheImpl(Protocol[_Cache]):
|
||||
_cache: _Cache
|
||||
|
||||
|
||||
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[[Any], _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: _CacheImpl[Any], owner: Optional[type[object]] = None
|
||||
) -> _T: ...
|
||||
|
||||
def __get__(
|
||||
self, inst: Optional[_CacheImpl[Any]], owner: Optional[type[object]] = None
|
||||
) -> Union[_T, Self]:
|
||||
if inst is None:
|
||||
return self
|
||||
try:
|
||||
return inst._cache[self.name] # type: ignore[no-any-return]
|
||||
except KeyError:
|
||||
val = self.wrapped(inst)
|
||||
inst._cache[self.name] = val
|
||||
return val
|
||||
|
||||
def __set__(self, inst: _CacheImpl[Any], value: _T) -> None:
|
||||
raise AttributeError("cached property is read-only")
|
||||
8
venv/lib/python3.12/site-packages/propcache/api.py
Normal file
8
venv/lib/python3.12/site-packages/propcache/api.py
Normal file
@@ -0,0 +1,8 @@
|
||||
"""Public API of the property caching library."""
|
||||
|
||||
from ._helpers import cached_property, under_cached_property
|
||||
|
||||
__all__ = (
|
||||
"cached_property",
|
||||
"under_cached_property",
|
||||
)
|
||||
1
venv/lib/python3.12/site-packages/propcache/py.typed
Normal file
1
venv/lib/python3.12/site-packages/propcache/py.typed
Normal file
@@ -0,0 +1 @@
|
||||
# Placeholder
|
||||
Reference in New Issue
Block a user