1from collections.abc import Awaitable
2from inspect import CO_ITERABLE_COROUTINE
3from types import CoroutineType
4from types import GeneratorType
5
6
7async def do_await(obj):
8 return await obj
9
10
11def do_yield_from(gen):
12 return (yield from gen)
13
14
15def await_(obj):
16 obj_type = type(obj)
17 if (
18 obj_type is CoroutineType
19 or obj_type is GeneratorType
20 and bool(obj.gi_code.co_flags & CO_ITERABLE_COROUTINE)
21 or isinstance(obj, Awaitable)
22 ):
23 return do_await(obj).__await__()
24 else:
25 return do_yield_from(obj)
26
27
28def __aiter__(self):
29 return self.__wrapped__.__aiter__()
30
31
32async def __anext__(self):
33 return await self.__wrapped__.__anext__()
34
35
36def __await__(self):
37 return await_(self.__wrapped__)
38
39
40def __aenter__(self):
41 return self.__wrapped__.__aenter__()
42
43
44def __aexit__(self, *args, **kwargs):
45 return self.__wrapped__.__aexit__(*args, **kwargs)
46
47
48def identity(obj):
49 return obj
50
51
52class cached_property:
53 def __init__(self, func):
54 self.func = func
55
56 def __get__(self, obj, cls):
57 if obj is None:
58 return self
59 value = obj.__dict__[self.func.__name__] = self.func(obj)
60 return value