| Total Complexity | 5 |
| Total Lines | 24 |
| Duplicated Lines | 0 % |
| Coverage | 91.67% |
| Changes | 0 | ||
| 1 | 1 | from typing import List, Type, Union |
|
| 2 | |||
| 3 | 1 | from lagom.definitions import SingletonWrapper, ConstructionWithoutContainer |
|
| 4 | 1 | from lagom.interfaces import ExtendableContainer, WriteableContainer, ReadableContainer |
|
| 5 | |||
| 6 | |||
| 7 | 1 | def update_container_singletons( |
|
| 8 | container: Union[ExtendableContainer, WriteableContainer], singletons: List[Type] |
||
| 9 | ): |
||
| 10 | 1 | if isinstance(container, ExtendableContainer): |
|
| 11 | 1 | new_container = container.clone() |
|
| 12 | else: |
||
| 13 | new_container = container |
||
| 14 | 1 | for dep in singletons: |
|
| 15 | 1 | _define_singleton_in_new_container(new_container, container, dep) |
|
| 16 | 1 | return new_container |
|
| 17 | |||
| 18 | |||
| 19 | 1 | def _define_singleton_in_new_container( |
|
| 20 | new_container: WriteableContainer, container: ReadableContainer, dep: Type |
||
| 21 | ): |
||
| 22 | 1 | new_container[dep] = SingletonWrapper( |
|
| 23 | ConstructionWithoutContainer(lambda: container.resolve(dep)) |
||
| 24 | ) |
||
| 25 |