Skip to content

Make IO and RawIO classes usable as context managers - #1975

Open
ptlnextdoor wants to merge 1 commit into
NeuralEnsemble:masterfrom
ptlnextdoor:fix/oss-neo-175
Open

ptlnextdoor wants to merge 1 commit into
NeuralEnsemble:masterfrom
ptlnextdoor:fix/oss-neo-175

Conversation

@ptlnextdoor

Copy link
Copy Markdown

Fixes #175

What was broken

IO classes could not be used in a with statement. Only NixIO, NixIOFr and EDFRawIO had __enter__ and __exit__.

What changed

BaseIO and BaseRawIO now have __enter__, __exit__ and a default close() that does nothing. __exit__ calls close(). IO classes that hold file handles already override close() (or can do so), so with SomeIO(...) as io: now closes them on exit. Files are still opened in the constructor. No per-IO changes. Classes with their own __enter__/__exit__ keep them.

How it was tested

  • New neo/test/iotest/test_context_manager.py. It fails on master (does not support the context manager protocol) and passes now. It checks that close() runs on normal exit and when an exception is raised.
  • ruff and black -l 120 are clean on the changed files.
  • I ran only the new test file in full. test_exampleio.py and test_examplerawio.py could not run here, because the ephy_testing_data files are missing. I did not run the full suite.

AI disclosure: this PR was prepared with AI assistance (Claude, Anthropic).

Add __enter__, __exit__ and a default no-op close() to BaseIO and BaseRawIO,
so that 'with SomeIO(...) as io:' calls close() on exit.

Fixes NeuralEnsemble#175
Copilot AI balanced review requested due to automatic review settings November 17, 2026 05:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

IO classes as context managers

2 participants