`str()` of a style from `Style.update_link()` leaves out the new link once the original has been converted to a string

Open Beginner friendly
#4,262 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/rich

All issues in Textualize/rich

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.