STINNER Victor <vstin...@python.org> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue47143>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to