@@ -646,17 +646,6 @@ def pytest_fixture_setup(
646
646
) -> Optional [object ]:
647
647
"""Adjust the event loop policy when an event loop is produced."""
648
648
if fixturedef .argname == "event_loop" :
649
- # FixtureDef.baseid is an empty string when the Fixture was found in a plugin.
650
- # This is also true, when the fixture was defined in a conftest.py
651
- # at the rootdir.
652
- fixture_filename = inspect .getsourcefile (fixturedef .func )
653
- if not getattr (fixturedef .func , "__original_func" , False ):
654
- _ , fixture_line_number = inspect .getsourcelines (fixturedef .func )
655
- warnings .warn (
656
- _REDEFINED_EVENT_LOOP_FIXTURE_WARNING
657
- % (fixture_filename , fixture_line_number ),
658
- DeprecationWarning ,
659
- )
660
649
# The use of a fixture finalizer is preferred over the
661
650
# pytest_fixture_post_finalizer hook. The fixture finalizer is invoked once
662
651
# for each fixture, whereas the hook may be invoked multiple times for
@@ -669,6 +658,16 @@ def pytest_fixture_setup(
669
658
)
670
659
outcome = yield
671
660
loop = outcome .get_result ()
661
+ # Weird behavior was observed when checking for an attribute of FixtureDef.func
662
+ # Instead, we now check for a special attribute of the returned event loop
663
+ fixture_filename = inspect .getsourcefile (fixturedef .func )
664
+ if not getattr (loop , "__original_fixture_loop" , False ):
665
+ _ , fixture_line_number = inspect .getsourcelines (fixturedef .func )
666
+ warnings .warn (
667
+ _REDEFINED_EVENT_LOOP_FIXTURE_WARNING
668
+ % (fixture_filename , fixture_line_number ),
669
+ DeprecationWarning ,
670
+ )
672
671
policy = asyncio .get_event_loop_policy ()
673
672
try :
674
673
with warnings .catch_warnings ():
@@ -836,13 +835,13 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
836
835
@pytest .fixture
837
836
def event_loop (request : FixtureRequest ) -> Iterator [asyncio .AbstractEventLoop ]:
838
837
"""Create an instance of the default event loop for each test case."""
839
- # Add a magic value to the fixture function, so that we can check for overrides
840
- # of this fixture in pytest_fixture_setup
841
- # The magic value must be part of the function definition, because pytest may have
842
- # multiple instances of the fixture function
843
- event_loop .__original_func = True
844
-
845
838
loop = asyncio .get_event_loop_policy ().new_event_loop ()
839
+ # Add a magic value to the event loop, so pytest-asyncio can determine if the
840
+ # event_loop fixture was overridden. Other implementations of event_loop don't
841
+ # set this value.
842
+ # The magic value must be set as part of the function definition, because pytest
843
+ # seems to have multiple instances of the same FixtureDef or fixture function
844
+ loop .__original_fixture_loop = True # type: ignore[attr-defined]
846
845
yield loop
847
846
loop .close ()
848
847
0 commit comments