Passed
Pull Request — main (#329)
by
unknown
03:11
created

pincer.utils.insertion.should_pass_gateway()   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 17
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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]]) -> bool:
12
    # TODO: fix docs
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
13
    """
14
    Checks whether a callable requires a self/cls as first parameter.
15
16
    Parameters
17
    ----------
18
    call: Union[Coro, Callable[..., Any]]
19
        The callable to check.
20
21
    Returns
22
    -------
23
    bool
24
        Whether the callable requires a self/cls as first parameter.
25
    """
26
    args = getfullargspec(call).args
27
    return len(args) >= 1 and args[0] in ["self", "cls"]
28
29
30
def should_pass_gateway(call: Union[Coro, Callable[..., Any]]) -> bool:
31
    # TODO: fix docs
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
32
    """
33
    Checks whether a callable requires a dispatcher as last parameter.
34
35
    Parameters
36
    ----------
37
    call: Union[Coro, Callable[..., Any]]
38
        The callable to check.
39
40
    Returns
41
    -------
42
    bool
43
        Whether the callable requires a dispatcher as first parameter.
44
    """
45
    args = getfullargspec(call).args
46
    return len(args) >= 1 and args[1] in ["gateway", "shard"]
47
48
49
context_types = [Signature.empty, MessageContext]
50
51
52
def should_pass_ctx(sig: Mapping[str, Parameter], params: List[str]) -> bool:
53
    # TODO: fix docs
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
54
    """
55
56
    Parameters
57
    ----------
58
    sig
59
    params
60
61
    Returns
62
    -------
63
64
    """
65
    if not params:
66
        return False
67
68
    annotation = sig[params[0]].annotation
69
    if isinstance(annotation, str):
70
        TypeCache()
71
        annotation = eval(annotation, TypeCache.cache, globals())
0 ignored issues
show
Security Best Practice introduced by
eval should generally only be used if absolutely necessary.

The usage of eval might allow for executing arbitrary code if a user manages to inject dynamic input. Please use this language feature with care and only when you are sure of the input.

Loading history...
72
73
    return (
74
        annotation == MessageContext
75
        or params[0] == "ctx"
76
    )
77