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

typish._state   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 3

1 Function

Rating   Name   Duplication   Size   Complexity  
A register_get_type() 0 13 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A State.__init__() 0 5 1
A State.register_get_type() 0 12 1
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