-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (42 loc) · 1.43 KB
/
utils.py
File metadata and controls
51 lines (42 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import typing
from fast_depends.utils import is_async_gen_callable, is_gen_callable
from faststream.types import SendableMessage
try:
from faststream.utils.functions import to_async
except ImportError:
from faststream._internal.utils import to_async # type: ignore[no-redef]
async def resolve_msg(
msg: None
| SendableMessage
| typing.Callable[[], SendableMessage]
| typing.Callable[[], typing.Awaitable[SendableMessage]]
| typing.Callable[[], typing.Iterator[SendableMessage]]
| typing.Callable[[], typing.AsyncIterator[SendableMessage]],
) -> typing.AsyncIterator[SendableMessage]:
"""Resolve message generation callback.
Args:
msg: object to send or sync/async message generation callback.
Returns:
The message to send
"""
if callable(msg):
if is_async_gen_callable(msg):
async for i in typing.cast(
typing.Callable[[], typing.AsyncIterator[SendableMessage]],
msg,
)():
yield i
elif is_gen_callable(msg):
for i in typing.cast(
typing.Callable[[], typing.Iterator[SendableMessage]],
msg,
)():
yield i
else:
get_msg = typing.cast(
typing.Callable[[], typing.Awaitable[SendableMessage]],
to_async(msg),
)
yield await get_msg()
else:
yield msg