Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5fe9712

Browse files
committedJan 20, 2025·
Use contextlib.ExitStack for managing multiple patchers
1 parent 0324153 commit 5fe9712

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed
 

‎backend/app/tests/utils/utils.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import random
22
import string
33
from collections.abc import Generator
4-
from contextlib import contextmanager
4+
from contextlib import ExitStack, contextmanager
55
from unittest.mock import patch
66

77
from fastapi.testclient import TestClient
@@ -36,14 +36,10 @@ def patch_password_hashing(*modules: str) -> Generator[None, None, None]:
3636
:param modules: list of modules to patch.
3737
:return:
3838
"""
39-
patchers = []
40-
for module in modules:
41-
patcher_p = patch(f"{module}.pwd_context.verify", lambda x, y: x == y)
42-
patcher_h = patch(f"{module}.pwd_context.hash", lambda x: x)
43-
patcher_p.start()
44-
patcher_h.start()
45-
46-
patchers.extend((patcher_p, patcher_h))
47-
yield
48-
for patcher in patchers:
49-
patcher.stop()
39+
with ExitStack() as stack:
40+
for module in modules:
41+
stack.enter_context(
42+
patch(f"{module}.pwd_context.verify", lambda x, y: x == y)
43+
)
44+
stack.enter_context(patch(f"{module}.pwd_context.hash", lambda x: x))
45+
yield

0 commit comments

Comments
 (0)
Please sign in to comment.