Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure wrapped function is accessible in Cython versions #72

Merged
merged 2 commits into from
Dec 1, 2024
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
1 change: 1 addition & 0 deletions CHANGES/72.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``wrapped`` and ``func`` not being accessible in the Cython versions of :func:`propcache.api.cached_property` and :func:`propcache.api.under_cached_property` decorators -- by :user:`bdraco`.
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
Loading