Total Complexity | 3 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Changes | 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 |