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
This commit is contained in:
Jérémie Bolduc
2025-04-19 11:55:02 -04:00
committed by GitHub
parent e090153d93
commit efe2b7c539
2 changed files with 15 additions and 0 deletions

View File

@@ -114,6 +114,8 @@ def cache_self1(function: typing.Callable[[S, T], RetType]) -> typing.Callable[[
cache[arg] = res cache[arg] = res
return res return res
wrap.__defaults__ = function.__defaults__
return wrap return wrap

View File

@@ -35,6 +35,19 @@ class TestCacheSelf1(unittest.TestCase):
self.assertFalse(o1.func(1) is o1.func(2)) self.assertFalse(o1.func(1) is o1.func(2))
self.assertFalse(o1.func(1) is o2.func(1)) self.assertFalse(o1.func(1) is o2.func(1))
def test_cache_default(self) -> None:
class Cls:
@cache_self1
def func(self, _: Any = 1) -> object:
return object()
o1 = Cls()
o2 = Cls()
self.assertIs(o1.func(), o1.func())
self.assertIs(o1.func(1), o1.func())
self.assertIsNot(o1.func(2), o1.func())
self.assertIsNot(o1.func(), o2.func())
def test_gc(self) -> None: def test_gc(self) -> None:
# verify that we don't keep a global reference # verify that we don't keep a global reference
import gc import gc