jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1328288?usp=email )
Change subject: input_list_choice: Return non-option defaults
......................................................................
input_list_choice: Return non-option defaults
Honor non-numeric defaults outside the answer sequence when the user
presses Enter or force mode is enabled. Keep numeric defaults as
one-based list indexes.
Bug: T305937
Change-Id: I7638cf3f156f11b04ab7dad8d081381ccb33702b
---
M pywikibot/bot.py
M pywikibot/userinterfaces/terminal_interface_base.py
M tests/ui_tests.py
3 files changed, 40 insertions(+), 4 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 407aeef..5064c24 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -662,7 +662,7 @@
answers: AnswerType,
default: int | str | None = None,
force: bool = False) -> str:
- """Ask the user the question and return one of the valid answers.
+ """Ask the user the question and return an answer or the default.
:param question: The question asked without trailing spaces.
:param answers: The valid answers each containing a full length
@@ -670,7 +670,7 @@
:param default: The result if no answer was entered. It must not be
in the valid answers and can be disabled by setting it to None.
:param force: Automatically use the default
- :return: The selected answer.
+ :return: The selected answer or the default.
"""
assert ui is not None
return ui.input_list_choice(question, answers, default=default,
diff --git a/pywikibot/userinterfaces/terminal_interface_base.py
b/pywikibot/userinterfaces/terminal_interface_base.py
index e3cb43f..e86e4b7 100644
--- a/pywikibot/userinterfaces/terminal_interface_base.py
+++ b/pywikibot/userinterfaces/terminal_interface_base.py
@@ -512,14 +512,14 @@
def input_list_choice(self, question: str, answers: Sequence[Any],
default: int | str | None = None,
force: bool = False) -> Any:
- """Ask the user to select one entry from a list of entries.
+ """Ask the user to select an entry or accept the default.
:param question: The question, without trailing whitespace.
:param answers: A sequence of options to be chosen.
:param default: The default answer if no was entered. None to
require an answer.
:param force: Automatically use the default.
- :return: Return a single Sequence entry.
+ :return: Return a single sequence entry or the default.
"""
# lock stream output
with self.lock:
@@ -536,6 +536,8 @@
except (TypeError, ValueError):
if choice in answers:
return choice
+ if default is not None and choice == default:
+ return default
parsedchoice = -1
# User typed choice number
diff --git a/tests/ui_tests.py b/tests/ui_tests.py
index 364fa0f..8f9f45b 100755
--- a/tests/ui_tests.py
+++ b/tests/ui_tests.py
@@ -292,6 +292,40 @@
+ 'question (default: 2): ')
self.assertEqual(rv, 'answer 2')
+ def test_input_list_choice_non_option_default(self) -> None:
+ """Test input_list_choice with a default outside the options."""
+ options = ('foo', 'bar', None)
+ self.strin.write('\n2\n')
+ self.strin.seek(0)
+
+ rv = pywikibot.bot.input_list_choice(
+ 'question', options, default='baz')
+
+ self.assertEqual(self.strout.getvalue(), '')
+ self.assertEqual(
+ self.strerr.getvalue(),
+ ''.join(f'{num}: {item}\n'
+ for num, item in enumerate(options, start=1))
+ + 'question (default: baz): ')
+ self.assertEqual(rv, 'baz')
+
+ def test_input_list_choice_forced_non_option_default(self) -> None:
+ """Test forced input_list_choice with an external default."""
+ rv = pywikibot.bot.input_list_choice(
+ 'question', ('foo', 'bar'), default='baz', force=True)
+
+ self.assertEqual(self.strout.getvalue(), '')
+ self.assertEqual(
+ self.strerr.getvalue(), 'question (default: baz):\n')
+ self.assertEqual(rv, 'baz')
+
+ def test_input_list_choice_forced_invalid_numeric_default(self) -> None:
+ """Test forced input_list_choice with an invalid list index."""
+ with self.assertRaisesRegex(
+ ValueError, 'Invalid value "3" for default during force.'):
+ pywikibot.bot.input_list_choice(
+ 'question', ('foo', 'bar'), default='3', force=True)
+
@unittest.skipUnless(os.name == 'posix', 'requires Unix console')
class TestTerminalOutputColorUnix(UITestCase):
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1328288?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I7638cf3f156f11b04ab7dad8d081381ccb33702b
Gerrit-Change-Number: 1328288
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]