← pod_r2_v1f

rich_3052

failed NO_PATCH NO_PATCH UNSUBMITTED empty_patch(nudges_exhausted) · 8 tool calls · 213 s · Textualize/rich

Task input

Case insensitive prompt choices

## Type of changes

- [ ] Bug fix
- [x] New feature
- [ ] Documentation / docstrings
- [ ] Tests
- [ ] Other

## Checklist

- [x] I've run the latest [black](https://github.com/psf/black) with default args on new code.
- [x] I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
- [x] I've added tests for new code.
- [x] I accept that @willmcgugan may be pedantic in the code review.

## Description

Optional flag for `Prompt.ask` allowing case-insensitive responses. Just add `case_sensitive=False` to the command options:

```python
from rich.prompt import Prompt
name = Prompt.ask("Enter your name", choices=["Paul", "Jessica", "Duncan"], default="Paul", case_sensitive=False)
```

Now, it would accept "paul" or "Paul" as valid responses. Defaults to True, which is the usual result.


Tool calls (8)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"Prompt.ask\" rich/prompt.py"
}
{"status": "ok", "stdout": "rich/prompt.py:122: >>> filename = Prompt.ask(\"Enter a filename\")\nrich/prompt.py:290: >>> name = Prompt.ask(\"Enter your name\")\
full
{"status": "ok", "stdout": "rich/prompt.py:122:            >>> filename = Prompt.ask(\"Enter a filename\")\nrich/prompt.py:290:        >>> name = Prompt.ask(\"Enter your name\")\nrich/prompt.py:302:        >>> burrito_count = IntPrompt.ask(\"How many burritos do you want to order\")\nrich/prompt.py:314:        >>> temperature = FloatPrompt.ask(\"Enter desired temperature\")\nrich/prompt.py:354:            result = IntPrompt.ask(\nrich/prompt.py:363:            password = Prompt.ask(\nrich/prompt.py:372:        fruit = Prompt.ask(\"Enter a fruit\", choices=[\"apple\", \"orange\", \"pear\"])\n", "stderr": "", "exit_code": 0}
2run_command
{
 "command": "sed -n '1,400p' rich/prompt.py"
}
{"status": "ok", "stdout": "from typing import Any, Generic, List, Optional, TextIO, TypeVar, Union, overload\n\nfrom . import get_console\nfrom .console import
full
{"status": "ok", "stdout": "from typing import Any, Generic, List, Optional, TextIO, TypeVar, Union, overload\n\nfrom . import get_console\nfrom .console import Console\nfrom .text import Text, TextType\n\nPromptType = TypeVar(\"PromptType\")\nDefaultType = TypeVar(\"DefaultType\")\n\n\nclass PromptError(Exception):\n    \"\"\"Exception base class for prompt related errors.\"\"\"\n\n\nclass InvalidResponse(PromptError):\n    \"\"\"Exception to indicate a response was invalid. Raise this within process_response() to indicate an error\n    and provide an error message.\n\n    Args:\n        message (Union[str, Text]): Error message.\n    \"\"\"\n\n    def __init__(self, message: TextType) -> None:\n        self.message = message\n\n    def __rich__(self) -> TextType:\n        return self.message\n\n\nclass PromptBase(Generic[PromptType]):\n    \"\"\"Ask the user for input until a valid response is received. This is the base class, see one of\n    the concrete classes for examples.\n\n    Args:\n        prompt (TextType, optional): Prompt text. Defaults to \"\".\n        console (Console, optional): A Console instance or None to use global console. Defaults to None.\n        password (bool, optional): Enable password input. Defaults to False.\n        choices (List[str], optional): A list of valid choices. Defaults to None.\n        show_default (bool, optional): Show default in prompt. Defaults to True.\n        show_choices (bool, optional): Show choices in prompt. Defaults to True.\n    \"\"\"\n\n    response_type: type = str\n\n    validate_error_message = \"[prompt.invalid]Please enter a valid value\"\n    illegal_choice_message = (\n        \"[prompt.invalid.choice]Please select one of the available options\"\n    )\n    prompt_suffix = \": \"\n\n    choices: Optional[List[str]] = None\n\n    def __init__(\n        self,\n        prompt: TextType = \"\",\n        *,\n        console: Optional[Console] = None,\n        password: bool = False,\n        choices: Opt
3run_command
{
 "command": "sed -n '401,600p' rich/prompt.py"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
4run_command
{
 "command": "sed -n '1,600p' rich/prompt.py | grep -n \"def __call__\" -A 50"
}
{"status": "ok", "stdout": "253: def __call__(self, *, stream: Optional[TextIO] = None) -> PromptType:\n254- ...\n255-\n256- @overload\n257: def __call__(\n258-
full
{"status": "ok", "stdout": "253:    def __call__(self, *, stream: Optional[TextIO] = None) -> PromptType:\n254-        ...\n255-\n256-    @overload\n257:    def __call__(\n258-        self, *, default: DefaultType, stream: Optional[TextIO] = None\n259-    ) -> Union[PromptType, DefaultType]:\n260-        ...\n261-\n262:    def __call__(self, *, default: Any = ..., stream: Optional[TextIO] = None) -> Any:\n263-        \"\"\"Run the prompt loop.\n264-\n265-        Args:\n266-            default (Any, optional): Optional default value.\n267-\n268-        Returns:\n269-            PromptType: Processed value.\n270-        \"\"\"\n271-        while True:\n272-            self.pre_prompt()\n273-            prompt = self.make_prompt(default)\n274-            value = self.get_input(self.console, prompt, self.password, stream=stream)\n275-            if value == \"\" and default != ...:\n276-                return default\n277-            try:\n278-                return_value = self.process_response(value)\n279-            except InvalidResponse as error:\n280-                self.on_validate_error(value, error)\n281-                continue\n282-            else:\n283-                return return_value\n284-\n285-\n286-class Prompt(PromptBase[str]):\n287-    \"\"\"A prompt that returns a str.\n288-\n289-    Example:\n290-        >>> name = Prompt.ask(\"Enter your name\")\n291-\n292-\n293-    \"\"\"\n294-\n295-    response_type = str\n296-\n297-\n298-class IntPrompt(PromptBase[int]):\n299-    \"\"\"A prompt that returns an integer.\n300-\n301-    Example:\n302-        >>> burrito_count = IntPrompt.ask(\"How many burritos do you want to order\")\n303-\n304-    \"\"\"\n305-\n306-    response_type = int\n307-    validate_error_message = \"[prompt.invalid]Please enter a valid integer number\"\n308-\n309-\n310-class FloatPrompt(PromptBase[int]):\n311-    \"\"\"A prompt that returns a float.\n312-\n", "stderr": "", "exit_code": 0}
5run_command
{
 "command": "sed -n '1,300p' rich/prompt.py | grep -n \"def process_response\" -A 20"
}
{"status": "ok", "stdout": "217: def process_response(self, value: str) -> PromptType:\n218- \"\"\"Process response from user, convert to prompt type.\n219-\n22
full
{"status": "ok", "stdout": "217:    def process_response(self, value: str) -> PromptType:\n218-        \"\"\"Process response from user, convert to prompt type.\n219-\n220-        Args:\n221-            value (str): String typed by user.\n222-\n223-        Raises:\n224-            InvalidResponse: If ``value`` is invalid.\n225-\n226-        Returns:\n227-            PromptType: The value to be returned from ask method.\n228-        \"\"\"\n229-        value = value.strip()\n230-        try:\n231-            return_value: PromptType = self.response_type(value)\n232-        except ValueError:\n233-            raise InvalidResponse(self.validate_error_message)\n234-\n235-        if self.choices is not None and not self.check_choice(value):\n236-            raise InvalidResponse(self.illegal_choice_message)\n237-\n", "stderr": "", "exit_code": 0}
6run_command
{
 "command": "sed -n '238,250p' rich/prompt.py"
}
{"status": "ok", "stdout": " return return_value\n\n def on_validate_error(self, value: str, error: InvalidResponse) -> None:\n \"\"\"Called to handle validatio
full
{"status": "ok", "stdout": "        return return_value\n\n    def on_validate_error(self, value: str, error: InvalidResponse) -> None:\n        \"\"\"Called to handle validation error.\n\n        Args:\n            value (str): String entered by user.\n            error (InvalidResponse): Exception instance the initiated the error.\n        \"\"\"\n        self.console.print(error)\n\n    def pre_prompt(self) -> None:\n        \"\"\"Hook to display something before the prompt.\"\"\"\n", "stderr": "", "exit_code": 0}
7run_command
{
 "command": "grep -n \"def check_choice\" rich/prompt.py"
}
{"status": "ok", "stdout": "205: def check_choice(self, value: str) -> bool:\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "205:    def check_choice(self, value: str) -> bool:\n", "stderr": "", "exit_code": 0}
8run_command
{
 "command": "sed -n '205,215p' rich/prompt.py"
}
{"status": "ok", "stdout": " def check_choice(self, value: str) -> bool:\n \"\"\"Check value is in the list of valid choices.\n\n Args:\n value (str): Value ent
full
{"status": "ok", "stdout": "    def check_choice(self, value: str) -> bool:\n        \"\"\"Check value is in the list of valid choices.\n\n        Args:\n            value (str): Value entered by user.\n\n        Returns:\n            bool: True if choice was valid, otherwise False.\n        \"\"\"\n        assert self.choices is not None\n        return value.strip() in self.choices\n", "stderr": "", "exit_code": 0}