Skip to content

Fix histogram formatting for zero counts - #529

Closed
saime428 wants to merge 1 commit into
mahmoud:masterfrom
saime428:codex/fix-zero-count-histogram
Closed

saime428 wants to merge 1 commit into
mahmoud:masterfrom
saime428:codex/fix-zero-count-histogram

Conversation

@saime428

@saime428 saime428 commented Oct 3, 2026

Copy link
Copy Markdown

The change

Stats([]).format_histogram() raises ZeroDivisionError, even though histogram counting returns a valid zero-count bin. Passing all-zero counts directly to format_histogram_counts() also fails.

Set the bar scale to zero when all counts are zero, retaining the existing | marker. For example, Stats([]).format_histogram(width=20) now returns 0.0: 0 |.

The tests cover empty datasets with default and explicit bin counts, single and multiple zero-count bins, and a mixed-count control. Normal histogram output is unchanged.

Validation on Windows with Python 3.12.13:

  • Before the fix, the new tests produced 5 division-by-zero failures and 1 passing control.
  • The full test suite, including doctests, passed: 684 tests and 12 subtests.
  • The source distribution and wheel built successfully with uv build.
  • After installing the wheel, the full suite passed against the installed package from outside the checkout: 684 tests and 12 subtests.
  • git diff --check passed.
  • A separate Codex agent reviewed the diff and found no actionable issues. It did not rerun tests.

Full-suite command:

python -B -m pytest -q -p no:cacheprovider --doctest-modules boltons tests

The GitHub Actions matrix has not run. Other Python versions and operating systems have not been tested.

The backstory

I asked Codex to help find a small bug fix in a Python library. It found this while checking Boltons' histogram code and tests. The count calculation already handles empty data, but the formatter crashes when it scales the bars. The fix is one line, with regression tests for both public formatting entry points.

AI assistance

  • Harness/tooling: Codex Desktop.
  • Model(s): gpt-6-astra, as reported by the Codex session.
  • Original prompts:
    • help me review the work
    • find some PR chances
  • Scope: Codex found and reproduced the bu. A separate Codex agent reviewed the diff without rerunning tests.

Avoid dividing by zero when the maximum histogram count is zero while
retaining the existing marker for zero-count bins.

Cover empty datasets with default and explicit bin counts, all-zero
histograms, and mixed zero/nonzero counts.
mahmoud added a commit that referenced this pull request Oct 25, 2026
@mahmoud

mahmoud commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Folded into #609 (see the PR description for credit). Thanks for the report and the repro.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants