Skip to content

Commit 0c931b7

Browse files
committed
refactor: Extracted function to check if a loop was created by pytest-asyncio.
1 parent 0642dcd commit 0c931b7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

pytest_asyncio/plugin.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,7 @@ def pytest_fixture_setup(
872872
policy = asyncio.get_event_loop_policy()
873873
try:
874874
old_loop = _get_event_loop_no_warn(policy)
875-
is_pytest_asyncio_loop = getattr(old_loop, "__pytest_asyncio", False)
876-
if old_loop is not loop and not is_pytest_asyncio_loop:
875+
if old_loop is not loop and not _is_pytest_asyncio_loop(old_loop):
877876
old_loop.close()
878877
except RuntimeError:
879878
# Either the current event loop has been set to None
@@ -886,6 +885,10 @@ def pytest_fixture_setup(
886885
yield
887886

888887

888+
def _is_pytest_asyncio_loop(loop: AbstractEventLoop) -> bool:
889+
return getattr(loop, "__pytest_asyncio", False)
890+
891+
889892
def _add_finalizers(fixturedef: FixtureDef, *finalizers: Callable[[], object]) -> None:
890893
"""
891894
Registers the specified fixture finalizers in the fixture.
@@ -919,7 +922,7 @@ def _close_event_loop() -> None:
919922
loop = policy.get_event_loop()
920923
except RuntimeError:
921924
loop = None
922-
if loop is not None and not getattr(loop, "__pytest_asyncio", False):
925+
if loop is not None and not _is_pytest_asyncio_loop(loop):
923926
if not loop.is_closed():
924927
warnings.warn(
925928
_UNCLOSED_EVENT_LOOP_WARNING % loop,
@@ -936,7 +939,7 @@ def _restore_policy():
936939
loop = _get_event_loop_no_warn(previous_policy)
937940
except RuntimeError:
938941
loop = None
939-
if loop and not getattr(loop, "__pytest_asyncio", False):
942+
if loop and not _is_pytest_asyncio_loop(loop):
940943
loop.close()
941944
asyncio.set_event_loop_policy(previous_policy)
942945

@@ -955,7 +958,7 @@ def _provide_clean_event_loop() -> None:
955958
old_loop = _get_event_loop_no_warn(policy)
956959
except RuntimeError:
957960
old_loop = None
958-
if old_loop is not None and not getattr(old_loop, "__pytest_asyncio", False):
961+
if old_loop is not None and not _is_pytest_asyncio_loop(old_loop):
959962
new_loop = policy.new_event_loop()
960963
policy.set_event_loop(new_loop)
961964

0 commit comments

Comments
 (0)