Skip to content

Commit

Permalink
Add missing coverage for __set_name__ (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 3, 2024
1 parent eaeba84 commit 6c0f300
Show file tree
Hide file tree
Showing 2 changed files with 31 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
30 changes: 30 additions & 0 deletions tests/test_cached_property.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import platform
from operator import not_

import pytest

Expand Down Expand Up @@ -102,3 +103,32 @@ 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:

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

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

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


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

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

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

0 comments on commit 6c0f300

Please sign in to comment.