mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 01:47:36 +03:00 
			
		
		
		
	Edit list provider docs
This commit is contained in:
		
							parent
							
								
									d573c990b0
								
							
						
					
					
						commit
						a5e5b4b193
					
				| 
						 | 
					@ -10,7 +10,7 @@ follows `Semantic versioning`_
 | 
				
			||||||
Development version
 | 
					Development version
 | 
				
			||||||
-------------------
 | 
					-------------------
 | 
				
			||||||
- Update documentation and rework examples for: ``Singleton``, ``Callable``, ``Coroutine``,
 | 
					- Update documentation and rework examples for: ``Singleton``, ``Callable``, ``Coroutine``,
 | 
				
			||||||
  ``Object`` providers.
 | 
					  ``Object``, ``List`` providers.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
3.34.0
 | 
					3.34.0
 | 
				
			||||||
------
 | 
					------
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,10 @@
 | 
				
			||||||
List providers
 | 
					List provider
 | 
				
			||||||
--------------
 | 
					-------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.. meta::
 | 
				
			||||||
 | 
					   :keywords: Python,DI,Dependency injection,IoC,Inversion of Control,List,Injection
 | 
				
			||||||
 | 
					   :description: List provider helps to inject a list of the dependencies. This page demonstrates
 | 
				
			||||||
 | 
					                 how to use a List provider.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. currentmodule:: dependency_injector.providers
 | 
					.. currentmodule:: dependency_injector.providers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,28 +12,12 @@ List providers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. literalinclude:: ../../examples/providers/list.py
 | 
					.. literalinclude:: ../../examples/providers/list.py
 | 
				
			||||||
   :language: python
 | 
					   :language: python
 | 
				
			||||||
   :emphasize-lines: 6-9
 | 
					 | 
				
			||||||
   :lines: 6-8, 23-29
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
:py:class:`List` provider is needed for injecting a list of dependencies. It handles
 | 
					 | 
				
			||||||
positional argument injections the same way as :py:class:`Factory` provider:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
+ All providers (instances of :py:class:`Provider`) are called every time
 | 
					 | 
				
			||||||
  when injection needs to be done.
 | 
					 | 
				
			||||||
+ Providers could be injected "as is" (delegated), if it is defined explicitly. Check out
 | 
					 | 
				
			||||||
  :ref:`factory_providers_delegation`.
 | 
					 | 
				
			||||||
+ All other values are injected *"as is"*.
 | 
					 | 
				
			||||||
+ Positional context arguments will be appended after :py:class:`List` positional injections.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Full example:
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
.. literalinclude:: ../../examples/providers/list.py
 | 
					 | 
				
			||||||
   :language: python
 | 
					 | 
				
			||||||
   :emphasize-lines: 23-26
 | 
					 | 
				
			||||||
   :lines: 3-
 | 
					   :lines: 3-
 | 
				
			||||||
 | 
					   :emphasize-lines: 19-22
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					``List`` provider handles positional arguments the same way as a :ref:`factory-provider`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. note::
 | 
					.. note::
 | 
				
			||||||
 | 
					    Keyword argument are not supported.
 | 
				
			||||||
    Keyword argument injections are not supported.
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. disqus::
 | 
					.. disqus::
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -8,15 +8,11 @@ from dependency_injector import providers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@dataclasses.dataclass
 | 
					@dataclasses.dataclass
 | 
				
			||||||
class Module:
 | 
					class Module:
 | 
				
			||||||
    """Example module."""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    name: str
 | 
					    name: str
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@dataclasses.dataclass
 | 
					@dataclasses.dataclass
 | 
				
			||||||
class Dispatcher:
 | 
					class Dispatcher:
 | 
				
			||||||
    """Example dispatcher."""
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    modules: List[Module]
 | 
					    modules: List[Module]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -28,6 +24,7 @@ dispatcher_factory = providers.Factory(
 | 
				
			||||||
    ),
 | 
					    ),
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    dispatcher = dispatcher_factory()
 | 
					    dispatcher = dispatcher_factory()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,11 +32,10 @@ if __name__ == '__main__':
 | 
				
			||||||
    assert dispatcher.modules[0].name == 'm1'
 | 
					    assert dispatcher.modules[0].name == 'm1'
 | 
				
			||||||
    assert dispatcher.modules[1].name == 'm2'
 | 
					    assert dispatcher.modules[1].name == 'm2'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Call of dispatcher_factory() is equivalent to:
 | 
					    # Call "dispatcher = dispatcher_factory()" is an equivalent for:
 | 
				
			||||||
 | 
					    # dispatcher = Dispatcher(
 | 
				
			||||||
    dispatcher = Dispatcher(
 | 
					    #     modules=[
 | 
				
			||||||
        modules=[
 | 
					    #         Module(name='m1'),
 | 
				
			||||||
            Module(name='m1'),
 | 
					    #         Module(name='m2'),
 | 
				
			||||||
            Module(name='m2'),
 | 
					    #     ],
 | 
				
			||||||
        ],
 | 
					    # )
 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user