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.

Author vstinner
Recipients JelleZijlstra, corona10, eric.smith, petr.viktorin, pitrou, rhettinger, serhiy.storchaka, vstinner
Date 2022-03-28.23:32:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1648510358.75.0.470938350738.issue47143@roundup.psfhosted.org>
In-reply-to
Content
Jelle Zijlstra:
> I believe the attrs code wouldn't work if a method is decorated with a decorator that wraps the original function, such as @functools.cache.

What do you mean by "wouldn't work"? Do you mean that the semantics of "copy_class()" should be better defined? Currently, copy.deepcopy(MyClass) doesn't copy the class at all, it returns the class unmodified :-)

@functools.cache is designed for unbound methods.

Example:
---
import attr
import functools

@attr.s(slots=True)
class A:
    @staticmethod
    @functools.cache
    def incr(x):
        return x + 1

    @staticmethod
    @functools.lru_cache
    def incr_lru(x):
        return x + 1

obj = A()
print(obj.incr(1))
print(obj.incr_lru(2))
---

Output (current Python main branch, attrs 21.4.0):
---
2
3
---

@attr.s(slots=True) copies a class but then drops the original class. It doesn't create two classes which share methods, functools caches, etc.
History
Date User Action Args
2022-03-28 23:32:38vstinnersetrecipients: + vstinner, rhettinger, pitrou, eric.smith, petr.viktorin, serhiy.storchaka, JelleZijlstra, corona10
2022-03-28 23:32:38vstinnersetmessageid: <1648510358.75.0.470938350738.issue47143@roundup.psfhosted.org>
2022-03-28 23:32:38vstinnerlinkissue47143 messages
2022-03-28 23:32:38vstinnercreate