Passed
Push — main ( fdd10b...6849a6 )
by Yohann
01:28
created

pincer.utils.conversion.convert()   C

Complexity

Conditions 9

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 29
rs 6.6666
c 0
b 0
f 0
cc 9
nop 4
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 dataclasses import is_dataclass
7
from inspect import getfullargspec
8
from typing import TYPE_CHECKING
9
10
from .types import T, MISSING
11
12
if TYPE_CHECKING:
13
    from ..client import Client
0 ignored issues
show
introduced by
Cannot import 'client' due to syntax error 'invalid syntax (<unknown>, line 351)'
Loading history...
14
    from typing import Dict, Callable, Any, Optional
15
16
17
def construct_client_dict(client: Client, data: Dict[...]):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
18
    return {
19
        **data,
20
        "_client": client,
21
        "_http": client.http
22
    }
23
24
25
def convert(
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
26
        value: Any,
27
        factory: Callable[[Any], T],
28
        check: Optional[T] = None,
29
        client: Optional[Client] = None
30
) -> T:
31
    def handle_factory() -> T:
32
        def fin_fac(v: Any):
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
            if is_dataclass(v):
34
                return
35
36
            if check is not None and isinstance(v, check):
37
                return v
38
39
            try:
40
                if client and "_client" in getfullargspec(factory).args:
41
                    return factory(construct_client_dict(client, v))
42
            except TypeError:  # Building type/has no signature
43
                pass
44
45
            return factory(v)
46
47
        return (
48
            list(map(fin_fac, value))
49
            if isinstance(value, list)
50
            else fin_fac(value)
51
        )
52
53
    return MISSING if value is MISSING else handle_factory()
54