1 | # Copyright Pincer 2021-Present |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
2 | # Full MIT License can be found in `LICENSE` at the project root. |
||
3 | |||
4 | from inspect import getfullargspec, Parameter, Signature |
||
5 | from typing import Any, Union, Callable, Mapping, List |
||
6 | |||
7 | from .types import Coro, TypeCache |
||
8 | from ..objects.message.context import MessageContext |
||
9 | |||
10 | |||
11 | def should_pass_cls(call: Union[Coro, Callable[[Any], Any]]) -> bool: |
||
12 | """ |
||
13 | Checks whether a callable requires a self/cls as first parameter. |
||
14 | |||
15 | Parameters |
||
16 | ---------- |
||
17 | call: Union[Coro, Callable[[Any], Any]] |
||
18 | The callable to check. |
||
19 | |||
20 | Returns |
||
21 | ------- |
||
22 | bool |
||
23 | Whether the callable requires a self/cls as first parameter. |
||
24 | """ |
||
25 | args = getfullargspec(call).args |
||
26 | return len(args) >= 1 and args[0] in ["self", "cls"] |
||
27 | |||
28 | |||
29 | def should_pass_gateway(call: Union[Coro, Callable[[Any], Any]]) -> bool: |
||
30 | """ |
||
31 | Checks whether a callable requires a dispatcher as last parameter. |
||
32 | |||
33 | Parameters |
||
34 | ---------- |
||
35 | call: Union[:class:`~pincer.utils.types.Coro`, Callable[[Any], Any]] |
||
36 | The callable to check. |
||
37 | |||
38 | Returns |
||
39 | ------- |
||
40 | bool |
||
41 | Whether the callable requires a dispatcher as first parameter. |
||
42 | """ |
||
43 | args = getfullargspec(call).args |
||
44 | return len(args) >= 2 and args[1] in ("gateway", "shard") |
||
45 | |||
46 | |||
47 | context_types = [Signature.empty, MessageContext] |
||
48 | |||
49 | |||
50 | def should_pass_ctx(sig: Mapping[str, Parameter], params: List[str]) -> bool: |
||
51 | # TODO: Fix docs |
||
0 ignored issues
–
show
|
|||
52 | """ |
||
53 | |||
54 | Parameters |
||
55 | ---------- |
||
56 | sig |
||
57 | params |
||
58 | |||
59 | Returns |
||
60 | ------- |
||
61 | |||
62 | """ |
||
63 | if not params: |
||
64 | return False |
||
65 | |||
66 | annotation = sig[params[0]].annotation |
||
67 | if isinstance(annotation, str): |
||
68 | TypeCache() |
||
69 | annotation = eval(annotation, TypeCache.cache, globals()) |
||
0 ignored issues
–
show
|
|||
70 | |||
71 | return annotation == MessageContext or params[0] == "ctx" |
||
72 |