| Total Complexity | 6 |
| Total Lines | 53 |
| Duplicated Lines | 0 % |
| Coverage | 92.86% |
| Changes | 0 | ||
| 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 |