mirror of
https://github.com/MarioSpore/Grinch-AP.git
synced 2025-10-21 12:11:33 -06:00
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:
2
Utils.py
2
Utils.py
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user