Skip to content

Commit 0f18a45

Browse files
committed
[fix] Avoid trying to install scoped event loops for unknown test collector types.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 2301f95 commit 0f18a45

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pytest_asyncio/plugin.py

+19-21
Original file line numberDiff line numberDiff line change
@@ -608,21 +608,7 @@ def scoped_event_loop(
608608
# know it exists. We work around this by attaching the fixture function to the
609609
# collected Python object, where it will be picked up by pytest.Class.collect()
610610
# or pytest.Module.collect(), respectively
611-
if type(collector) is Module:
612-
# Accessing Module.obj triggers a module import executing module-level
613-
# statements. A module-level pytest.skip statement raises the "Skipped"
614-
# OutcomeException or a Collector.CollectError, if the "allow_module_level"
615-
# kwargs is missing. These cases are handled correctly when they happen inside
616-
# Collector.collect(), but this hook runs before the actual collect call.
617-
# Therefore, we monkey patch Module.collect to add the scoped fixture to the
618-
# module before it runs the actual collection.
619-
def _patched_collect():
620-
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
621-
return collector.__original_collect()
622-
623-
collector.__original_collect = collector.collect
624-
collector.collect = _patched_collect
625-
elif type(collector) is Package:
611+
if type(collector) is Package:
626612

627613
def _patched_collect():
628614
# When collector is a Package, collector.obj is the package's
@@ -648,12 +634,24 @@ def _patched_collect():
648634

649635
collector.__original_collect = collector.collect
650636
collector.collect = _patched_collect
651-
else:
652-
pyobject = collector.obj
653-
# If the collected module is a DoctestTextfile, collector.obj is None
654-
if pyobject is None:
655-
return
656-
pyobject.__pytest_asyncio_scoped_event_loop = scoped_event_loop
637+
elif isinstance(collector, Module):
638+
# Accessing Module.obj triggers a module import executing module-level
639+
# statements. A module-level pytest.skip statement raises the "Skipped"
640+
# OutcomeException or a Collector.CollectError, if the "allow_module_level"
641+
# kwargs is missing. These cases are handled correctly when they happen inside
642+
# Collector.collect(), but this hook runs before the actual collect call.
643+
# Therefore, we monkey patch Module.collect to add the scoped fixture to the
644+
# module before it runs the actual collection.
645+
def _patched_collect():
646+
# If the collected module is a DoctestTextfile, collector.obj is None
647+
if collector.obj is not None:
648+
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
649+
return collector.__original_collect()
650+
651+
collector.__original_collect = collector.collect
652+
collector.collect = _patched_collect
653+
elif isinstance(collector, Class):
654+
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
657655

658656

659657
def _removesuffix(s: str, suffix: str) -> str:

0 commit comments

Comments
 (0)