`Style() + style` can return a null or a non-null style depending on what was added earlier
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- Nửa ngày
- Mức phù hợp với người mới
- 68/100
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Python
- Star
- 57.8k
- Fork
- 2.6k
- Merge trung bình
- 20 phút
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của Textualize/rich
-
Color.parse() raises raw ValueError instead of ColorParseError for an empty rgb(...) componentĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Textualize/rich#4269 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Textualize/rich#4268 ·
-
Needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Textualize/rich#4267 ·
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
Textualize/rich#4266 ·
-
[BUG] `Text.right_crop(0)` empties the text and an amount larger than the text makes `len()` raiseĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Textualize/rich#4265 ·
Tất cả issue của Textualize/rich
Issue tương tự
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
sandbox-quantum/switch#2163 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug pydanty:is-working
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
pydantic/pydantic-ai#13939 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
priority: low python
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
LearningCircuit/local-deep-research#8849 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 86/100
francescopace/espectre#223 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
good first issue Status: Triaged
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 84/100
canonical/imagecraft#538 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày