Mailing List Archive

[issue41523] functools.cached_property does not satisfy the property check
New submission from Bernat Gabor <gaborjbernat@gmail.com>:

from functools import cached_property, lru_cache

class A:
@property
def a(self): return ''

@cached_property
def b(self): return ''

@property
@lru_cache
def c(self):
return ""


print(isinstance(A.a, property))
print(isinstance(A.b, property))
print(isinstance(A.c, property))

True
False
True

I feel like cached property should be of type property, not?

----------
messages: 375188
nosy: Bernat Gabor
priority: normal
severity: normal
status: open
title: functools.cached_property does not satisfy the property check
versions: Python 3.10, Python 3.8, Python 3.9

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:

No, cached_property is a completely different class.

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Bernat Gabor <gaborjbernat@gmail.com> added the comment:

I understand under the hood they're differenet, however I think cached_property wanted to be a shorter/better version of @property @lru_cache which does passes this check. Tools expecting properties to pass that check now need to extend their check... and similarly now is no longer enough to use isinstance property to check if something is a property.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:

There are not much things common to property and cached_property. cached_property does not have attributes fget, fset, fdel. They are both descriptors, but cached_property is not a data descriptor.

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Bernát Gábor <jokerjokerer@gmail.com> added the comment:

I think they're implemented differently and have slightly different semantics, but they're equal from a user point of view most of the time ????

----------
nosy: +Bernát Gábor

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
William Pickard <lollol222gg@gmail.com> added the comment:

In the lru_cache example, I think property is using the result of 'lru_cache(c)', which in turns returns a property instance, not a subtype instance.

----------
nosy: +WildCard65

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Pablo Galindo Salgado <pablogsal@gmail.com> added the comment:

> They are both descriptors, but cached_property is not a data descriptor.

I think this is one of the key reasons this should not return "True" for isinstance(A.b, property). Also, cached_property does not allow you to implement a setter or a deleter.

The docs say "Similar to property()" but not "the same as property()"

----------
nosy: +pablogsal

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:

I recommend closing this.

As Serhiy pointed out, they are profoundly different. Currently, a user can rely on the isinstance() check to differentiate them. So, changing the behavior would be a regression.

AFAICT, a user would not be able to deduce anything useful from the isinstance() check returning True in all three cases. Essentially, all they have in common is the concept of caching; otherwise, the mechanisms and implications are entirely unrelated.

----------
nosy: +rhettinger

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
William Pickard <lollol222gg@gmail.com> added the comment:

Another thing to note Raymond, as I stated before, example C is, from an external context, is a plain property object who's "fget" attribute is an instance of whatever "lru_cache" returns.

isinstance won't be able to differentiate between example "a" and "c" without checking "fget"

----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41523] functools.cached_property does not satisfy the property check [ In reply to ]
Change by Serhiy Storchaka <storchaka+cpython@gmail.com>:


----------
resolution: -> not a bug
stage: -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41523>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com