From 7d626fbfa0ff2078926c57580a1d7b0d2fbf93f2 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 26 Mar 2018 11:45:58 +0200 Subject: [PATCH] BUG: raise error when setting cached properties --- pandas/_libs/properties.pyx | 3 +++ pandas/tests/indexes/test_base.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/pandas/_libs/properties.pyx b/pandas/_libs/properties.pyx index e3f16f224db1c..0f2900619fdb6 100644 --- a/pandas/_libs/properties.pyx +++ b/pandas/_libs/properties.pyx @@ -37,6 +37,9 @@ cdef class CachedProperty(object): PyDict_SetItem(cache, self.name, val) return val + def __set__(self, obj, value): + raise AttributeError("Can't set attribute") + cache_readonly = CachedProperty diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 22ef2fe7aa19e..ff9c86fbfe384 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2056,6 +2056,11 @@ def test_iadd_preserves_name(self): ser.index -= 1 assert ser.index.name == "foo" + def test_cached_properties_not_settable(self): + idx = pd.Index([1, 2, 3]) + with tm.assert_raises_regex(AttributeError, "Can't set attribute"): + idx.is_unique = False + class TestMixedIntIndex(Base): # Mostly the tests from common.py for which the results differ