mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 07:57:43 +03:00 
			
		
		
		
	Add container usage to provided instance examples
This commit is contained in:
		
							parent
							
								
									4f111aae9b
								
							
						
					
					
						commit
						d6e4e8fb08
					
				|  | @ -14,7 +14,7 @@ You can inject provided object attribute, item or result of its method call. | ||||||
| 
 | 
 | ||||||
| .. literalinclude:: ../../examples/providers/provided_instance.py | .. literalinclude:: ../../examples/providers/provided_instance.py | ||||||
|    :language: python |    :language: python | ||||||
|    :emphasize-lines: 26-32 |    :emphasize-lines: 28-34 | ||||||
|    :lines: 3- |    :lines: 3- | ||||||
| 
 | 
 | ||||||
| To use the feature you should use the ``.provided`` attribute of the injected provider. This | To use the feature you should use the ``.provided`` attribute of the injected provider. This | ||||||
|  | @ -32,7 +32,7 @@ You can do nested constructions: | ||||||
| 
 | 
 | ||||||
| .. literalinclude:: ../../examples/providers/provided_instance_complex.py | .. literalinclude:: ../../examples/providers/provided_instance_complex.py | ||||||
|    :language: python |    :language: python | ||||||
|    :emphasize-lines: 24-30 |    :emphasize-lines: 26-32 | ||||||
|    :lines: 3- |    :lines: 3- | ||||||
| 
 | 
 | ||||||
| The ``.provided`` attribute is available for the next providers: | The ``.provided`` attribute is available for the next providers: | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| """Example of the injecting of provided instance attributes and items.""" | """Example of the injecting of provided instance attributes and items.""" | ||||||
| 
 | 
 | ||||||
| from dependency_injector import providers | from dependency_injector import containers, providers | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Service: | class Service: | ||||||
|  | @ -23,6 +23,8 @@ class Client: | ||||||
|         self.value4 = value4 |         self.value4 = value4 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | class Container(containers.DeclarativeContainer): | ||||||
|  | 
 | ||||||
|     service = providers.Singleton(Service) |     service = providers.Singleton(Service) | ||||||
| 
 | 
 | ||||||
|     client_factory = providers.Factory( |     client_factory = providers.Factory( | ||||||
|  | @ -35,5 +37,7 @@ client_factory = providers.Factory( | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     client = client_factory() |     container = Container() | ||||||
|  | 
 | ||||||
|  |     client = container.client_factory() | ||||||
|     assert client.value1 == client.value2 == client.value3 == 'foo' |     assert client.value1 == client.value2 == client.value3 == 'foo' | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| """Complex example of the injecting of provided instance attributes and items.""" | """Complex example of the injecting of provided instance attributes and items.""" | ||||||
| 
 | 
 | ||||||
| from dependency_injector import providers | from dependency_injector import containers, providers | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class Service: | class Service: | ||||||
|  | @ -12,6 +12,8 @@ class Service: | ||||||
|         return self.value |         return self.value | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | class Container(containers.DeclarativeContainer): | ||||||
|  | 
 | ||||||
|     service = providers.Singleton(Service, value=42) |     service = providers.Singleton(Service, value=42) | ||||||
| 
 | 
 | ||||||
|     dependency = providers.Object( |     dependency = providers.Object( | ||||||
|  | @ -33,10 +35,12 @@ demo_list = providers.List( | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||||
|     assert demo_list() == [ |     container = Container() | ||||||
|  | 
 | ||||||
|  |     assert container.demo_list() == [ | ||||||
|         10, |         10, | ||||||
|         22, |         22, | ||||||
|         service(), |         container.service(), | ||||||
|         42, |         42, | ||||||
|         42, |         42, | ||||||
|     ] |     ] | ||||||
|  |  | ||||||
|  | @ -283,8 +283,8 @@ class Selector(Provider): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class ProvidedInstanceFluentInterface: | class ProvidedInstanceFluentInterface: | ||||||
|     def __getattr__(self, item: str) -> AttributeGetter: ... |     def __getattr__(self, item: Any) -> AttributeGetter: ... | ||||||
|     def __getitem__(self, item: str) -> ItemGetter: ... |     def __getitem__(self, item: Any) -> ItemGetter: ... | ||||||
|     def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ... |     def call(self, *args: Injection, **kwargs: Injection) -> MethodCaller: ... | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user