lagom.markers   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 53
rs 10
c 0
b 0
f 0
ccs 13
cts 14
cp 0.9286
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Injectable.__bool__() 0 7 1
A Injectable.__getattr__() 0 12 3
A Injectable.__deepcopy__() 0 7 1
A Injectable.__copy__() 0 7 1
1 1
from typing import Any
2
3 1
from lagom.exceptions import InjectableNotResolved
4
5
6 1
class Injectable:
7
    """
8
    This class is looked for when analysing function signatures for arguments to
9
    injected
10
    """
11
12 1
    def __bool__(self):
13
        """
14
        If this is used in an if statement it should be falsy as it indicates the dependency
15
        has not been injected.
16
        :return:
17
        """
18 1
        return False
19
20 1
    def __copy__(self):
21
        """
22
        Much like highlander there can be only one. Injectable
23
        is a singleton.
24
        :return:
25
        """
26
        return self
27
28 1
    def __deepcopy__(self, memodict=None):
29
        """
30
        Much like highlander there can be only one. Injectable
31
        is a singleton.
32
        :return:
33
        """
34 1
        return self
35
36 1
    def __getattr__(self, item: str):
37
        """
38
        injectable should never have it's attributes referenced or a method call.
39
        This normally indicates that the default injectable value hasn't been
40
        handled by lagom - which is likely a function missing a bind decorator.
41
        """
42
        # Ignore dunder methods as it's likely some decorator magic and
43
        # it doesn't really help to raise an exception then.
44 1
        if item.startswith("__") and item.endswith("__"):
45 1
            return None
46 1
        raise InjectableNotResolved(
47
            f"Cannot get {item} on injectable. Make sure the function was bound to a container instance"
48
        )
49
50
51
# singleton object used to indicate that an argument should be injected
52
injectable: Any = Injectable()
53