Passed
Pull Request — master (#13)
by Ramon
01:28
created

typish._state.State.__init__()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
from typing import Callable
2
3
from typish import T
4
5
6
class State:
7
    """
8
    A class which instances hold any state that may be used by typish.
9
    """
10
    def __init__(self) -> None:
11
        """
12
        Constructor.
13
        """
14
        self.get_type_per_cls = {}
15
16
    def register_get_type(
17
            self,
18
            cls: T,
19
            get_type_function: Callable[[T], type]) -> None:
20
        """
21
        Register a callable for some type that is to be used when calling
22
        typish.get_type.
23
        :param cls: the type for which that given callable is to be called.
24
        :param get_type_function: the callable to call for that type.
25
        :return: None.
26
        """
27
        self.get_type_per_cls[cls] = get_type_function
28
29
30
DEFAULT = State()
31
32
33
def register_get_type(
34
        cls: T,
35
        get_type_function: Callable[[T], type],
36
        state: State = DEFAULT) -> None:
37
    """
38
    Register a callable for some type that is to be used when calling
39
    typish.get_type.
40
    :param cls: the type for which that given callable is to be called.
41
    :param get_type_function: the callable to call for that type.
42
    :param state: any state that is used by typish.
43
    :return: None.
44
    """
45
    state.register_get_type(cls, get_type_function)
46