async def test_cached_property_func(self): class C: pass async def f(self): return 42 C.f = async_cached_property(f) self.assertEqual(C.f.func, f)
async def test_cached_property_slot_property(self): class C: __slots__ = ("f",) async def f(self): return 42 prev_f = C.f C.f = async_cached_property(f, C.f) self.assertEqual(C.f.slot, prev_f)
async def test_cached_property_slot_name(self): class C: __slots__ = ("f",) async def f(self): return 42 C.f = async_cached_property(f, C.f) self.assertEqual(C.f.name, "f")
async def test_cached_property_slot_raises(self): class C: __slots__ = ("f",) async def f(self): raise NoWayError() C.f = async_cached_property(f, C.f) with self.assertRaises(NoWayError): await C().f
async def test_cached_property_slot_wrong_type(self): """apply a cached property from one type to another""" class C: __slots__ = ("abc", ) class D: pass async def f(self): return 42 D.abc = async_cached_property(f, C.abc) a = D() with self.assertRaises(TypeError): x = await a.abc
async def test_cached_property_slot(self): class C: __slots__ = ("f", "calls") def __init__(self): self.calls = 0 async def f(self): self.calls += 1 return 42 C.f = async_cached_property(f, C.f) a = C() self.assertEqual(await a.f, 42) self.assertEqual(a.calls, 1) self.assertEqual(await a.f, 42) self.assertEqual(a.calls, 1)
async def test_cached_property_readonly_descriptor(self): async def f(self): return 42 with self.assertRaises(TypeError): async_cached_property(f, range.start)
async def test_cached_property_incompatible_descriptor(self): async def f(self): return 42 with self.assertRaises(TypeError): async_cached_property(f, GeneratorType.gi_frame)
async def test_cached_property_non_descriptor(self): async def f(self): return 42 with self.assertRaises(TypeError): async_cached_property(f, 42)