Skip to content

Commit

Permalink
Ensure wrapped function is accessible in Cython versions
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Dec 1, 2024
1 parent ef6af70 commit e8287b4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/propcache/_helpers_c.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cdef class under_cached_property:
"""

cdef object wrapped
cdef readonly object wrapped
cdef object name

def __init__(self, wrapped):
Expand Down Expand Up @@ -47,16 +47,16 @@ cdef class cached_property:
"""

cdef object wrapped
cdef readonly object func
cdef object name

def __init__(self, wrapped):
self.wrapped = wrapped
def __init__(self, func):
self.func = func
self.name = None

@property
def __doc__(self):
return self.wrapped.__doc__
return self.func.__doc__

def __set_name__(self, owner, name):
if self.name is None:
Expand All @@ -77,7 +77,7 @@ cdef class cached_property:
cdef dict cache = inst.__dict__
val = cache.get(self.name, _sentinel)
if val is _sentinel:
val = self.wrapped(inst)
val = self.func(inst)
cache[self.name] = val
return val

Expand Down
16 changes: 16 additions & 0 deletions tests/test_cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,19 @@ class A:
match = r"Cannot use cached_property instance "
with pytest.raises(TypeError, match=match):
_ = A().cp # type: ignore[attr-defined]


def test_ensured_wrapped_function_is_accessible(propcache_module: APIProtocol) -> None:
"""Test that the wrapped function can be accessed from python."""

class A:
def __init__(self) -> None:
"""Init."""

@propcache_module.cached_property
def prop(self) -> int:
"""Docstring."""
return 1

a = A()
assert A.prop.func(a) == 1
17 changes: 17 additions & 0 deletions tests/test_under_cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,20 @@ def prop(self) -> Any:

assert isinstance(A.prop, propcache_module.under_cached_property)
assert "Docstring." == A.prop.__doc__


def test_ensured_wrapped_function_is_accessible(propcache_module: APIProtocol) -> None:
"""Test that the wrapped function can be accessed from python."""

class A:
def __init__(self) -> None:
"""Init."""
self._cache: dict[str, int] = {}

@propcache_module.under_cached_property
def prop(self) -> int:
"""Docstring."""
return 1

a = A()
assert A.prop.wrapped(a) == 1

0 comments on commit e8287b4

Please sign in to comment.