Skip to content

Commit 46d4b60

Browse files
committed
Merge branch 'release/1.0.0'
2 parents c5a5f0a + 1ffacb2 commit 46d4b60

File tree

7 files changed

+113
-49
lines changed

7 files changed

+113
-49
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
This project was initially created to show the abillities of [taskiq-dependencies](https://github.com/taskiq-python/taskiq-dependencies) project, which is used by [taskiq](https://github.com/taskiq-python/taskiq) to provide you with the best experience of sending distributed tasks.
99

10-
This project adds [FastAPI](https://github.com/tiangolo/fastapi)-like dependency injection to your [AioHTTP](https://github.com/aio-libs/aiohttp) application.
10+
This project adds [FastAPI](https://github.com/tiangolo/fastapi)-like dependency injection to your [AioHTTP](https://github.com/aio-libs/aiohttp) application and swagger documentation based on types.
1111

1212
To start using dependency injection, just initialize the injector.
1313

@@ -362,8 +362,8 @@ async def my_handler(var: str = Depends(Path())):
362362
Sometimes for tests you don't want to calculate actual functions
363363
and you want to pass another functions instead.
364364

365-
To do so, you can add "dependency_overrides" key to the aplication.
366-
It's a dict that is passed as additional context to dependency resolvers.
365+
To do so, you can add "dependency_overrides" or "values_overrides" to the aplication's state.
366+
These values should be dicts.
367367

368368
Here's an example.
369369

@@ -385,5 +385,19 @@ where you create your application. And make sure that keys
385385
of that dict are actual function that are being replaced.
386386

387387
```python
388-
my_app["dependency_overrides"] = {original_dep: 2}
388+
my_app["values_overrides"] = {original_dep: 2}
389389
```
390+
391+
But `values_overrides` only overrides values. If you want to
392+
override functions, you have to use `dependency_overrides`. Here's an example:
393+
394+
```python
395+
def replacing_function() -> int:
396+
return 2
397+
398+
399+
my_app["dependency_overrides"] = {original_dep: replacing_function}
400+
```
401+
402+
The cool point about `dependency_overrides`, is that it recalculates graph and
403+
you can use dependencies in function that replaces the original.

aiohttp_deps/initializer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ async def __call__(self, request: web.Request) -> web.StreamResponse:
4444
{
4545
web.Request: request,
4646
web.Application: request.app,
47-
**request.app.get("dependency_overrides", {}),
47+
**request.app.get("values_overrides", {}),
4848
},
49+
replaced_deps=request.app.get("dependency_overrides"),
4950
) as resolver:
5051
return await self.original_handler(**(await resolver.resolve_kwargs()))
5152

aiohttp_deps/view.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ async def _iter(self) -> StreamResponse:
4343
{
4444
web.Request: self.request,
4545
web.Application: self.request.app,
46-
**self.request.app.get("dependency_overrides", {}),
46+
**self.request.app.get("values_overrides", {}),
4747
},
48+
replaced_deps=self.request.app.get("dependency_overrides"),
4849
) as ctx:
4950
return await method(**(await ctx.resolve_kwargs())) # type: ignore

poetry.lock

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "aiohttp-deps"
33
description = "Dependency injection for AioHTTP"
44
authors = ["Taskiq team <taskiq@no-reply.com>"]
55
maintainers = ["Taskiq team <taskiq@no-reply.com>"]
6-
version = "0.3.2"
6+
version = "1.0.0"
77
readme = "README.md"
88
license = "LICENSE"
99
classifiers = [

tests/test_func_dependencies.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ async def handler(app: web.Application = Depends()):
3737
assert "Application" in (await resp.json())["request"]
3838

3939

40+
@pytest.mark.anyio
41+
async def test_values_override(
42+
my_app: web.Application,
43+
aiohttp_client: ClientGenerator,
44+
):
45+
def original_dep() -> int:
46+
return 1
47+
48+
async def handler(num: int = Depends(original_dep)):
49+
return web.json_response({"request": num})
50+
51+
my_app.router.add_get("/", handler)
52+
my_app["values_overrides"] = {original_dep: 2}
53+
54+
client = await aiohttp_client(my_app)
55+
resp = await client.get("/")
56+
assert resp.status == 200
57+
assert (await resp.json())["request"] == 2
58+
59+
4060
@pytest.mark.anyio
4161
async def test_dependency_override(
4262
my_app: web.Application,
@@ -45,11 +65,14 @@ async def test_dependency_override(
4565
def original_dep() -> int:
4666
return 1
4767

68+
def custom_dep() -> int:
69+
return 2
70+
4871
async def handler(num: int = Depends(original_dep)):
4972
return web.json_response({"request": num})
5073

5174
my_app.router.add_get("/", handler)
52-
my_app["dependency_overrides"] = {original_dep: 2}
75+
my_app["dependency_overrides"] = {original_dep: custom_dep}
5376

5477
client = await aiohttp_client(my_app)
5578
resp = await client.get("/")

tests/test_view_dependencies.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ async def get(self):
5555
assert resp.status == 405
5656

5757

58+
@pytest.mark.anyio
59+
async def test_values_override(
60+
my_app: web.Application,
61+
aiohttp_client: ClientGenerator,
62+
):
63+
def original_dep() -> int:
64+
return 1
65+
66+
class MyView(View):
67+
async def get(self, num: int = Depends(original_dep)):
68+
"""Nothing."""
69+
return web.json_response({"request": num})
70+
71+
my_app.router.add_view("/", MyView)
72+
my_app["values_overrides"] = {original_dep: 2}
73+
74+
client = await aiohttp_client(my_app)
75+
resp = await client.get("/")
76+
assert resp.status == 200
77+
assert (await resp.json())["request"] == 2
78+
79+
5880
@pytest.mark.anyio
5981
async def test_dependency_override(
6082
my_app: web.Application,
@@ -63,13 +85,16 @@ async def test_dependency_override(
6385
def original_dep() -> int:
6486
return 1
6587

88+
def replaced() -> int:
89+
return 2
90+
6691
class MyView(View):
6792
async def get(self, num: int = Depends(original_dep)):
6893
"""Nothing."""
6994
return web.json_response({"request": num})
7095

7196
my_app.router.add_view("/", MyView)
72-
my_app["dependency_overrides"] = {original_dep: 2}
97+
my_app["dependency_overrides"] = {original_dep: replaced}
7398

7499
client = await aiohttp_client(my_app)
75100
resp = await client.get("/")

0 commit comments

Comments
 (0)