Skip to content

Commit 0cb2e38

Browse files
committed
Added docs about propagation.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 238a582 commit 0cb2e38

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,54 @@ with graph.sync_ctx() as ctx:
125125
```
126126

127127
The ParamInfo has the information about name and parameters signature. It's useful if you want to create a dependency that changes based on parameter name, or signature.
128+
129+
130+
## Exception propagation
131+
132+
By default if error happens within the context, we send this error to the dependency,
133+
so you can close it properly. You can disable this functionality by setting `exception_propagation` parameter to `False`.
134+
135+
Let's imagine that you want to get a database session from pool and commit after the function is done.
136+
137+
138+
```python
139+
async def get_session():
140+
session = sessionmaker()
141+
142+
yield session
143+
144+
await session.commit()
145+
146+
```
147+
148+
But what if the error happened when the dependant function was called? In this case you want to rollback, instead of commit.
149+
To solve this problem, you can just wrap the `yield` statement in `try except` to handle the error.
150+
151+
```python
152+
async def get_session():
153+
session = sessionmaker()
154+
155+
try:
156+
yield session
157+
except Exception:
158+
await session.rollback()
159+
return
160+
161+
await session.commit()
162+
163+
```
164+
165+
**Also, as a library developer, you can disable exception propagation**. If you do so, then no exception will ever be propagated to dependencies and no such `try except` expression will ever work.
166+
167+
168+
Example of disabled propogation.
169+
170+
```python
171+
172+
graph = DependencyGraph(target_func)
173+
174+
with graph.sync_ctx(exception_propagation=False) as ctx:
175+
print(ctx.resolve_kwargs())
176+
177+
178+
```

taskiq_dependencies/ctx.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def __init__(
2020
self,
2121
graph: "DependencyGraph",
2222
initial_cache: Optional[Dict[Any, Any]] = None,
23-
exception_propogation: bool = True,
23+
exception_propagation: bool = True,
2424
) -> None:
2525
self.graph = graph
2626
self.opened_dependencies: List[Any] = []
2727
self.sub_contexts: "List[Any]" = []
2828
self.initial_cache = initial_cache or {}
29-
self.propogate_excs = exception_propogation
29+
self.propagate_excs = exception_propagation
3030

3131
def traverse_deps( # noqa: C901, WPS210
3232
self,
@@ -133,7 +133,7 @@ def close(self, *args: Any) -> None: # noqa: C901
133133
:param args: exception info if any.
134134
"""
135135
exception_found = False
136-
if args[1] is not None and self.propogate_excs:
136+
if args[1] is not None and self.propagate_excs:
137137
exception_found = True
138138
for ctx in self.sub_contexts:
139139
ctx.close(*args)
@@ -234,7 +234,7 @@ async def close(self, *args: Any) -> None: # noqa: C901
234234
:param args: exception info if any.
235235
"""
236236
exception_found = False
237-
if args[1] is not None and self.propogate_excs:
237+
if args[1] is not None and self.propagate_excs:
238238
exception_found = True
239239
for ctx in self.sub_contexts:
240240
await ctx.close(*args) # type: ignore

taskiq_dependencies/graph.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,43 @@ def is_empty(self) -> bool:
4141
def async_ctx(
4242
self,
4343
initial_cache: Optional[Dict[Any, Any]] = None,
44-
exception_propogation: bool = True,
44+
exception_propagation: bool = True,
4545
) -> AsyncResolveContext:
4646
"""
4747
Create dependency resolver context.
4848
4949
This context is used to actually resolve dependencies.
5050
5151
:param initial_cache: initial cache dict.
52-
:param exception_propogation: If true, all found errors within
53-
context will be propogated to dependencies.
52+
:param exception_propagation: If true, all found errors within
53+
context will be propagated to dependencies.
5454
:return: new resolver context.
5555
"""
5656
return AsyncResolveContext(
5757
self,
5858
initial_cache,
59-
exception_propogation,
59+
exception_propagation,
6060
)
6161

6262
def sync_ctx(
6363
self,
6464
initial_cache: Optional[Dict[Any, Any]] = None,
65-
exception_propogation: bool = True,
65+
exception_propagation: bool = True,
6666
) -> SyncResolveContext:
6767
"""
6868
Create dependency resolver context.
6969
7070
This context is used to actually resolve dependencies.
7171
7272
:param initial_cache: initial cache dict.
73-
:param exception_propogation: If true, all found errors within
74-
context will be propogated to dependencies.
73+
:param exception_propagation: If true, all found errors within
74+
context will be propagated to dependencies.
7575
:return: new resolver context.
7676
"""
7777
return SyncResolveContext(
7878
self,
7979
initial_cache,
80-
exception_propogation,
80+
exception_propagation,
8181
)
8282

8383
def _build_graph(self) -> None: # noqa: C901, WPS210

tests/test_graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def target(_: int = Depends(my_generator)) -> None:
403403

404404

405405
@pytest.mark.anyio
406-
async def test_async_propogation_disabled() -> None:
406+
async def test_async_propagation_disabled() -> None:
407407

408408
errors_found = 0
409409

@@ -420,14 +420,14 @@ def target(_: int = Depends(my_generator)) -> None:
420420

421421
with pytest.raises(ValueError):
422422
async with DependencyGraph(target=target).async_ctx(
423-
exception_propogation=False,
423+
exception_propagation=False,
424424
) as g:
425425
target(**(await g.resolve_kwargs()))
426426

427427
assert errors_found == 0
428428

429429

430-
def test_sync_propogation_disabled() -> None:
430+
def test_sync_propagation_disabled() -> None:
431431

432432
errors_found = 0
433433

@@ -443,7 +443,7 @@ def target(_: int = Depends(my_generator)) -> None:
443443
raise ValueError()
444444

445445
with pytest.raises(ValueError):
446-
with DependencyGraph(target=target).sync_ctx(exception_propogation=False) as g:
446+
with DependencyGraph(target=target).sync_ctx(exception_propagation=False) as g:
447447
target(**(g.resolve_kwargs()))
448448

449449
assert errors_found == 0

0 commit comments

Comments
 (0)