Skip to content

Commit ef202eb

Browse files
committed
refactor: Removed obsolete fixture handling hook for *event_loop*.
1 parent e869ce8 commit ef202eb

File tree

1 file changed

+0
-120
lines changed

1 file changed

+0
-120
lines changed

pytest_asyncio/plugin.py

Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
Mapping,
2323
Sequence,
2424
)
25-
from textwrap import dedent
2625
from typing import (
2726
Any,
2827
Callable,
@@ -788,20 +787,6 @@ def _temporary_event_loop_policy(policy: AbstractEventLoopPolicy) -> Iterator[No
788787
asyncio.set_event_loop(old_loop)
789788

790789

791-
_REDEFINED_EVENT_LOOP_FIXTURE_WARNING = dedent(
792-
"""\
793-
The event_loop fixture provided by pytest-asyncio has been redefined in
794-
%s:%d
795-
Replacing the event_loop fixture with a custom implementation is deprecated
796-
and will lead to errors in the future.
797-
If you want to request an asyncio event loop with a scope other than function
798-
scope, use the "loop_scope" argument to the asyncio mark when marking the tests.
799-
If you want to return different types of event loops, use the event_loop_policy
800-
fixture.
801-
"""
802-
)
803-
804-
805790
@pytest.hookimpl(tryfirst=True)
806791
def pytest_generate_tests(metafunc: Metafunc) -> None:
807792
marker = metafunc.definition.get_closest_marker("asyncio")
@@ -841,51 +826,6 @@ def pytest_generate_tests(metafunc: Metafunc) -> None:
841826
)
842827

843828

844-
@pytest.hookimpl(hookwrapper=True)
845-
def pytest_fixture_setup(
846-
fixturedef: FixtureDef,
847-
) -> Generator[None, pluggy.Result, None]:
848-
"""Adjust the event loop policy when an event loop is produced."""
849-
if fixturedef.argname == "event_loop":
850-
# The use of a fixture finalizer is preferred over the
851-
# pytest_fixture_post_finalizer hook. The fixture finalizer is invoked once
852-
# for each fixture, whereas the hook may be invoked multiple times for
853-
# any specific fixture.
854-
# see https://github.com/pytest-dev/pytest/issues/5848
855-
_add_finalizers(
856-
fixturedef,
857-
_close_event_loop,
858-
_restore_event_loop_policy(asyncio.get_event_loop_policy()),
859-
_provide_clean_event_loop,
860-
)
861-
outcome = yield
862-
loop: asyncio.AbstractEventLoop = outcome.get_result()
863-
# Weird behavior was observed when checking for an attribute of FixtureDef.func
864-
# Instead, we now check for a special attribute of the returned event loop
865-
fixture_filename = inspect.getsourcefile(fixturedef.func)
866-
if not _is_pytest_asyncio_loop(loop):
867-
_, fixture_line_number = inspect.getsourcelines(fixturedef.func)
868-
warnings.warn(
869-
_REDEFINED_EVENT_LOOP_FIXTURE_WARNING
870-
% (fixture_filename, fixture_line_number),
871-
DeprecationWarning,
872-
)
873-
policy = asyncio.get_event_loop_policy()
874-
try:
875-
old_loop = _get_event_loop_no_warn(policy)
876-
if old_loop is not loop and not _is_pytest_asyncio_loop(old_loop):
877-
old_loop.close()
878-
except RuntimeError:
879-
# Either the current event loop has been set to None
880-
# or the loop policy doesn't specify to create new loops
881-
# or we're not in the main thread
882-
pass
883-
policy.set_event_loop(loop)
884-
return
885-
886-
yield
887-
888-
889829
def _make_pytest_asyncio_loop(loop: AbstractEventLoop) -> AbstractEventLoop:
890830
loop.__pytest_asyncio = True # type: ignore[attr-defined]
891831
return loop
@@ -895,19 +835,6 @@ def _is_pytest_asyncio_loop(loop: AbstractEventLoop) -> bool:
895835
return getattr(loop, "__pytest_asyncio", False)
896836

897837

898-
def _add_finalizers(fixturedef: FixtureDef, *finalizers: Callable[[], object]) -> None:
899-
"""
900-
Registers the specified fixture finalizers in the fixture.
901-
902-
Finalizers need to be specified in the exact order in which they should be invoked.
903-
904-
:param fixturedef: Fixture definition which finalizers should be added to
905-
:param finalizers: Finalizers to be added
906-
"""
907-
for finalizer in reversed(finalizers):
908-
fixturedef.addfinalizer(finalizer)
909-
910-
911838
_UNCLOSED_EVENT_LOOP_WARNING = dedent(
912839
"""\
913840
pytest-asyncio detected an unclosed event loop when tearing down the event_loop
@@ -922,53 +849,6 @@ def _add_finalizers(fixturedef: FixtureDef, *finalizers: Callable[[], object]) -
922849
)
923850

924851

925-
def _close_event_loop() -> None:
926-
policy = asyncio.get_event_loop_policy()
927-
try:
928-
loop = policy.get_event_loop()
929-
except RuntimeError:
930-
loop = None
931-
if loop is not None and not _is_pytest_asyncio_loop(loop):
932-
if not loop.is_closed():
933-
warnings.warn(
934-
_UNCLOSED_EVENT_LOOP_WARNING % loop,
935-
DeprecationWarning,
936-
)
937-
loop.close()
938-
939-
940-
def _restore_event_loop_policy(previous_policy) -> Callable[[], None]:
941-
def _restore_policy():
942-
# Close any event loop associated with the old loop policy
943-
# to avoid ResourceWarnings in the _provide_clean_event_loop finalizer
944-
try:
945-
loop = _get_event_loop_no_warn(previous_policy)
946-
except RuntimeError:
947-
loop = None
948-
if loop and not _is_pytest_asyncio_loop(loop):
949-
loop.close()
950-
asyncio.set_event_loop_policy(previous_policy)
951-
952-
return _restore_policy
953-
954-
955-
def _provide_clean_event_loop() -> None:
956-
# At this point, the event loop for the current thread is closed.
957-
# When a user calls asyncio.get_event_loop(), they will get a closed loop.
958-
# In order to avoid this side effect from pytest-asyncio, we need to replace
959-
# the current loop with a fresh one.
960-
# Note that we cannot set the loop to None, because get_event_loop only creates
961-
# a new loop, when set_event_loop has not been called.
962-
policy = asyncio.get_event_loop_policy()
963-
try:
964-
old_loop = _get_event_loop_no_warn(policy)
965-
except RuntimeError:
966-
old_loop = None
967-
if old_loop is not None and not _is_pytest_asyncio_loop(old_loop):
968-
new_loop = policy.new_event_loop()
969-
policy.set_event_loop(new_loop)
970-
971-
972852
def _get_event_loop_no_warn(
973853
policy: AbstractEventLoopPolicy | None = None,
974854
) -> asyncio.AbstractEventLoop:

0 commit comments

Comments
 (0)