Skip to content

Commit f1c6bec

Browse files
authored
Merge pull request #6160 from gvx/main
Fix str() + Content()
2 parents bce43d5 + ceac084 commit f1c6bec

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/textual/content.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,10 @@ def __add__(self, other: Content | str) -> Content:
760760
return content
761761
return NotImplemented
762762

763-
def __radd__(self, other: Content | str) -> Content:
764-
if not isinstance(other, (Content, str)):
763+
def __radd__(self, other: str) -> Content:
764+
if not isinstance(other, str):
765765
return NotImplemented
766-
return self + other
766+
return Content(other) + self
767767

768768
@classmethod
769769
def _trim_spans(cls, text: str, spans: list[Span]) -> list[Span]:

tests/test_content.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ def test_add() -> None:
136136
assert content.spans == [Span(0, 3, "red"), Span(4, 7, "blue")]
137137
assert content.cell_length == 7
138138

139+
def test_radd() -> None:
140+
"""Test reverse addition."""
141+
assert "foo" + Content("bar") == Content("foobar")
142+
143+
# Test spans after addition
144+
content = "foo " + Content.styled("bar", "blue")
145+
assert str(content) == "foo bar"
146+
assert content.spans == [Span(4, 7, "blue")]
139147

140148
def test_from_markup():
141149
"""Test simple parsing of content markup."""

0 commit comments

Comments
 (0)