Passed
Pull Request — main (#248)
by
unknown
01:42
created

pincer.utils.conversion.remove_none()   A

Complexity

Conditions 4

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 20
rs 10
c 0
b 0
f 0
cc 4
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 __future__ import annotations
5
6
from inspect import getfullargspec
7
from typing import TYPE_CHECKING
8
9
from .types import T, MISSING
10
11
if TYPE_CHECKING:
12
    from ..client import Client
0 ignored issues
show
introduced by
Cannot import 'client' due to syntax error 'invalid syntax (<unknown>, line 354)'
Loading history...
13
    from typing import Any, Callable, Dict, List, Optional, Set, Union
14
15
16
def construct_client_dict(client: Client, data: Dict) -> Dict:
17
    # TODO: fix docs
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
18
    """
19
20
    Parameters
21
    ----------
22
    client
23
    data
24
25
    Returns
26
    -------
27
28
    """
29
    return {**data, "_client": client, "_http": client.http}
30
31
32
def convert(
33
    value: Any,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
34
    factory: Callable[[Any], T],
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
35
    check: Optional[T] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
36
    client: Optional[Client] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
37
) -> T:
38
    """
39
    Parameters
40
    ----------
41
    value : Any
42
        The value that has to have its type converted.
43
    factory : Callable[[Any], T]
44
        The conversion factory/object to use.
45
    check : Optional[T]
46
        Skip conversion if ``value`` is already this type.
47
    client : Optional[:class:`~pincer.client.Client`]
48
        Reference to :class:`~pincer.client.Client`
49
    """
50
    def handle_factory() -> T:
51
        if check is not None and isinstance(value, check):
52
            return value
53
54
        try:
55
            if client and "_client" in getfullargspec(factory).args:
56
                return factory(construct_client_dict(client, value))
57
        except TypeError:  # Building type/has no signature
58
            pass
59
60
        return factory(value)
61
62
    return MISSING if value is MISSING else handle_factory()
63
64
65
def remove_none(obj: Union[List, Dict, Set]) -> Union[List, Dict, Set]:
0 ignored issues
show
Unused Code introduced by
Either all return statements in a function should return an expression, or none of them should.
Loading history...
66
    """
67
    Removes all ``None`` values from a list, dict or set.
68
69
    Parameters
70
    ----------
71
    obj : Union[List, Dict, Set]
72
        The list, dict or set to remove ``None`` values from.
73
74
    Returns
75
    -------
76
    Union[List, Dict, Set]
77
        The list, dict or set without ``None`` values.
78
    """
79
    if isinstance(obj, list):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
80
        return [i for i in obj if i is not None]
81
    elif isinstance(obj, set):
82
        return obj - {None}
83
    elif isinstance(obj, dict):
84
        return {k: v for k, v in obj.items() if None not in {k, v}}
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...