Skip to content

Commit db82165

Browse files
CLN/DOC: cache_readonly: remove allow_setting + preserve docstring (pandas-dev#19991)
1 parent e02f737 commit db82165

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

pandas/_libs/properties.pyx

+10-24
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,28 @@ from cpython cimport (
66
PyDict_Contains, PyDict_GetItem, PyDict_SetItem)
77

88

9-
cdef class cache_readonly(object):
9+
cdef class CachedProperty(object):
1010

1111
cdef readonly:
12-
object func, name, allow_setting
12+
object func, name, __doc__
1313

14-
def __init__(self, func=None, allow_setting=False):
15-
if func is not None:
16-
self.func = func
17-
self.name = func.__name__
18-
self.allow_setting = allow_setting
19-
20-
def __call__(self, func, doc=None):
14+
def __init__(self, func):
2115
self.func = func
2216
self.name = func.__name__
23-
return self
17+
self.__doc__ = getattr(func, '__doc__', None)
2418

2519
def __get__(self, obj, typ):
26-
# Get the cache or set a default one if needed
20+
if obj is None:
21+
# accessed on the class, not the instance
22+
return self
2723

24+
# Get the cache or set a default one if needed
2825
cache = getattr(obj, '_cache', None)
2926
if cache is None:
3027
try:
3128
cache = obj._cache = {}
3229
except (AttributeError):
33-
return
30+
return self
3431

3532
if PyDict_Contains(cache, self.name):
3633
# not necessary to Py_INCREF
@@ -40,20 +37,9 @@ cdef class cache_readonly(object):
4037
PyDict_SetItem(cache, self.name, val)
4138
return val
4239

43-
def __set__(self, obj, value):
44-
45-
if not self.allow_setting:
46-
raise Exception("cannot set values for [%s]" % self.name)
4740

48-
# Get the cache or set a default one if needed
49-
cache = getattr(obj, '_cache', None)
50-
if cache is None:
51-
try:
52-
cache = obj._cache = {}
53-
except (AttributeError):
54-
return
41+
cache_readonly = CachedProperty
5542

56-
PyDict_SetItem(cache, self.name, value)
5743

5844
cdef class AxisProperty(object):
5945
cdef:

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ def _is_strictly_monotonic_decreasing(self):
14011401
def is_lexsorted_for_tuple(self, tup):
14021402
return True
14031403

1404-
@cache_readonly(allow_setting=True)
1404+
@cache_readonly
14051405
def is_unique(self):
14061406
""" return if the index has unique values """
14071407
return self._engine.is_unique

pandas/tests/indexes/test_base.py

-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,6 @@ def test_is_(self):
519519
assert not ind.is_(ind.copy())
520520
assert not ind.is_(ind.copy(deep=False))
521521
assert not ind.is_(ind[:])
522-
assert not ind.is_(ind.view(np.ndarray).view(Index))
523522
assert not ind.is_(np.array(range(10)))
524523

525524
# quasi-implementation dependent

pandas/tests/test_lib.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
import numpy as np
6+
from pandas import Index
67
from pandas._libs import lib, writers as libwriters
78
import pandas.util.testing as tm
89

@@ -198,3 +199,8 @@ def test_get_reverse_indexer(self):
198199
result = lib.get_reverse_indexer(indexer, 5)
199200
expected = np.array([4, 2, 3, 6, 7], dtype=np.int64)
200201
tm.assert_numpy_array_equal(result, expected)
202+
203+
204+
def test_cache_readonly_preserve_docstrings():
205+
# GH18197
206+
assert Index.hasnans.__doc__ is not None

0 commit comments

Comments
 (0)