Passed
Pull Request — main (#226)
by
unknown
01:52
created

pincer.utils.conversion.convert()   B

Complexity

Conditions 7

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 19
rs 8
c 0
b 0
f 0
cc 7
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 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 Dict, Callable, Any, Optional
14
15
16
def construct_client_dict(client: Client, data: Dict[...]):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
17
    return {**data, "_client": client, "_http": client.http}
18
19
20
def convert(
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
21
    value: Any,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
22
    factory: Callable[[Any], T],
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
23
    check: Optional[T] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
24
    client: Optional[Client] = None,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
25
) -> T:
26
    def handle_factory() -> T:
27
        if check is not None and isinstance(value, check):
28
            return value
29
30
        try:
31
            if client and "_client" in getfullargspec(factory).args:
32
                return factory(construct_client_dict(client, value))
33
        except TypeError:  # Building type/has no signature
34
            pass
35
36
        return factory(value)
37
38
    return MISSING if value is MISSING else handle_factory()
39