Skip to content

Commit e9965b5

Browse files
committed
refactor: Synchronization wrappers of test items are temporary.
Previously, synchronizers modified the obj attribute of a function item permanently. This could possibly result in multiple levels of wrapping. This patch restores the original function object after the test finished.
1 parent 8dee421 commit e9965b5

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

pytest_asyncio/plugin.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,10 @@ def _can_substitute(item: Function) -> bool:
451451
return inspect.iscoroutinefunction(func)
452452

453453
def runtest(self) -> None:
454-
self.obj = wrap_in_sync(
455-
# https://github.com/pytest-dev/pytest-asyncio/issues/596
456-
self.obj, # type: ignore[has-type]
457-
)
458-
super().runtest()
454+
synchronized_obj = wrap_in_sync(self.obj)
455+
with MonkeyPatch.context() as c:
456+
c.setattr(self, "obj", synchronized_obj)
457+
super().runtest()
459458

460459

461460
class AsyncGenerator(PytestAsyncioFunction):
@@ -494,11 +493,10 @@ def _can_substitute(item: Function) -> bool:
494493
)
495494

496495
def runtest(self) -> None:
497-
self.obj = wrap_in_sync(
498-
# https://github.com/pytest-dev/pytest-asyncio/issues/596
499-
self.obj, # type: ignore[has-type]
500-
)
501-
super().runtest()
496+
synchronized_obj = wrap_in_sync(self.obj)
497+
with MonkeyPatch.context() as c:
498+
c.setattr(self, "obj", synchronized_obj)
499+
super().runtest()
502500

503501

504502
class AsyncHypothesisTest(PytestAsyncioFunction):
@@ -517,10 +515,10 @@ def _can_substitute(item: Function) -> bool:
517515
)
518516

519517
def runtest(self) -> None:
520-
self.obj.hypothesis.inner_test = wrap_in_sync(
521-
self.obj.hypothesis.inner_test,
522-
)
523-
super().runtest()
518+
synchronized_obj = wrap_in_sync(self.obj.hypothesis.inner_test)
519+
with MonkeyPatch.context() as c:
520+
c.setattr(self.obj.hypothesis, "inner_test", synchronized_obj)
521+
super().runtest()
524522

525523

526524
# The function name needs to start with "pytest_"

0 commit comments

Comments
 (0)