@@ -608,21 +608,7 @@ def scoped_event_loop(
608
608
# know it exists. We work around this by attaching the fixture function to the
609
609
# collected Python object, where it will be picked up by pytest.Class.collect()
610
610
# 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 :
626
612
627
613
def _patched_collect ():
628
614
# When collector is a Package, collector.obj is the package's
@@ -648,12 +634,24 @@ def _patched_collect():
648
634
649
635
collector .__original_collect = collector .collect
650
636
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
657
655
658
656
659
657
def _removesuffix (s : str , suffix : str ) -> str :
0 commit comments