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

Open Beginner friendly
#4,260 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli

Research direction

The failure is in Table._calculate_column_widths in rich/table.py, where ratio_distribute is called with column widths that all sum to 0 when min_width exceeds the available space. Start by reproducing with the snippet in the issue at widths 1 and 2, then check whether padding should be skipped when no column has width. Done means the table renders at those widths with min_width set, and a test covers the narrow Panel case as well.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
57.8k
Forks
2.6k
Avg merge
20m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/rich

All issues in Textualize/rich

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.