Skip to content

Commit 9038155

Browse files
authored
docs: Imporve dependencies docs (#2263)
1 parent 47ff6e4 commit 9038155

File tree

13 files changed

+48
-41
lines changed

13 files changed

+48
-41
lines changed

docs/docs/en/getting-started/dependencies/index.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ To implement dependencies in **FastStream**, a special class called **Depends**
8484
**The first step**: You need to declare a dependency, which can be any `Callable` object.
8585

8686
??? note "Callable"
87-
A "Callable" is an object that can be "called". It can be a function, a class, or a class method.
87+
A ["Callable"](https://docs.python.org/3/glossary.html#term-callable){.external-link target="_blank"} is an object that can be "called". It can be a function, a class, or a class method.
8888

8989
In other words, if you can write code like `my_object()` - `my_object` is `Callable`
9090

@@ -140,13 +140,7 @@ To implement dependencies in **FastStream**, a special class called **Depends**
140140
{!> docs_src/getting_started/dependencies/basic/redis/depends.py [ln:11-12] !}
141141
```
142142

143-
**The last step**: Just use the result of executing your dependency!
144-
145-
It's easy, isn't it?
146-
147-
!!! tip "Auto `#!python @apply_types`"
148-
In the code above, we didn't use this decorator for our dependencies. However, it still applies
149-
to all functions used as dependencies. Please keep this in your mind.
143+
**The last step**: Use the result of executing your dependency!
150144

151145
## Top-level Dependencies
152146

@@ -210,6 +204,10 @@ Dependencies can also contain other dependencies. This works in a very predictab
210204

211205
1. A nested dependency is called here
212206

207+
!!! tip "Auto `#!python @apply_types`"
208+
In the code above, we didn't use this decorator on our `simple_dependency`.
209+
However, it still automatically applies to all functions used as dependencies.
210+
213211
!!! Tip "Caching"
214212
In the example above, the `another_dependency` function will be called at **ONCE**!
215213
**FastDepends** caches all dependency execution results within **ONE** `#!python @apply_types` call stack.
@@ -247,13 +245,17 @@ these types have the same annotation. Just keep it in mind. Or not... Anyway, I'
247245
from faststream import Depends, apply_types
248246

249247
def simple_dependency(a: int, b: int = 3) -> str:
248+
# NOTE: This will make type-checkers unhappy,
249+
# it is better to avoid this pattern in production code!
250250
return a + b # 'return' is cast to `str` for the first time
251251

252252
@apply_types
253253
def method(a: int, d: int = Depends(simple_dependency)):
254254
# 'd' is cast to `int` for the second time
255255
return a + d
256256

257+
# NOTE: This will make type-checkers unhappy,
258+
# it is better to avoid this pattern in production code!
257259
assert method("1") == 5
258260
```
259261

docs/docs_src/getting_started/dependencies/basic/async_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import pytest
33
from faststream import Depends, apply_types
44

5-
async def simple_dependency(a: int, b: int = 3):
5+
async def simple_dependency(a: int, b: int = 3) -> int:
66
return a + b
77

8-
def another_dependency(a: int):
8+
def another_dependency(a: int) -> int:
99
return a
1010

1111
@apply_types
@@ -17,5 +17,5 @@ async def method(
1717
return a + b + c
1818

1919
@pytest.mark.asyncio
20-
async def test_async_dependency():
21-
assert 6 == await method("1")
20+
async def test_async_dependency() -> None:
21+
assert 6 == await method(1)

docs/docs_src/getting_started/dependencies/basic/confluent/depends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
broker = KafkaBroker("localhost:9092")
55
app = FastStream(broker)
66

7-
def simple_dependency():
7+
def simple_dependency() -> int:
88
return 1
99

1010
@broker.subscriber("test")

docs/docs_src/getting_started/dependencies/basic/confluent/nested_depends.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
broker = KafkaBroker("localhost:9092")
55
app = FastStream(broker)
66

7-
def another_dependency():
7+
def another_dependency() -> int:
88
return 1
99

10-
def simple_dependency(b: int = Depends(another_dependency)): # (1)
10+
def simple_dependency(b: int = Depends(another_dependency)) -> int: # (1)
1111
return b * 2
1212

1313
@broker.subscriber("test")
1414
async def handler(
1515
body: dict,
1616
a: int = Depends(another_dependency),
17-
b: int = Depends(simple_dependency)):
18-
assert (a + b) == 3
17+
b: int = Depends(simple_dependency),
18+
):
19+
assert a + b == 3

docs/docs_src/getting_started/dependencies/basic/kafka/depends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
broker = KafkaBroker("localhost:9092")
55
app = FastStream(broker)
66

7-
def simple_dependency():
7+
def simple_dependency() -> int:
88
return 1
99

1010
@broker.subscriber("test")

docs/docs_src/getting_started/dependencies/basic/kafka/nested_depends.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
broker = KafkaBroker("localhost:9092")
55
app = FastStream(broker)
66

7-
def another_dependency():
7+
def another_dependency() -> int:
88
return 1
99

10-
def simple_dependency(b: int = Depends(another_dependency)): # (1)
10+
def simple_dependency(b: int = Depends(another_dependency)) -> int: # (1)
1111
return b * 2
1212

1313
@broker.subscriber("test")
1414
async def handler(
1515
body: dict,
1616
a: int = Depends(another_dependency),
17-
b: int = Depends(simple_dependency)):
18-
assert (a + b) == 3
17+
b: int = Depends(simple_dependency),
18+
):
19+
assert a + b == 3

docs/docs_src/getting_started/dependencies/basic/nats/depends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
broker = NatsBroker("nats://localhost:4222")
55
app = FastStream(broker)
66

7-
def simple_dependency():
7+
def simple_dependency() -> int:
88
return 1
99

1010
@broker.subscriber("test")

docs/docs_src/getting_started/dependencies/basic/nats/nested_depends.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
broker = NatsBroker("nats://localhost:4222")
55
app = FastStream(broker)
66

7-
def another_dependency():
7+
def another_dependency() -> int:
88
return 1
99

10-
def simple_dependency(b: int = Depends(another_dependency)): # (1)
10+
def simple_dependency(b: int = Depends(another_dependency)) -> int: # (1)
1111
return b * 2
1212

1313
@broker.subscriber("test")
1414
async def handler(
1515
body: dict,
1616
a: int = Depends(another_dependency),
17-
b: int = Depends(simple_dependency)):
18-
assert (a + b) == 3
17+
b: int = Depends(simple_dependency),
18+
):
19+
assert a + b == 3

docs/docs_src/getting_started/dependencies/basic/rabbit/depends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
55
app = FastStream(broker)
66

7-
def simple_dependency():
7+
def simple_dependency() -> int:
88
return 1
99

1010
@broker.subscriber("test")

docs/docs_src/getting_started/dependencies/basic/rabbit/nested_depends.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
55
app = FastStream(broker)
66

7-
def another_dependency():
7+
def another_dependency() -> int:
88
return 1
99

10-
def simple_dependency(b: int = Depends(another_dependency)): # (1)
10+
def simple_dependency(b: int = Depends(another_dependency)) -> int: # (1)
1111
return b * 2
1212

1313
@broker.subscriber("test")
1414
async def handler(
1515
body: dict,
1616
a: int = Depends(another_dependency),
17-
b: int = Depends(simple_dependency)):
18-
assert (a + b) == 3
17+
b: int = Depends(simple_dependency),
18+
):
19+
assert a + b == 3

0 commit comments

Comments
 (0)