Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Running tests with pytest-asyncio 0.23.5.post1 makes test_asyncio.py tests fail due to DeprecationWarning #146

Closed
penguinpee opened this issue Mar 18, 2024 · 0 comments · Fixed by #148

Comments

@penguinpee
Copy link

We intend to update pytest-asyncio to the latest release (0.23.5.post1) in Fedora. Testing packages that depend on it, showed pytest-subprocess tests failing, because warnings are turned into errors as per:

filterwarnings =
error
ignore::pytest.PytestUnraisableExceptionWarning

resulting in:

==================================== ERRORS ====================================
__________________ ERROR at setup of test_basic_usage[shell] ___________________
fixturedef = <FixtureDef argname='event_loop' scope='function' baseid='tests/test_asyncio.py'>
    @pytest.hookimpl(hookwrapper=True)
    def pytest_fixture_setup(
        fixturedef: FixtureDef,
    ) -> Generator[None, Any, None]:
        """Adjust the event loop policy when an event loop is produced."""
        if fixturedef.argname == "event_loop":
            # The use of a fixture finalizer is preferred over the
            # pytest_fixture_post_finalizer hook. The fixture finalizer is invoked once
            # for each fixture, whereas the hook may be invoked multiple times for
            # any specific fixture.
            # see https://github.com/pytest-dev/pytest/issues/5848
            _add_finalizers(
                fixturedef,
                _close_event_loop,
                _restore_event_loop_policy(asyncio.get_event_loop_policy()),
                _provide_clean_event_loop,
            )
            outcome = yield
            loop: asyncio.AbstractEventLoop = outcome.get_result()
            # Weird behavior was observed when checking for an attribute of FixtureDef.func
            # Instead, we now check for a special attribute of the returned event loop
            fixture_filename = inspect.getsourcefile(fixturedef.func)
            if not getattr(loop, "__original_fixture_loop", False):
                _, fixture_line_number = inspect.getsourcelines(fixturedef.func)
>               warnings.warn(
                    _REDEFINED_EVENT_LOOP_FIXTURE_WARNING
                    % (fixture_filename, fixture_line_number),
                    DeprecationWarning,
                )
E               DeprecationWarning: The event_loop fixture provided by pytest-asyncio has been redefined in
E               /builddir/build/BUILD/pytest-subprocess-1.5.0/tests/test_asyncio.py:14
E               Replacing the event_loop fixture with a custom implementation is deprecated
E               and will lead to errors in the future.
E               If you want to request an asyncio event loop with a scope other than function
E               scope, use the "scope" argument to the asyncio mark when marking the tests.
E               If you want to return different types of event loops, use the event_loop_policy
E               fixture.
/usr/lib/python3.12/site-packages/pytest_asyncio/plugin.py:769: DeprecationWarning
_________________ ERROR at teardown of test_basic_usage[shell] _________________
    def _close_event_loop() -> None:
        policy = asyncio.get_event_loop_policy()
        try:
>           loop = policy.get_event_loop()
/usr/lib/python3.12/site-packages/pytest_asyncio/plugin.py:823: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at 0x7fd70d9466c0>
    def get_event_loop(self):
        """Get the event loop for the current context.
    
        Returns an instance of EventLoop or raises an exception.
        """
        if (self._local._loop is None and
                not self._local._set_called and
                threading.current_thread() is threading.main_thread()):
            stacklevel = 2
            try:
                f = sys._getframe(1)
            except AttributeError:
                pass
            else:
                # Move up the call stack so that the warning is attached
                # to the line outside asyncio itself.
                while f:
                    module = f.f_globals.get('__name__')
                    if not (module == 'asyncio' or module.startswith('asyncio.')):
                        break
                    f = f.f_back
                    stacklevel += 1
            import warnings
>           warnings.warn('There is no current event loop',
                          DeprecationWarning, stacklevel=stacklevel)
E           DeprecationWarning: There is no current event loop
/usr/lib64/python3.12/asyncio/events.py:697: DeprecationWarning

(Above is a sample. The same happens for all tests from test_asyncio.py)

We've turned off that setting for now.

@aklajnert aklajnert mentioned this issue Mar 25, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant