`str()` of a style from `Style.update_link()` leaves out the new link once the original has been converted to a string
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
The bug is in rich/style.py: update_link() copies _style_definition into the new Style, and __str__ returns that cached value when set. Reproduce with the snippet in the issue, then check that the copy starts with no cached definition, as _add does. Done when str(style.update_link(...)) reflects the new link after the original has been stringified, and a test covers both setting and clearing the link.
Written by the indexing model from the issue text.
Description
Hi! I hit this while writing generated tests against Rich. Style.update_link() returns a style with a different link, but str() of it gives the original style's definition if the original was converted to a string first:
from rich.style import Style
style = Style(bold=True)
print("before str():", str(style.update_link("http://x")))
str(style) # caches the definition on the original
print("after str():", str(style.update_link("http://x")))
Output on Rich 15.0.0 and on main (3a109cc):
before str(): bold link http://x
after str(): bold
update_link copies self._style_definition into the new style, and __str__ returns that cached string when it's set, so the copy reports the original's definition. The same happens the other way round: update_link(None) on a linked style that has been converted to a string still shows link ....
I think the copy should start with no cached definition, as _add does. I have a one-line fix with a test and I'm happy to open a PR.
Python 3.13, macOS.
- Dominant language
- Python
- Stars
- 57.8k
- Forks
- 2.6k
- Avg merge
- 20m
- Merged PRs (30d)
- 3
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/rich
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Textualize/rich#4269 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4268 ·
-
Needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/rich#4267 ·
-
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Textualize/rich#4265 ·
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day