Passed
Push — main ( 44da9c...c9cf49 )
by
unknown
01:41
created

pincer.utils.conversion.dict_to_query()   A

Complexity

Conditions 3

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 19
rs 10
c 0
b 0
f 0
cc 3
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
0 ignored issues
show
Unused Code introduced by
Unused getfullargspec imported from inspect
Loading history...
7
from typing import TYPE_CHECKING
8
9
from .types import T, MISSING
0 ignored issues
show
Unused Code introduced by
Unused MISSING imported from types
Loading history...
Unused Code introduced by
Unused T imported from types
Loading history...
10
11
if TYPE_CHECKING:
12
    from ..client import Client
13
    from typing import Any, Callable, Dict, List, Optional, Set, Union, Tuple
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}
30
31
32
def remove_none(obj: Union[List, Dict, Set, Tuple]) -> Union[List, Dict, Set, Tuple]:
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...
33
    """
34
    Removes all ``None`` values from a list, dict or set.
35
36
    Parameters
37
    ----------
38
    obj : Union[List, Dict, Set, Tuple]
39
        The list, dict, set or tuple to remove ``None`` values from.
40
41
    Returns
42
    -------
43
    Union[List, Dict, Set, Tuple]
44
        The list, dict, set or tuple, without ``None`` values.
45
    """
46
    if isinstance(obj, list):
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
47
        return [i for i in obj if i is not None]
48
    elif isinstance(obj, tuple):
49
        return tuple(i for i in obj if i is not None)
50
    elif isinstance(obj, set):
51
        return obj - {None}
52
    elif isinstance(obj, dict):
53
        return {k: v for k, v in obj.items() if None not in (k, v)}
54
55
56
def dict_to_query(data: Dict[str, Any]) -> str:
57
    """
58
    Takes a dictionary of arguments and converts it into a query string to append to a url.
59
60
    Parameters
61
    ----------
62
    data : Dict[:class:`str`, :class:`~typing.Any`]
63
        The arguments for the query string
64
65
    Returns
66
    -------
67
    str
68
        The query string to append to a url
69
    """
70
    query_str = ""
71
    for key in data:
72
        if data[key] is not None:
73
            query_str += f"{key}={data[key]}&"
74
    return query_str[:-1]
75