Skip to content

fix: mo.status.progress_bar correctly handles range step#9612

Closed
z3r0-815 wants to merge 2 commits into
marimo-team:mainfrom
z3r0-815:fix/progress-bar-range-step
Closed

fix: mo.status.progress_bar correctly handles range step#9612
z3r0-815 wants to merge 2 commits into
marimo-team:mainfrom
z3r0-815:fix/progress-bar-range-step

Conversation

@z3r0-815
Copy link
Copy Markdown

Fixes #9575


mo.status.progress_bar(range(0, 10, 2)) shows impossible progress states. The bug is in how progress_bar calculates the increment per iteration when the range has a non-default step.

Root Cause

progress_bar.__init__ extracts self.step from range objects:

if isinstance(collection, range):
    self.step = cast(range, collection).step

Then __iter__ and __aiter__ use self.step as the increment:

self.progress.update(increment=self.step)

While len(range(0, 10, 2)) correctly computes total = 5, the increment of 2 per iteration over 5 iterations produces current = 10 — double the actual count. The progress bar shows impossible states like 8/5, 10/5.

Fix

  • Remove self.step field entirely
  • Always increment by 1 per iteration

A progress bar counts loop iterations, not range values. range.step determines what values are yielded, not how many times the loop body executes. Each yield = 1 unit of progress, regardless of the range step. This is consistent with how tqdm behaves — it counts iterations, not step increments.

Tests Added

Test Input Items yielded current after
test_progress_bar_range_with_step range(0, 10, 2) [0, 2, 4, 6, 8] (5) 5
test_progress_bar_range_no_step range(10) [0..9] (10) 10
test_progress_bar_range_custom_step range(5, 20, 3) [5, 8, 11, 14, 17] (5) 5
test_progress_bar_range_negative_step range(10, 0, -2) [10, 8, 6, 4, 2] (5) 5

All 17 tests pass (13 existing + 4 new).

Checklist

  • Fix is minimal — only the self.step logic removed, increment changed to 1
  • All range variants covered: positive step, default step, custom step, negative step
  • All existing tests continue to pass
  • ruff check and ruff format pass cleanly

progress_bar(range(0, 10, 2)) was incrementing by 2 per iteration
instead of by 1, causing current to exceed total. len(range)
already correctly computes total with step accounted for, so the
fix is to always increment by 1 per iteration — counting iterations,
not range values.

Fixes marimo-team#9575
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 19, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 19, 2026 8:50pm

Request Review

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Architecture diagram
sequenceDiagram
    participant UserCode as User Code
    participant PB as progress_bar()
    participant Progress as _Progress
    participant Output as Output System
    participant Collection as Collection (range/list/etc.)

    Note over UserCode,Collection: Runtime Execution Flow

    UserCode->>PB: Call progress_bar(range(0, 10, 2))
    PB->>PB: Detect collection type (range)
    PB->>PB: Compute total = len(range(0, 10, 2)) → 5
    PB->>PB: Set increment = 1 per iteration
    PB->>Progress: Create ProgressBar instance (total=5)
    Progress-->>UserCode: ProgressBar iterator

    loop For each iteration (5 total)
        UserCode->>Progress: Request next item
        Progress->>Collection: Get next value
        Collection-->>Progress: Yield item (e.g., 0, 2, 4, 6, 8)
        Progress->>Progress: Increment current by 1
        alt Progress enabled
            Progress->>Output: update(current, total)
            Output-->>Progress: Rendered
        end
        Progress-->>UserCode: Yield item
    end

    UserCode->>Progress: Signal end of iteration
    Progress->>Progress: Mark complete (current == total)
    Progress->>Output: Final progress display (5/5)
Loading

Re-trigger cubic

@z3r0-815
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@z3r0-815 z3r0-815 marked this pull request as ready for review May 20, 2026 03:49
@z3r0-815 z3r0-815 marked this pull request as draft May 20, 2026 03:49
@mscolnick
Copy link
Copy Markdown
Contributor

thank you. we already have a PR up for this #9582

@mscolnick mscolnick closed this May 21, 2026
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.

mo.status.progress_bar should use the step property of 'range'

2 participants