mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 18:07:44 +03:00 
			
		
		
		
	Implement container API
This commit is contained in:
		
							parent
							
								
									8a31384b18
								
							
						
					
					
						commit
						ab13daa3a5
					
				
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| 
						 | 
					@ -41,6 +41,7 @@ class Container:
 | 
				
			||||||
    dependencies: Dict[str, Provider]
 | 
					    dependencies: Dict[str, Provider]
 | 
				
			||||||
    overridden: Tuple[Provider]
 | 
					    overridden: Tuple[Provider]
 | 
				
			||||||
    wiring_config: WiringConfiguration
 | 
					    wiring_config: WiringConfiguration
 | 
				
			||||||
 | 
					    auto_load_config: bool = True
 | 
				
			||||||
    __self__: Self
 | 
					    __self__: Self
 | 
				
			||||||
    def __init__(self) -> None: ...
 | 
					    def __init__(self) -> None: ...
 | 
				
			||||||
    def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
 | 
					    def __deepcopy__(self, memo: Optional[Dict[str, Any]]) -> Provider: ...
 | 
				
			||||||
| 
						 | 
					@ -58,6 +59,7 @@ class Container:
 | 
				
			||||||
    def unwire(self) -> None: ...
 | 
					    def unwire(self) -> None: ...
 | 
				
			||||||
    def init_resources(self) -> Optional[Awaitable]: ...
 | 
					    def init_resources(self) -> Optional[Awaitable]: ...
 | 
				
			||||||
    def shutdown_resources(self) -> Optional[Awaitable]: ...
 | 
					    def shutdown_resources(self) -> Optional[Awaitable]: ...
 | 
				
			||||||
 | 
					    def load_config(self) -> None: ...
 | 
				
			||||||
    def apply_container_providers_overridings(self) -> None: ...
 | 
					    def apply_container_providers_overridings(self) -> None: ...
 | 
				
			||||||
    def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
 | 
					    def reset_singletons(self) -> SingletonResetContext[C_Base]: ...
 | 
				
			||||||
    def check_dependencies(self) -> None: ...
 | 
					    def check_dependencies(self) -> None: ...
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -377,6 +377,12 @@ class DynamicContainer(Container):
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            return _sync_ordered_shutdown(resources)
 | 
					            return _sync_ordered_shutdown(resources)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def load_config(self):
 | 
				
			||||||
 | 
					        """Load configuration."""
 | 
				
			||||||
 | 
					        config: providers.Configuration
 | 
				
			||||||
 | 
					        for config in self.traverse(types=[providers.Configuration]):
 | 
				
			||||||
 | 
					            config.load()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def apply_container_providers_overridings(self):
 | 
					    def apply_container_providers_overridings(self):
 | 
				
			||||||
        """Apply container providers' overridings."""
 | 
					        """Apply container providers' overridings."""
 | 
				
			||||||
        for provider in self.traverse(types=[providers.Container]):
 | 
					        for provider in self.traverse(types=[providers.Container]):
 | 
				
			||||||
| 
						 | 
					@ -671,6 +677,12 @@ class DeclarativeContainer(Container):
 | 
				
			||||||
    :type: WiringConfiguration
 | 
					    :type: WiringConfiguration
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    auto_load_config = True
 | 
				
			||||||
 | 
					    """Automatically load configuration when the container is created.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    :type: bool
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cls_providers = dict()
 | 
					    cls_providers = dict()
 | 
				
			||||||
    """Read-only dictionary of current container providers.
 | 
					    """Read-only dictionary of current container providers.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -720,6 +732,9 @@ class DeclarativeContainer(Container):
 | 
				
			||||||
        container.override_providers(**overriding_providers)
 | 
					        container.override_providers(**overriding_providers)
 | 
				
			||||||
        container.apply_container_providers_overridings()
 | 
					        container.apply_container_providers_overridings()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if cls.auto_load_config:
 | 
				
			||||||
 | 
					            container.load_config()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if container.is_auto_wiring_enabled():
 | 
					        if container.is_auto_wiring_enabled():
 | 
				
			||||||
            container.wire()
 | 
					            container.wire()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										44
									
								
								tests/unit/containers/instance/test_load_config_py2_py3.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								tests/unit/containers/instance/test_load_config_py2_py3.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,44 @@
 | 
				
			||||||
 | 
					"""Tests for container config loading."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from dependency_injector import containers, providers
 | 
				
			||||||
 | 
					from pytest import fixture
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@fixture
 | 
				
			||||||
 | 
					def yaml_config_file(tmp_path):
 | 
				
			||||||
 | 
					    yaml_config_file = str(tmp_path / "config.yml")
 | 
				
			||||||
 | 
					    with open(yaml_config_file, "w") as file:
 | 
				
			||||||
 | 
					        file.write(
 | 
				
			||||||
 | 
					            "section1:\n"
 | 
				
			||||||
 | 
					            "  value1: yaml-loaded\n"
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    return yaml_config_file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_auto_load_config(yaml_config_file):
 | 
				
			||||||
 | 
					    class ContainerWithConfig(containers.DeclarativeContainer):
 | 
				
			||||||
 | 
					        config = providers.Configuration(yaml_files=[yaml_config_file])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    container = ContainerWithConfig()
 | 
				
			||||||
 | 
					    assert container.config.section1.value1() == "yaml-loaded"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_manual_load_config(yaml_config_file):
 | 
				
			||||||
 | 
					    class ContainerWithConfig(containers.DeclarativeContainer):
 | 
				
			||||||
 | 
					        auto_load_config = False
 | 
				
			||||||
 | 
					        config = providers.Configuration(yaml_files=[yaml_config_file])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    container = ContainerWithConfig()
 | 
				
			||||||
 | 
					    assert container.config.section1.value1() is None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    container.load_config()
 | 
				
			||||||
 | 
					    assert container.config.section1.value1() == "yaml-loaded"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_load_config_does_not_affect_class(yaml_config_file):
 | 
				
			||||||
 | 
					    class ContainerWithConfig(containers.DeclarativeContainer):
 | 
				
			||||||
 | 
					        config = providers.Configuration(yaml_files=[yaml_config_file])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert ContainerWithConfig.config.section1.value1() is None
 | 
				
			||||||
 | 
					    _ = ContainerWithConfig()
 | 
				
			||||||
 | 
					    assert ContainerWithConfig.config.section1.value1() is None
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
"""Tests for container self provier."""
 | 
					"""Tests for container self provider."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from dependency_injector import containers, providers, errors
 | 
					from dependency_injector import containers, providers, errors
 | 
				
			||||||
from pytest import raises
 | 
					from pytest import raises
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user