Title: Implement setter and deleter on functools.cached_property
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Epic_Wink
Priority: normal Keywords: patch

Created on 2019-10-21 10:31 by Epic_Wink, last changed 2019-10-21 11:32 by Epic_Wink. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16872 closed Epic_Wink, 2019-10-21 11:15
Messages (2)
msg355045 - (view) Author: Laurie Opperman (Epic_Wink) * Date: 2019-10-21 10:31
Support setting/updating and clearing the cached value of a cached-property (getter implemented by bpo-21145). Pretty straight-forward to implement

The question is whether cached-property updating should be:
a) always allowed
b) enabled with a cached-property instance attribute
c) enabled with the decorated method's class attribute
d) enabled with the decorated method's instance attribute
e) enabled in a separate decorator (eg functools.updatable_cached_property)

Note: options a, b, c, and d will make functools.cached_property a data descriptor.

I prefer option b or c, as the decision to make the property updatable becomes the property's definer's.

Also, should a warning be raised if a cached value is being overwritten? Should a warning be raised if no cached value is to be deleted?
msg355056 - (view) Author: Laurie Opperman (Epic_Wink) * Date: 2019-10-21 11:32
Turns out, that as a non-data descriptor, a cached property already has setting/updating and clearing through the normal mechanisms in Python. This feature request is therefore redundant: perhaps a new issue to document this inherent behaviour?

Unless you explicitly want to make cached property updating not allowed, but that's easily implemented in application code by sub-classing `functools.cached_property` and defining `__set__` and `__delete__` to raise:

    class unupdatable_cached_property(functools.cached_property):
        def __set__(self, instance, value):
            raise AttributeError("can't set attribute")

        def __delete__(self, instance):
            raise AttributeError("can't delete attribute")
Date User Action Args
2019-10-21 11:32:35Epic_Winksetstatus: open -> closed
resolution: rejected
messages: + msg355056

stage: patch review -> resolved
2019-10-21 11:15:30Epic_Winksetkeywords: + patch
stage: patch review
pull_requests: + pull_request16417
2019-10-21 10:31:01Epic_Winkcreate