Skip to content
Myr

Land the progress bar on its final position

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Closes #3571.

authored by Kevin Deldycke · committed by Kevin Deldycke · parent 8b44edf · Browse files · 3 files+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+43 added−0 removed

  • tests:+31 added−0 removed
  • other:+12 added−0 removed

3 files+43 added−0 removed

modified:src/click/_termui_impl.py+8 added
152 unmodified lines
153 # twice works and does "what you want".154 return next(iter(self))155156 def render_finish(self) -> None:157 # Steps stay pending until they add up to update_min_steps, so a length158 # that is not a multiple of it keeps a sub-threshold remainder that159 # never reaches pos. Apply it so the bar settles on its true position.160 if self._completed_intervals:161 self.make_step(self._completed_intervals)162 self._completed_intervals = 0163 self.render_progress()164165 if self.hidden or not self._is_atty:166 return167 self.file.write(AFTER_BAR)168 self.file.flush()
785 unmodified lines
modified:tests/test_termui.py+31 added
104 unmodified lines
105 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)106 assert runner.invoke(cli, []).output == ""107108109@pytest.mark.parametrize("update_min_steps", [1, 2, 3, 7, 20, 25])110@pytest.mark.parametrize("drive", ["iterate", "update"])111def test_progressbar_lands_on_final_position(monkeypatch, update_min_steps, drive):112 """The bar reaches its total whatever the ``update_min_steps`` threshold.113114 Regression for issue #3571: a threshold that does not divide the length115 left a remainder pending, so `show_pos` froze below completion, reporting116 `14/20` for a length of 20 and a threshold of 7. Thresholds of 1 and 2 do117 divide 20, and 25 exceeds it, so nothing is ever rendered until the end.118 """119 monkeypatch.setattr(click._termui_impl, "isatty", lambda _: True)120 stream = io.StringIO()121122 if drive == "iterate":123 with click.progressbar(124 range(20), show_pos=True, update_min_steps=update_min_steps, file=stream125 ) as bar:126 for _ in bar:127 pass128 else:129 with click.progressbar(130 length=20, show_pos=True, update_min_steps=update_min_steps, file=stream131 ) as bar:132 for _ in range(20):133 bar.update(1)134135 assert bar.pos == 20136 assert bar.finished137 assert "20/20" in stream.getvalue()138139140@pytest.mark.parametrize("avg, expected", [([], 0.0), ([1, 4], 2.5)])141def test_progressbar_time_per_iteration(runner, avg, expected):142 with _create_progress(2, avg=avg) as progress:143 assert progress.time_per_iteration == expected
1571 unmodified lines
modified:CHANGES.md+4 added
68 unmodified lines
69 file backend opened its file in binary mode, so writing a `str` to the pager70 raised `TypeError: a bytes-like object is required, not 'str'`, and the71 `color` argument was ignored on that path. Regression introduced in `8.4.0`72 by {pr}`1572`. {issue}`3731` {issue}`3732` {issue}`3740` {pr}`3739`73- {func}`progressbar` settles on its final position when `update_min_steps`74 does not divide the total. Steps below that threshold are applied when the75 bar finishes, so `show_pos` renders `20/20` rather than the last multiple76 it reached. {issue}`3571` {pr}`3769`7778## Version 8.4.27980Released 2026-06-24
1561 unmodified lines