Skip to content

Use context managers in test functions#469

Merged
TomNicholas merged 1 commit intozarr-developers:mainfrom
chuckwondo:context-managers-in-tests
Mar 5, 2025
Merged

Use context managers in test functions#469
TomNicholas merged 1 commit intozarr-developers:mainfrom
chuckwondo:context-managers-in-tests

Conversation

@chuckwondo
Copy link
Collaborator

@chuckwondo chuckwondo commented Mar 3, 2025

Eliminate 32 of 70 of the following warnings emitted while running unit tests: RuntimeWarning: deallocating CachingFileManager

The remaining 38 appear to all be related to the HDF5VirtualBackend, so they are not addressed in this PR, as they will require some code refactoring, as identified in #468.

Fixes #390

Elimiinate 32 of 70 of the following warnings emitted while running unit
tests: `RuntimeWarning: deallocating CachingFileManager`

The remaining 38 appear to all be related to the HDF5VirtualBackend (see
zarr-developers#468)

Fixes zarr-developers#390
@TomNicholas
Copy link
Member

This looks great @chuckwondo , but I'm confused as to how it actually prevents the warnings. We do not yet have any implementation of the __exit__ method that xarray's backends use, so how is this working? Is it effectively piggybacking off of xarray's __exit__ method?

https://github.com/pydata/xarray/blob/0184702f16c3f744fc9096c7dac690626dcc6922/xarray/backends/common.py#L323

@chuckwondo
Copy link
Collaborator Author

This looks great @chuckwondo , but I'm confused as to how it actually prevents the warnings. We do not yet have any implementation of the __exit__ method that xarray's backends use, so how is this working? Is it effectively piggybacking off of xarray's __exit__ method?

https://github.com/pydata/xarray/blob/0184702f16c3f744fc9096c7dac690626dcc6922/xarray/backends/common.py#L323

Right, and that's why my changes do not eliminate all of the warnings. Some of them are eliminated because they introduce context managers where xarray datasets (and other things that deal directly with files) are directly used. The rest remain precisely because of what you mention (hence, why I also opened #468), but I added the context managers in these places anyway, for consistency as well as for automatically taking advantage of the necessary changes. Once the other related issues are addressed, the remaining warning should automatically go away as well.

If you like, I can attempt to pare down the changes to only the places that eliminate warnings now, rather than including context managers that will only eliminate warnings later, when the other related changes land.

@TomNicholas
Copy link
Member

Thank you @chuckwondo , that makes sense. I just wanted to check I wasn't going to merge this without understanding it :)

@TomNicholas TomNicholas merged commit fd3a916 into zarr-developers:main Mar 5, 2025
10 checks passed
@chuckwondo chuckwondo deleted the context-managers-in-tests branch April 8, 2025 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RuntimeWarning: deallocating CachingFileManager during main test execution

2 participants