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.1k stars 717 forks source link

"coroutine was never awaited" and "no running event loop" #1828

Open WeepingClown13 opened 6 months ago

WeepingClown13 commented 6 months ago

Hi, I noticed that tests in the questionary library fail with prompt-toolkit 3.0.41. The log is below. Note that the same happens in python3.12 as well as 3.11. I am trying to package questionary in debian and this is the only current blocker, so would appreciate if a quick fix can be added. I did try to go through the library a bit myself but saw that fixing one thing breaks another and that probably someone who understands the library better can help more.

collected 151 items                                                                                                                                                                                                                                                                                                         

tests/test_examples.py ........                                                                                                                                                                                                                                                                                       [  5%]
tests/test_form.py ....                                                                                                                                                                                                                                                                                               [  7%]
tests/test_prompt.py .......                                                                                                                                                                                                                                                                                          [ 12%]
tests/test_question.py .......                                                                                                                                                                                                                                                                                        [ 17%]
tests/test_utils.py ..........                                                                                                                                                                                                                                                                                        [ 23%]
tests/prompts/test_autocomplete.py .......                                                                                                                                                                                                                                                                            [ 28%]
tests/prompts/test_checkbox.py ............................                                                                                                                                                                                                                                                           [ 47%]
tests/prompts/test_common.py .....F....                                                                                                                                                                                                                                                                               [ 53%]
tests/prompts/test_confirm.py ............                                                                                                                                                                                                                                                                            [ 61%]
tests/prompts/test_password.py .                                                                                                                                                                                                                                                                                      [ 62%]
tests/prompts/test_path.py .......                                                                                                                                                                                                                                                                                    [ 66%]
tests/prompts/test_press_any_key_to_continue.py .                                                                                                                                                                                                                                                                     [ 67%]
tests/prompts/test_rawselect.py ..........                                                                                                                                                                                                                                                                            [ 74%]
tests/prompts/test_select.py ...................................                                                                                                                                                                                                                                                      [ 97%]
tests/prompts/test_text.py ....                                                                                                                                                                                                                                                                                       [100%]

========================================================================================================================================================= FAILURES ==========================================================================================================================================================
____________________________________________________________________________________________________________________________________________________ test_blank_line_fix ____________________________________________________________________________________________________________________________________________________

    def test_blank_line_fix():
        def get_prompt_tokens():
            return [("class:question", "What is your favourite letter?")]

        ic = InquirerControl(["a", "b", "c"])

        def run(inp):
            inp.send_text("")
            layout = common.create_inquirer_layout(
                ic, get_prompt_tokens, input=inp, output=DummyOutput()
            )

            # usually this would be 2000000000000000000000000000000
            # but `common._fix_unecessary_blank_lines` makes sure
            # the main window is not as greedy (avoiding blank lines)
            assert (
                layout.container.preferred_height(100, 200).max
                == 1000000000000000000000000000001
            )

>       execute_with_input_pipe(run)

tests/prompts/test_common.py:89: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tests/utils.py:90: in execute_with_input_pipe
    return func(inp)
tests/prompts/test_common.py:84: in run
    assert (
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:319: in preferred_height
    c.preferred_height(width, max_available_height) for c in self._all_children
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:319: in preferred_height
    c.preferred_height(width, max_available_height) for c in self._all_children
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:786: in preferred_height
    return self.content.preferred_height(width, max_available_height)
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:319: in preferred_height
    c.preferred_height(width, max_available_height) for c in self._all_children
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:2629: in preferred_height
    return self.content.preferred_height(width, max_available_height)
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:1611: in preferred_height
    return self._merge_dimensions(
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:1639: in _merge_dimensions
    preferred = get_preferred()
/usr/lib/python3/dist-packages/prompt_toolkit/layout/containers.py:1604: in preferred_content_height
    return self.content.preferred_height(
/usr/lib/python3/dist-packages/prompt_toolkit/layout/controls.py:620: in preferred_height
    content = self.create_content(width, height=1)  # Pass a dummy '1' as height.
/usr/lib/python3/dist-packages/prompt_toolkit/layout/controls.py:721: in create_content
    buffer.load_history_if_not_yet_loaded()
/usr/lib/python3/dist-packages/prompt_toolkit/buffer.py:401: in load_history_if_not_yet_loaded
    self._load_history_task = get_app().create_background_task(load_history())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <prompt_toolkit.application.dummy.DummyApplication object at 0x7f62cd8ba3f0>, coroutine = <coroutine object Buffer.load_history_if_not_yet_loaded.<locals>.load_history at 0x7f62cdfcca00>

    def create_background_task(
        self, coroutine: Coroutine[Any, Any, None]
    ) -> asyncio.Task[None]:
        """
        Start a background task (coroutine) for the running application. When
        the `Application` terminates, unfinished background tasks will be
        cancelled.

        Given that we still support Python versions before 3.11, we can't use
        task groups (and exception groups), because of that, these background
        tasks are not allowed to raise exceptions. If they do, we'll call the
        default exception handler from the event loop.

        If at some point, we have Python 3.11 as the minimum supported Python
        version, then we can use a `TaskGroup` (with the lifetime of
        `Application.run_async()`, and run run the background tasks in there.

        This is not threadsafe.
        """
>       loop = self.loop or get_running_loop()
E       RuntimeError: no running event loop

/usr/lib/python3/dist-packages/prompt_toolkit/application/application.py:1147: RuntimeError
===================================================================================================================================================== warnings summary ======================================================================================================================================================
tests/prompts/test_confirm.py::test_confirm_enter_default_yes
  /usr/lib/python3/dist-packages/prompt_toolkit/key_binding/key_bindings.py:277: RuntimeWarning: coroutine 'Buffer.load_history_if_not_yet_loaded.<locals>.load_history' was never awaited
    keys = tuple(_parse_key(k) for k in keys)
  Enable tracemalloc to get traceback where the object was allocated.
  See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
FantasqueX commented 6 months ago

I'm investigating this as well. It's introduced in this commit https://github.com/prompt-toolkit/python-prompt-toolkit/commit/a7759969891c54ea56abfa286524cc301eedaf05