pincer.utils.insertion   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A should_pass_gateway() 0 16 1
A should_pass_cls() 0 16 1
A should_pass_ctx() 0 22 3
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], 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
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
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
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...
70
71
    return annotation == MessageContext or params[0] == "ctx"
72