From e8287b4b23d6e5e866c8d4113b53d69f0807d786 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 1 Dec 2024 09:36:43 -0600 Subject: [PATCH] Ensure wrapped function is accessible in Cython versions --- src/propcache/_helpers_c.pyx | 12 ++++++------ tests/test_cached_property.py | 16 ++++++++++++++++ tests/test_under_cached_property.py | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/propcache/_helpers_c.pyx b/src/propcache/_helpers_c.pyx index 5369e12..0c42ff3 100644 --- a/src/propcache/_helpers_c.pyx +++ b/src/propcache/_helpers_c.pyx @@ -13,7 +13,7 @@ cdef class under_cached_property: """ - cdef object wrapped + cdef readonly object wrapped cdef object name def __init__(self, wrapped): @@ -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: @@ -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 diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index a02b023..06c429d 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -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 diff --git a/tests/test_under_cached_property.py b/tests/test_under_cached_property.py index 3e9e9d0..2cf2d6d 100644 --- a/tests/test_under_cached_property.py +++ b/tests/test_under_cached_property.py @@ -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