← eval_submission_analyzer

rich_3471

failed WRONG_FIX wrong_fix · 7 tool calls · 89 s · Textualize/rich

Task input

Fixed exception in append_tokens

Fixes https://github.com/Textualize/rich/issues/3014

Tool calls (8)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"def append_tokens\" ."
}
{"status": "ok", "stdout": "./rich/text.py:1028: def append_tokens(\n", "exit_code": 0}
full
{"status": "ok", "stdout": "./rich/text.py:1028:    def append_tokens(\n", "exit_code": 0}
2read_file
{
 "end_line": 1050,
 "filepath": "rich/text.py",
 "start_line": 1028
}
{"status": "ok", "filepath": "rich/text.py", "content": " def append_tokens(\n self, tokens: Iterable[Tuple[str, Optional[StyleType]]]\n ) -> \"Text\":\n \"\"\"
full
{"status": "ok", "filepath": "rich/text.py", "content": "    def append_tokens(\n        self, tokens: Iterable[Tuple[str, Optional[StyleType]]]\n    ) -> \"Text\":\n        \"\"\"Append iterable of str and style. Style may be a Style instance or a str style definition.\n\n        Args:\n            tokens (Iterable[Tuple[str, Optional[StyleType]]]): An iterable of tuples containing str content and style.\n\n        Returns:\n            Text: Returns self for chaining.\n        \"\"\"\n        append_text = self._text.append\n        append_span = self._spans.append\n        _Span = Span\n        offset = len(self)\n        for content, style in tokens:\n            append_text(content)\n            if style:\n                append_span(_Span(offset, offset + len(content), style))\n            offset += len(content)\n        self._length = offset\n        return self\n\n", "start_line": 1028, "end_line": 1050, "total_lines": 1357, "is_truncated": false}
3run_command
{
 "command": "grep -rn \"issue 3014\" ."
}
{"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": "", "exit_code": 1}}
full
{"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": "", "exit_code": 1}}
4run_command
{
 "command": "python3 -c \"from rich.text import Text; t = Text(); t.append_tokens([ (123, None) ]); print('Success')\""
}
{"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": "Traceback (most recen
full
{"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n  File \"<string>\", line 1, in <module>\n    from rich.text import Text; t = Text(); t.append_tokens([ (123, None) ]); print('Success')\n                                            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^\n  File \"/workspace/rich/text.py\", line 1047, in append_tokens\n    offset += len(content)\n              ~~~^^^^^^^^^\nTypeError: object of type 'int' has no len()\n", "exit_code": 1}}
5read_file
{
 "filepath": "rich/text.py",
 "start_line": 1028,
 "end_line": 1050
}
{"status": "ok", "filepath": "rich/text.py", "content": " def append_tokens(\n self, tokens: Iterable[Tuple[str, Optional[StyleType]]]\n ) -> \"Text\":\n \"\"\"
full
{"status": "ok", "filepath": "rich/text.py", "content": "    def append_tokens(\n        self, tokens: Iterable[Tuple[str, Optional[StyleType]]]\n    ) -> \"Text\":\n        \"\"\"Append iterable of str and style. Style may be a Style instance or a str style definition.\n\n        Args:\n            tokens (Iterable[Tuple[str, Optional[StyleType]]]): An iterable of tuples containing str content and style.\n\n        Returns:\n            Text: Returns self for chaining.\n        \"\"\"\n        append_text = self._text.append\n        append_span = self._spans.append\n        _Span = Span\n        offset = len(self)\n        for content, style in tokens:\n            append_text(content)\n            if style:\n                append_span(_Span(offset, offset + len(content), style))\n            offset += len(content)\n        self._length = offset\n        return self\n\n", "start_line": 1028, "end_line": 1050, "total_lines": 1357, "is_truncated": false}
6edit_file
{
 "filepath": "rich/text.py",
 "new_string": "        for content, style in tokens:\n            content = str(content)\n            append_text(content)\n            if style:\n                append_span(_Span(offset, offset + len(content), style))\n            offset += len(content)",
 "old_string": "        for content, style in tokens:\n            append_text(content)\n            if style:\n                append_span(_Span(offset, offset + len(content), style))\n            offset += len(content)"
}
{"status": "ok", "filepath": "rich/text.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/rich/text.py\n+++ b/rich/text.py\n@@ -1041,6 +1041,7 @@\n _Sp
full
{"status": "ok", "filepath": "rich/text.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/rich/text.py\n+++ b/rich/text.py\n@@ -1041,6 +1041,7 @@\n         _Span = Span\n         offset = len(self)\n         for content, style in tokens:\n+            content = str(content)\n             append_text(content)\n             if style:\n                 append_span(_Span(offset, offset + len(content), style))\n", "is_truncated": false}
7run_command
{
 "command": "python3 -c \"from rich.text import Text; t = Text(); t.append_tokens([ (123, None) ]); print('Success')\""
}
{"status": "ok", "stdout": "Success\n", "exit_code": 0}
full
{"status": "ok", "stdout": "Success\n", "exit_code": 0}
8submit_patch
[]
{"status": "ok", "patch_size": 415, "files_changed": 1}
full
{"status": "ok", "patch_size": 415, "files_changed": 1}

Patch

diff --git a/rich/text.py b/rich/text.py
index 7b32967f..0f26ed62 100644
--- a/rich/text.py
+++ b/rich/text.py
@@ -1041,6 +1041,7 @@ class Text(JupyterMixin):
         _Span = Span
         offset = len(self)
         for content, style in tokens:
+            content = str(content)
             append_text(content)
             if style:
                 append_span(_Span(offset, offset + len(content), style))

Test output

show
........................................................................ [ 66%]
...................................F
=================================== FAILURES ===================================
______________________________ test_append_tokens ______________________________

    def test_append_tokens() -> None:
        """Regression test for https://github.com/Textualize/rich/issues/3014"""
    
        console = Console()
        t = Text().append_tokens(
            [
                (
                    "long text that will be wrapped with a control code \r\n",
                    "red",
                ),
            ]
        )
        with console.capture() as capture:
>           console.print(t, width=40)

tests/test_text.py:999: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
rich/console.py:1710: in print
    extend(render(renderable, render_options))
rich/console.py:1331: in render
    for render_output in iter_render:
rich/text.py:694: in __rich_console__
    lines = self.wrap(
rich/text.py:1235: in wrap
    new_lines = line.divide(offsets)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <text 'long text that will be wrapped with a control code ' [Span(0, 52, 'red')]>
offsets = [38]

    def divide(self, offsets: Iterable[int]) -> Lines:
        """Divide text in to a number of lines at given offsets.
    
        Args:
            offsets (Iterable[int]): Offsets used to divide text.
    
        Returns:
            Lines: New RichText instances between offsets.
        """
        _offsets = list(offsets)
    
        if not _offsets:
            return Lines([self.copy()])
    
        text = self.plain
        text_length = len(text)
        divide_offsets = [0, *_offsets, text_length]
        line_ranges = list(zip(divide_offsets, divide_offsets[1:]))
    
        style = self.style
        justify = self.justify
        overflow = self.overflow
        _Text = Text
        new_lines = Lines(
            _Text(
                text[start:end],
                style=style,
                justify=justify,
                overflow=overflow,
            )
            for start, end in line_ranges
        )
        if not self._spans:
            return new_lines
    
        _line_appends = [line._spans.append for line in new_lines._lines]
        line_count = len(line_ranges)
        _Span = Span
    
        for span_start, span_end, style in self._spans:
            lower_bound = 0
            upper_bound = line_count
            start_line_no = (lower_bound + upper_bound) // 2
    
            while True:
                line_start, line_end = line_ranges[start_line_no]
                if span_start < line_start:
                    upper_bound = start_line_no - 1
                elif span_start > line_end:
                    lower_bound = start_line_no + 1
                else:
                    break
                start_line_no = (lower_bound + upper_bound) // 2
    
            if span_end < line_end:
                end_line_no = start_line_no
            else:
                end_line_no = lower_bound = start_line_no
                upper_bound = line_count
    
                while True:
>                   line_start, line_end = line_ranges[end_line_no]
E                   IndexError: list index out of range

rich/text.py:1165: IndexError
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 107 passed in 0.37s