diff --git a/src/textual/content.py b/src/textual/content.py index 8c65440c06..b860bca845 100644 --- a/src/textual/content.py +++ b/src/textual/content.py @@ -760,10 +760,10 @@ def __add__(self, other: Content | str) -> Content: return content return NotImplemented - def __radd__(self, other: Content | str) -> Content: - if not isinstance(other, (Content, str)): + def __radd__(self, other: str) -> Content: + if not isinstance(other, str): return NotImplemented - return self + other + return Content(other) + self @classmethod def _trim_spans(cls, text: str, spans: list[Span]) -> list[Span]: diff --git a/tests/test_content.py b/tests/test_content.py index 5020db8f90..61e86d0f68 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -136,6 +136,14 @@ def test_add() -> None: assert content.spans == [Span(0, 3, "red"), Span(4, 7, "blue")] assert content.cell_length == 7 +def test_radd() -> None: + """Test reverse addition.""" + assert "foo" + Content("bar") == Content("foobar") + + # Test spans after addition + content = "foo " + Content.styled("bar", "blue") + assert str(content) == "foo bar" + assert content.spans == [Span(4, 7, "blue")] def test_from_markup(): """Test simple parsing of content markup."""