Skip to content

Style() + style can return a null or a non-null style depending on what was added earlier #4263

Open

Description

@visionik

Hi! I hit this while writing generated tests against Rich. Adding two styles can give a different result depending on which other sums happened earlier in the process. It needs a style that compares equal to Style() but isn't null, which several methods return; for example update_link(None) on a style that only had a link (this is what Text.from_ansi does when an OSC 8 link ends):

import sys
from rich.style import Style

not_null = Style(link="http://x").update_link(None)
if sys.argv[1] == "null-first":
    print("Style() + Style():", bool(Style() + Style()))
    print("Style() + not_null:", bool(Style() + not_null))
else:
    print("Style() + not_null:", bool(Style() + not_null))
    print("Style() + Style():", bool(Style() + Style()))

On Rich 15.0.0 and on main (3a109cc):

$ python add.py null-first
Style() + Style(): False
Style() + not_null: False
$ python add.py other
Style() + not_null: True
Style() + Style(): True

So Style() + Style() is truthy if Style() + not_null was computed first, and Style() + not_null is falsy the other way round.

Style._add is wrapped in lru_cache, which finds entries by equality, and Style.__eq__ compares the hash of colors, attributes, link and meta, which leaves out _null. The null shortcuts at the top of _add (return self / return style) are inside the cached function, so whichever of two equal operands was seen first is returned for both.

I haven't found a difference in what Rich prints, since a non-null style with nothing set renders no codes. It does matter to code that tests a style's truthiness (if style:) to decide whether to apply or skip it, where the result then depends on what was rendered before.

A fix is to do the null shortcuts in __add__, before the cached call, so the cache only sees real combinations. I'm happy to open a PR.

Python 3.13, macOS.

Activity

  1. eliem08 commented on Nov 27, 2026

    @eliem08

    Hi @willmcgugan, I would like to fix this one if the approach sounds right.

    The problem is that the null shortcuts live inside the lru_cached Style._add, while __eq__/__hash__ ignore _null. A null style and an equal non-null style (like the one update_link(None) returns) therefore share a cache entry, and whichever pair was added first decides the result for both.

    My plan: move the null shortcuts into __add__, before the cached call, so the cache only sees real combinations. The combined style is then handled exactly as today, including the copy when it carries a link. No change to __eq__, __hash__, or _add itself.

    I would also add a regression test that clears the _add cache and checks both orders, plus a CHANGELOG entry.

    Happy to open the PR once you confirm this is the direction you want.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions