`Table(min_width=...)` raises `AssertionError: Sum of ratios must be > 0` on a very narrow console

Aperta Adatta ai principianti
#4,260 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

Il problema si trova in Table._calculate_column_widths in rich/table.py, dove ratio_distribute viene chiamata con larghezze di colonna che sommano 0 quando min_width supera lo spazio disponibile. Inizia riproducendo con lo snippet dell'issue alle larghezze 1 e 2, poi verifica se il padding dovrebbe essere saltato quando nessuna colonna ha larghezza. Il lavoro è completo quando la tabella viene renderizzata a quelle larghezze con min_width impostato e un test copre anche il caso del Panel stretto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi! I hit this while writing generated tests against Rich. Printing a table that has a min_width raises AssertionError when the console is so narrow that the columns get no width at all:

from rich.console import Console
from rich.table import Table

for width in (1, 2, 3, 10):
    console = Console(width=width, color_system=None)
    try:
        console.print(Table("a", min_width=5))
        print(f"width {width}: rendered")
    except Exception as error:
        print(f"width {width}: {type(error).__name__}: {error}")

Output on Rich 15.0.0 and on main (3a109cc), with the tables themselves left out:

width 1: AssertionError: Sum of ratios must be > 0
width 2: AssertionError: Sum of ratios must be > 0
width 3: rendered
width 10: rendered

Without min_width, the same table renders at widths 1 and 2 (cut down to its borders), and so does expand=True.

In Table._calculate_column_widths, every column width is 0 at these sizes, and the table is still narrower than min_width, so it pads the columns with ratio_distribute(_max_width - table_width, widths). The ratios are the widths, which sum to 0, and ratio_distribute asserts they don't.

It can also happen inside a narrow container: console.print(Panel(Table("a", min_width=5), width=5)) on a 40-column console raises the same error. I'd expect it to render like the table without min_width does. I have a small fix that skips the padding when no column has any width, and I'm happy to open a PR.

Python 3.13, macOS.

Lingua principale
Python
Stelle
57.8k
Fork
2.6k
Merge medio
20m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/rich

Tutte le issue di Textualize/rich

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.