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

Abierto
#4,263 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
Medio día
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
cli

Línea de trabajo

Start in rich/style.py with Style.add and the lru_cache-wrapped Style._add, and check how Style.eq and the _null flag interact with the cache. Run the reproduction script from the issue in both argument orders and compare the truthiness of Style() + Style() and Style() + not_null. Done when both orders give the same result, with the null shortcuts handled before the cached call and a regression test added.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
57.8k
Forks
2.6k
Merge medio
20 min
PR fusionados (30 d)
3

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Textualize/rich

Todos los issues de Textualize/rich

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.