Skip to content

Fix iterator handling in ProgressBar (issue 5540) - #5546

Open
Srikar025 wants to merge 11 commits into
MDAnalysis:developfrom
Srikar025:fix-5540
Open

Srikar025 wants to merge 11 commits into
MDAnalysis:developfrom
Srikar025:fix-5540

Conversation

@Srikar025

@Srikar025 Srikar025 commented Nov 11, 2026 •

Copy link
Copy Markdown

Fixes #5540

Changes made in this Pull Request

  • ProgressBar.__iter__() now uses a regular for loop instead of
    yield from when the progress display is disabled. yield from forwards
    generator closure to the wrapped iterator, which closed the
    TrajectoryReader when iteration was interrupted (tqdm >= 4.70).
  • Behavior with the progress display enabled is unchanged.
  • Added a regression test checking that the trajectory can still be read
    after a disabled ProgressBar loop is exited early.
  • Added a package/CHANGELOG entry under Fixes and added myself to
    package/AUTHORS.

Testing

  • python -m pytest testsuite/MDAnalysisTests/lib/test_log.py -q: 4 passed
  • python -m pytest testsuite/MDAnalysisTests/lib -q: 9950 passed, 3 skipped

LLM / AI generated code disclosure

I used AI to help investigate the bug and understand its root cause, and to
help draft this PR description.

PR Checklist

  • Issue raised/referenced?
  • Tests updated/added?
  • Documentation updated/added? (not needed, internal bug fix)
  • package/CHANGELOG file updated?
  • Is your name in package/AUTHORS? (If it is not, add it!)
  • I have read and understand the current AI Policy
  • LLM/AI disclosure was updated.

Developers Certificate of Origin

I certify that I can submit this code contribution as described in the Developer Certificate of Origin, under the MDAnalysis LICENSE.

@read-the-docs-community

read-the-docs-community Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.93%. Comparing base (a72b0c2) to head (236e203).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5546   +/-   ##
========================================
  Coverage    93.93%   93.93%           
========================================
  Files          184      184           
  Lines        22680    22686    +6     
  Branches      3228     3230    +2     
========================================
+ Hits         21304    21311    +7     
+ Misses         916      915    -1     
  Partials       460      460           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Srikar025

Copy link
Copy Markdown
Author

please have a look at this ive completed the fix and added a regression test covering early iterator closure when ProgressBar is disabled.
The test also simulates the yield from behaviour independently of the installed tqdm version.
All available CI checks have passed, except the macOS ARM64 job, which was cancelled due to an unsupported runner configuration.

@orbeckst

Copy link
Copy Markdown
Member

I'll let @hejamu have a look and comment. Please ping me once this is done and I'll use the review report.

@orbeckst orbeckst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll wait for @hejamu 's review.

Comment thread package/AUTHORS Resolved
@hejamu

hejamu commented Nov 19, 2026

Copy link
Copy Markdown

@Srikar025 thanks for looking into this! The fix looks fine to me, this basically reintroduces the old behaviour since tqdm 4.70 changed its disabled path from a plain for loop to yield from iterable.

One thing I would suggest is to think about the bigger picture here. Maybe ProtoReader.__iter__ should not return the reader itself, then we would not need to protect it from a close. But one can open a follow up issue on that. @orbeckst if you agree, I can do a write up of that and open an issue.

actual = err.strip().split("\r")[-1]
assert actual == expected

def test_trajectory_remains_open_when_disabled(self, monkeypatch):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This test is fine but it is a bit synthetic. In MAICoS we also tested an Analysis workflow that breaks. https://github.com/atomistic-lab/maicos/pull/614/changes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, something like your test_aborted_run_keeps_trajectory_open would be good.

Comment on lines +63 to +64
tqdm_class = ProgressBar.__mro__[1]
monkeypatch.setattr(tqdm_class, "__iter__", iter_with_yield_from)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does this work?

Suggested change
tqdm_class = ProgressBar.__mro__[1]
monkeypatch.setattr(tqdm_class, "__iter__", iter_with_yield_from)
import tqdm.std
monkeypatch.setattr(tqdm.std.tqdm, "__iter__", iter_with_yield_from)

@orbeckst

Copy link
Copy Markdown
Member

One thing I would suggest is to think about the bigger picture here. Maybe ProtoReader.__iter__ should not return the reader itself, then we would not need to protect it from a close. But one can open a follow up issue on that.

Separate issue would be good for discussion!

def test_trajectory_remains_open_when_disabled(self, monkeypatch):
"""Ensure early iterator closure leaves the trajectory accessible."""
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is pretty common for LLMs to scope imports in (test) functions when that isn't necessary. Do we really need to scope these imports locally like this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just made a similar comment — we collect imports at the top unless there's a very good reason to do something else.

@orbeckst orbeckst left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please see @hejamu 's comments and my own.

When you have made changes, please show a screen shot that demonstrates that the old code triggers your tests to fail while your changes make them pass.

super(ProgressBar, self).__init__(*args, **kwargs)

def __iter__(self):
"""Iterate without closing the underlying iterable when disabled."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

add a comment referencing issue and PR and state briefly that we're working around a change in tqdm (and mention the version of the change). When looking at this code, readers should understand why it's necessary.

Comment on lines +57 to +58
import MDAnalysis as mda
from MDAnalysis.tests.datafiles import PSF, DCD

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

imports at top

tqdm_class = ProgressBar.__mro__[1]
monkeypatch.setattr(tqdm_class, "__iter__", iter_with_yield_from)

u = mda.Universe(PSF, DCD)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Make the universe a fixture so that you can re-use it for a test_aborted_run_keeps_trajectory_open, too.

actual = err.strip().split("\r")[-1]
assert actual == expected

def test_trajectory_remains_open_when_disabled(self, monkeypatch):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, something like your test_aborted_run_keeps_trajectory_open would be good.

@Srikar025

Copy link
Copy Markdown
Author

Thanks, @hejamu and @orbeckst, for the helpful reviews and suggestions. I'll work on them and improve.
I'll leave the ProtoReader.iter improvement for a separate follow-up issue as suggested.

@orbeckst orbeckst self-assigned this Nov 25, 2026
@orbeckst orbeckst changed the title Fix 5540 Fix iterator handling in ProgressBar (issue 5540) Nov 25, 2026

This branch has not been deployed

No deployments
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.

tqdm>= 4.70 changes how iterators are handled

4 participants