From c4c95dad98577148daa09a985270b5faf948bd81 Mon Sep 17 00:00:00 2001 From: Letu Ren Date: Sat, 30 Dec 2023 20:13:30 +0800 Subject: [PATCH] Try to support prompt_toolkit >3.0.37 Currently one test will fail with latest prompt_toolkit(3.0.43). test_blank_line_fix will throw RuntimeError: no running event loop. By bisecting commit, the culprit is https://github.com/prompt-toolkit/python-prompt-toolkit/commit/a7759969891c54ea56abfa286524cc301eedaf05. This commit replaces custom `get_event_loop` with `asyncio.get_event_loop`. The former will creator a new loop if `asyncio.get_running_loop` fails while the latter won't. I mimic the changes in the examples to use `asyncio.run` and the test passes. I'm not sure whether more changes are needed. Fixes: https://github.com/tmbo/questionary/issues/344 --- poetry.lock | 10 +++---- pyproject.toml | 2 +- tests/prompts/test_common.py | 53 ++++++++++++++++++++++++++---------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/poetry.lock b/poetry.lock index f82de67c..ff1ec6f0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -577,13 +577,13 @@ virtualenv = ">=20.10.0" [[package]] name = "prompt-toolkit" -version = "3.0.36" +version = "3.0.43" description = "Library for building powerful interactive command lines in Python" optional = false -python-versions = ">=3.6.2" +python-versions = ">=3.7.0" files = [ - {file = "prompt_toolkit-3.0.36-py3-none-any.whl", hash = "sha256:aa64ad242a462c5ff0363a7b9cfe696c20d55d9fc60c11fd8e632d064804d305"}, - {file = "prompt_toolkit-3.0.36.tar.gz", hash = "sha256:3e163f254bef5a03b146397d7c1963bd3e2812f0964bb9a24e6ec761fd28db63"}, + {file = "prompt_toolkit-3.0.43-py3-none-any.whl", hash = "sha256:a11a29cb3bf0a28a387fe5122cdb649816a957cd9261dcedf8c9f1fef33eacf6"}, + {file = "prompt_toolkit-3.0.43.tar.gz", hash = "sha256:3527b7af26106cbc65a040bcc84839a3566ec1b051bb0bfe953631e704b0ff7d"}, ] [package.dependencies] @@ -1094,4 +1094,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = ">=3.8" -content-hash = "69da0b625156d12413563199c39faece347a06ef44da899307205c0798ad6d0f" +content-hash = "eb9c4e36a3591050ff87532d3cf31a6a4c013872a47e16fcc345165676d9ad6b" diff --git a/pyproject.toml b/pyproject.toml index 681f6267..9b67b21c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ license = "MIT" [tool.poetry.dependencies] python = ">=3.8" -prompt_toolkit = ">=2.0,<=3.0.36" # once https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1726 is fixed, this can be changed to ">=2.0,<4.0" +prompt_toolkit = ">=2.0,<4.0" # once https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1726 is fixed, this can be changed to ">=2.0,<4.0" [tool.poetry.group.docs] optional = true diff --git a/tests/prompts/test_common.py b/tests/prompts/test_common.py index d6ce51e2..e7d86287 100644 --- a/tests/prompts/test_common.py +++ b/tests/prompts/test_common.py @@ -1,11 +1,14 @@ +import asyncio from unittest.mock import Mock from unittest.mock import call import pytest +import prompt_toolkit from prompt_toolkit.document import Document from prompt_toolkit.output import DummyOutput from prompt_toolkit.styles import Attrs from prompt_toolkit.validation import ValidationError +from prompt_toolkit.input.defaults import create_pipe_input from prompt_toolkit.validation import Validator from questionary import Choice @@ -72,21 +75,41 @@ def get_prompt_tokens(): 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) + prompt_toolkit_version = tuple([int(v) for v in prompt_toolkit.VERSION]) + + if prompt_toolkit_version >= (3, 0, 37): + async 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 + ) + with create_pipe_input() as inp: + asyncio.run(run(inp)) + + else: + 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) def test_prompt_highlight_coexist():