{"record":{"id":"661886fdcc95abb8","repo":"Textualize/rich","slug":"style-must-not-be-set-when-appending-text-instance","errorCode":null,"errorMessage":"style must not be set when appending Text instance","messagePattern":"style must not be set when appending Text instance","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"rich/text.py","lineNumber":992,"sourceCode":"            Text: Returns self for chaining.\n        \"\"\"\n\n        if not isinstance(text, (str, Text)):\n            raise TypeError(\"Only str or Text can be appended to Text\")\n\n        if len(text):\n            if isinstance(text, str):\n                sanitized_text = strip_control_codes(text)\n                self._text.append(sanitized_text)\n                offset = len(self)\n                text_length = len(sanitized_text)\n                if style:\n                    self._spans.append(Span(offset, offset + text_length, style))\n                self._length += text_length\n            elif isinstance(text, Text):\n                _Span = Span\n                if style is not None:\n                    raise ValueError(\n                        \"style must not be set when appending Text instance\"\n                    )\n                text_length = self._length\n                if text.style:\n                    self._spans.append(\n                        _Span(text_length, text_length + len(text), text.style)\n                    )\n                self._text.append(text.plain)\n                self._spans.extend(\n                    _Span(start + text_length, end + text_length, style)\n                    for start, end, style in text._spans.copy()\n                )\n                self._length += len(text)\n        return self\n\n    def append_text(self, text: \"Text\") -> \"Text\":\n        \"\"\"Append another Text instance. This method is more performant than Text.append, but\n        only works for Text.","sourceCodeStart":974,"sourceCodeEnd":1010,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/text.py#L974-L1010","documentation":"Text.append() accepts a style= keyword only for str input. When appending a Text instance, all styling must come from the appended Text's own spans and its .style — copying a caller-supplied style on top is ambiguous (it would need to wrap the copied spans), so rich raises ValueError('style must not be set when appending Text instance').","triggerScenarios":"text.append(other_text, style='bold'); also any default-argument bug where style defaults to '' or a Style object instead of None — note the check is 'style is not None', so even style='' (empty string) raises.","commonSituations":"Generic append(value, style) helpers that pass style through unconditionally; a refactor that changed a str to a Text mid-pipeline while style= stayed; a default style parameter of '' rather than None that only fails on the Text branch.","solutions":["Drop style= when appending Text: text.append(other_text).","Pre-style the appended Text: text.append(Text('x', style='bold')) or other_text.stylize('bold') before appending.","In generic helpers, branch: append(text, style=None if isinstance(text, Text) else style) — or normalize your helper's default style to None, not ''."],"exampleFix":"# before\ntext.append(label, style='')  # ValueError when label is a Text\n\n# after\ntext.append(label)  # Text carries its own style\n# or: text.append(Text(str(label), style='bold')) for str values","handlingStrategy":"validation","validationCode":"from rich.text import Text\ntext.append(t if isinstance(t, Text) else str(t), style=None if isinstance(t, Text) else style)","typeGuard":"from rich.text import Text\n\ndef append_call_args(value: object, style):\n    \"\"\"Return (value, style) safe for Text.append.\"\"\"\n    if isinstance(value, Text):\n        return value, None\n    return str(value), style","tryCatchPattern":null,"preventionTips":["Never pass style= when the argument is a Text instance; style only applies to str.","Note the check is `style is not None` — an empty-string default ('') still raises; use None as the default.","Pre-style appended Text via its constructor or stylize() instead."],"tags":["rich","text","append","style","valueerror"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}