Skip to content

Commit 2e76b2b

Browse files
fixup! Add support for Fast Stream Depends
1 parent 1cb1d66 commit 2e76b2b

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

docs/examples/fastdepends.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. _fastdepends-example:
2+
3+
FastDepends example
4+
===================
5+
6+
.. meta::
7+
:keywords: Python,Dependency Injection,FastDepends,Example
8+
:description: This example demonstrates a usage of the FastDepends and Dependency Injector.
9+
10+
11+
This example shows how to use ``Dependency Injector`` with `FastDepends <https://github.com/Lancetnik/FastDepends>`_.
12+
13+
The example application is a REST API that searches for funny GIFs on the `Giphy <https://giphy.com/>`_.
14+
15+
Example code is available on `Github <https://github.com/ets-labs/python-dependency-injector/tree/master/tests/unit/wiringfastdepends>`_.
16+
17+
Quick sample
18+
------------
19+
20+
Just use it within ``Depends``
21+
22+
.. code-block:: python
23+
24+
import sys
25+
26+
from dependency_injector import containers, providers
27+
from dependency_injector.wiring import inject, Provide
28+
from fast_depends import Depends
29+
30+
31+
class CoefficientService:
32+
@staticmethod
33+
def get_coefficient() -> float:
34+
return 1.2
35+
36+
37+
class Container(containers.DeclarativeContainer):
38+
service = providers.Factory(CoefficientService)
39+
40+
41+
@inject
42+
def apply_coefficient(
43+
a: int,
44+
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
45+
) -> float:
46+
return a * coefficient_provider.get_coefficient()
47+
48+
49+
container = Container()
50+
container.wire(modules=[sys.modules[__name__]])
51+
52+
assert apply_coefficient(100) == 120.0
53+
54+

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ scipy
2020
boto3
2121
mypy_boto3_s3
2222
typing_extensions
23+
fast-depends
2324

2425
-r requirements-ext.txt
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from dependency_injector.wiring import inject, Provide
2+
from fast_depends import Depends
3+
4+
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
5+
import os
6+
7+
_SAMPLES_DIR = os.path.abspath(
8+
os.path.sep.join(
9+
(
10+
os.path.dirname(__file__),
11+
"../samples/",
12+
)
13+
),
14+
)
15+
import sys
16+
17+
sys.path.append(_SAMPLES_DIR)
18+
19+
20+
from wiringfastdepends.sample import CoefficientService, Container
21+
22+
23+
24+
@inject
25+
def apply_coefficient(
26+
a: int,
27+
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
28+
) -> float:
29+
return a * coefficient_provider.get_coefficient()
30+
31+
32+
container = Container()
33+
container.wire(modules=[sys.modules[__name__]])
34+
35+
36+
def test_wire_positive() -> None:
37+
assert apply_coefficient(100) == 120.0
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import sys
3+
4+
from dependency_injector import containers, providers
5+
from dependency_injector.wiring import inject, Provide
6+
from fast_depends import Depends
7+
8+
9+
class CoefficientService:
10+
@staticmethod
11+
def get_coefficient() -> float:
12+
return 1.2
13+
14+
15+
class Container(containers.DeclarativeContainer):
16+
service = providers.Factory(CoefficientService)
17+
18+
19+
@inject
20+
def apply_coefficient(
21+
a: int,
22+
coefficient_provider: CoefficientService = Depends(Provide[Container.service]),
23+
) -> float:
24+
return a * coefficient_provider.get_coefficient()
25+
26+
27+
container = Container()
28+
container.wire(modules=[sys.modules[__name__]])
29+
30+
assert apply_coefficient(100) == 120.0

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ deps=
1717
mypy_boto3_s3
1818
pydantic-settings
1919
werkzeug
20+
fast-depends
2021
extras=
2122
yaml
2223
commands = pytest
@@ -44,6 +45,7 @@ deps =
4445
boto3
4546
mypy_boto3_s3
4647
werkzeug
48+
fast-depends
4749
commands = pytest -m pydantic
4850

4951
[testenv:coveralls]

0 commit comments

Comments
 (0)