Skip to content

Commit ef3fdff

Browse files
authored
Merge pull request reagento#473 from Tapeline/docs/inline-component-declaraton
Docs on inline component declaraton in return type
2 parents ea41b3c + 0fb8cfc commit ef3fdff

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

docs/advanced/components.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,33 @@ when declaring a dependency by ``FromComponent`` type annotation.
167167
container.get(float) # returns 0.1
168168
169169
170+
You can use ``Annotated[T, FromComponent(...)]`` in factory return type to provide this factory
171+
in specified component, instead of marking ``component = "component_name"`` in provider itself:
172+
173+
.. code-block:: python
174+
from typing import Annotated
175+
from dishka import FromComponent, make_container, Provider, provide, Scope
176+
177+
class MainProvider(Provider):
178+
179+
@provide(scope=Scope.APP)
180+
def foobar(self, a: Annotated[int, FromComponent("X")]) -> float:
181+
return a/10
182+
183+
@provide(scope=Scope.APP)
184+
def foo(self) -> Annotated[int, FromComponent("X")]:
185+
return 1
186+
187+
container = make_container(MainProvider())
188+
container.get(float) # returns 0.1
189+
190+
.. warning::
191+
Although dishka allows such declarations, it is considered to be a bad practice,
192+
because it allows you to mix factories related to different components in
193+
one provider. Use it only if your components consist of max of 1-2 factories and
194+
split them in every other case.
195+
196+
170197
``alias`` now can be used across components without changing the type:
171198

172199
.. code-block:: python

0 commit comments

Comments
 (0)