Skip to content

Commit efe2b7c

Browse files
authored
Core: Support default value with cache_self1 (#4667)
* add cache_self1_default and tests * merge the two decorators * just change the defaults of the wrap lol * add test for default and default
1 parent e090153 commit efe2b7c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ def wrap(self: S, arg: T) -> RetType:
114114
cache[arg] = res
115115
return res
116116

117+
wrap.__defaults__ = function.__defaults__
118+
117119
return wrap
118120

119121

test/utils/test_caches.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ def func(self, _: Any) -> object:
3535
self.assertFalse(o1.func(1) is o1.func(2))
3636
self.assertFalse(o1.func(1) is o2.func(1))
3737

38+
def test_cache_default(self) -> None:
39+
class Cls:
40+
@cache_self1
41+
def func(self, _: Any = 1) -> object:
42+
return object()
43+
44+
o1 = Cls()
45+
o2 = Cls()
46+
self.assertIs(o1.func(), o1.func())
47+
self.assertIs(o1.func(1), o1.func())
48+
self.assertIsNot(o1.func(2), o1.func())
49+
self.assertIsNot(o1.func(), o2.func())
50+
3851
def test_gc(self) -> None:
3952
# verify that we don't keep a global reference
4053
import gc

0 commit comments

Comments
 (0)