Skip to content

Commit

Permalink
Add missing coverage for __set_name__
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 3, 2024
1 parent 0ab05e4 commit 8ce6faa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion propcache/_helpers_c.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ cdef class cached_property:
if self.name is None:
raise TypeError(
"Cannot use cached_property instance"
"without calling __set_name__ on it.")
" without calling __set_name__ on it.")
cdef dict cache = inst.__dict__
val = cache.get(self.name, _sentinel)
if val is _sentinel:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_cached_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,45 @@ class TestPyCachedProperty(CachedPropertyMixin):

class TestCCachedProperty(CachedPropertyMixin):
cached_property = _helpers.cached_property_c # type: ignore[assignment, attr-defined, unused-ignore] # noqa: E501


def test_set_name():
"""Test that the __set_name__ method is called and checked."""

class A:
def __init__(self):
self._cache = {}

@cached_property
def prop(self):
"""Docstring."""
return 1

A.prop.__set_name__(A, "prop")

with pytest.raises(
TypeError,
match=(
r"Cannot assign the same cached_property to two "
r"different names \('prop' and 'something_else'\)."
),
):
A.prop.__set_name__(A, "something_else")


def test_get_without_set_name():
"""Test that get without __set_name__ fails."""
cp = cached_property(lambda s: None)

class A:
"""A class."""

A.cp = cp
with pytest.raises(
TypeError,
match=(
r"Cannot use cached_property instance "
r"without calling __set_name__ on it."
),
):
_ = A().cp

0 comments on commit 8ce6faa

Please sign in to comment.