This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Document functools.cached_property supports value updating and clearing
Type: enhancement Stage:
Components: Documentation, Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Epic_Wink, docs@python, hongweipeng
Priority: normal Keywords:

Created on 2019-10-21 23:53 by Epic_Wink, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg355110 - (view) Author: Laurie Opperman (Epic_Wink) * Date: 2019-10-21 23:53
As I discovered during working on bpo-38545, `functools.cached_property` natively supports cached value setting/updating and clearing (via attribute assignment and deletion, respectively) through mechanisms defined in the language for non-data descriptors:

    class A:
        j = 0
    
        @functools.cached_property
        def b(self):
            self.j += 1
            return self.j

    a = A()
    print(a.b)  # 1
    print(a.b)  # 1
    del a.b
    print(a.b)  # 2
    print(a.b)  # 2
    a.b = 42
    print(a.b)  # 42
    print(a.b)  # 42
    del a.b
    print(a.b)  # 3
    print(a.b)  # 3

I propose that this functionality be documented, for two reasons:
1. To notify developers that this functionality is available, and how to access it
2. To notify developers that this functionality needs to be explicitly disabled. This is important as the built-in `property` is the reverse: property setting and deletion needs to be explicitly implemented

Disabling the value can be achieved by subclassing `functools.cached_property` and overriding/implementing `__set__` and `__delete__` to raise:

    class immutable_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")

Further reading:
- Defining descriptors: https://docs.python.org/3/reference/datamodel.html#implementing-descriptors
- Descriptor how-to: https://docs.python.org/3/howto/descriptor.html
- Class construction: https://docs.python.org/3/reference/datamodel.html#creating-the-class-object
msg355112 - (view) Author: hongweipeng (hongweipeng) * Date: 2019-10-22 02:20
It can be solved by extending propery. `class cached_property(property):` . How about this idea?
msg355114 - (view) Author: Laurie Opperman (Epic_Wink) * Date: 2019-10-22 03:16
@hongweipeng What can be solved? Do you mean `functools.cached_property` should inherit `property`? First, this would break existing code dependant on the functionality of `functools.cached_property` (admittedly, Python 3.8 was release less than two weeks ago). Second, that seems like arbitrary restriction: I'd prefer just to update the documentation
History
Date User Action Args
2022-04-11 14:59:22adminsetgithub: 82734
2019-10-22 03:16:10Epic_Winksetmessages: + msg355114
2019-10-22 02:20:31hongweipengsetnosy: + hongweipeng
messages: + msg355112
2019-10-21 23:53:20Epic_Winkcreate