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

Open
#1,828 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
cli, testing-qa

Research direction

Reproduce the failure with the tests in tests/prompts/test_common.py, using tests/utils.py:execute_with_input_pipe as the entry point. Inspect the interaction between the layout test and prompt-toolkit's background task handling, then verify that test_blank_line_fix passes without the no-running-event-loop error or unawaited-coroutine warning.

Written by the indexing model from the issue text.

Description

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
Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prompt-toolkit/python-prompt-toolkit

All issues in prompt-toolkit/python-prompt-toolkit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.