prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

DeprecationWarning: There is no current event loop #1696

Closed jaraco closed 1 year ago

jaraco commented 1 year ago

Very similar to #1440, I've started getting a renewed deprecation warning on Python 3.11.1 when running xonsh.

Using jaraco/multipy-tox, which recently updated to Python 3.11.1, it's started to emit deprecation warnings:

 multipy-tox main $ docker run -it jaraco/multipy-tox
/root/.local/pipx/venvs/xonsh/lib/python3.11/site-packages/prompt_toolkit/eventloop/utils.py:118: DeprecationWarning: There is no current event loop
  return asyncio.get_event_loop_policy().get_event_loop()
jaraco commented 1 year ago

The issue appears to be that the deprecation warning was re-introduced in Python 3.11.1?

 ~ $ py -3.11 -W error
Python 3.11.0 (main, Oct 26 2022, 19:06:18) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> asyncio.get_event_loop_policy().get_event_loop()
<_UnixSelectorEventLoop running=False closed=False debug=False>
>>> ^D
 ~ $ docker run -it jaraco/multipy-tox py -3.11 -W error
Python 3.11.1 (main, Dec  7 2022, 01:11:34) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> asyncio.get_event_loop_policy().get_event_loop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.11/asyncio/events.py", line 687, in get_event_loop
    warnings.warn('There is no current event loop',
DeprecationWarning: There is no current event loop

In one case, I'm using Python 3.11.0 on my mac. In another, I'm using Python 3.11.1 on Ubuntu, because I don't have ready access to minor version changes on the same platform.

jaraco commented 1 year ago

Likely relevant is https://github.com/python/cpython/issues/93453.

gwhitney commented 1 year ago

For those of us whose package managers dumped us to python 3.10.9 and who use xonsh and are now getting warnings on every xonsh invocation, is there a workaround while the Python folks get this sorted out? Thanks for any thoughts.

gwhitney commented 1 year ago

Oh, to answer my own question, I installed pyenv, did pyenv install 3.10.8 and pyenv global 3.10.8, uninstalled the system xonsh, installed xonsh with pip, and I seem to be back in business until this blows over. (Sorry to clutter your issue, feel free to delete my two comments if you prefer.)

jaraco commented 1 year ago

In the previous issue, I found that adding the following to my .xonshrc suppressed the warnings:

# workaround https://github.com/xonsh/xonsh/issues/4409
__import__('warnings').simplefilter('ignore', DeprecationWarning, 882)

It's also possible to configure a filter by setting a PYTHONWARNINGS env var, probably something like ignore::DeprecationWarning:882 (untested).

However, now that the warnings are on another line (687), it may be necessary to tune the filter and make it less sensitive to line number.

I'll loop back here once homebrew updates the mac to 3.11.1 and I need to apply the change.

jaraco commented 1 year ago

Today homebrew updated and the deprecation error returned. The workaround working for me is:

__import__('warnings').filterwarnings('ignore', 'There is no current event loop', DeprecationWarning, 'prompt_toolkit.eventloop.utils')
Gorgeous-Patrick commented 1 year ago

This problem occurred for me as well when using xonsh.

mattmc3 commented 1 year ago

This was the faulty code last time: https://github.com/prompt-toolkit/python-prompt-toolkit/blob/65c3d0607c69c19d80abb052a18569a2546280e5/src/prompt_toolkit/eventloop/utils.py#L106-L118

I think something must've changed that causes it to hit that except block now.

aufildelanuit commented 1 year ago

I am also observing a DepreciationWarning every time xonsh starts (xonsh 0.13.4 with python 3.11.1).

The same get_event_loop() function seems to be the origin of the DepreciationWarning.

A quick search in the asyncio documentation leads to this note:

In Python versions 3.10.0–3.10.8 and 3.11.0 this function (and other functions which use it implicitly) emitted a DeprecationWarning if there was no running event loop, even if the current loop was set on the policy. In Python versions 3.10.9, 3.11.1 and 3.12 they emit a DeprecationWarning if there is no running event loop and no current loop is set. In some future Python release this will become an error.

I didn't push the investigation further yet, but my guess is that when the get_event_loop() call is made for the first time, there might not be any event loop running yet, which triggers the warning.

Filtering the warning might not be a good idea in the long term, as the underlying cause will end up triggering an error in future versions of asyncio.

tacaswell commented 1 year ago

I believe the relevant bit of code in CPython is:

            import warnings
            warnings.warn('There is no current event loop',
                          DeprecationWarning, stacklevel=stacklevel)
            self.set_event_loop(self.new_event_loop())

which is warning when the get_event_loop() implicitly creates the event loop. I think the fix is to explicitly create the event loop here.

jontwo commented 1 year ago

Is this fix released? I'm still getting the DeprecationWarning for v3.0.37 on Python 3.10.6.

LoicGrobol commented 1 year ago

I also get the warning, but from https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/application/application.py#L947 now.

jaraco commented 1 year ago

Indeed. I reinstalled xonsh today and my workaround is no longer working.

The new workaround is:

# workaround https://github.com/xonsh/xonsh/issues/4409
__import__('warnings').filterwarnings('ignore', 'There is no current event loop', DeprecationWarning, 'prompt_toolkit.application.application')

Please re-open.

tshead2 commented 1 year ago

I just ran into this issue with Python 3.10.9, prompt-toolkit 3.0.38, and xonsh 0.13.4, and can confirm that @jaraco's most recent fix hides the warning.