Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with link IDs and style caching #2949

Open
rodrigogiraoserrao opened this issue May 2, 2023 · 2 comments
Open

Issues with link IDs and style caching #2949

rodrigogiraoserrao opened this issue May 2, 2023 · 2 comments

Comments

@rodrigogiraoserrao
Copy link
Contributor

This is an account of many small related issues (not necessarily bugs, per se) uncovered while working on Textualize/textual#1587

  • Hashing doesn't take into account the link ID, so styles that look the same but have different link IDs will clash.

  • Style._link_id is created inconsistently: the attribute _link_id is created in a couple of different places and is created inconsistently across those locations.

Some examples

rich/rich/style.py

Lines 192 to 194 in 6d30ad0

self._link_id = (
f"{randint(0, 999999)}{hash(self._meta)}" if (link or meta) else ""
)

style._link_id = f"{randint(0, 999999)}{hash(style._meta)}"

style._link_id = f"{randint(0, 999999)}" if link else ""

  • Style addition is cached in Style._add and the dunder method __add__ only refreshes the link (by using .copy on the result) if the result has a .link, but it doesn't check for .meta.

  • The method Text.render adds another layer of caching that bypasses the link refresh because of hashing clashes (maybe using the style indices instead of the styles themselves as cache keys fixes this).

I couldn't make much progress in solving these issues because I don't quite understand the semantics of the attribute .link_id. Is it supposed to be a different ID for any Style instance that contains a link or some meta data?
Then, why not use the object ID itself?

@property
def link_id(self):
    return str(id(self)) if self.link or self.meta else ""
@github-actions
Copy link

github-actions bot commented May 2, 2023

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

The _link_id is a unique id used when generating terminal links. It's sole purpose is to let the terminal know what to highlight when you hover over it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants