Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use context manager for xarray dataset file opening #953

Merged
merged 7 commits into from
Oct 4, 2024

Conversation

spjuhel
Copy link
Collaborator

@spjuhel spjuhel commented Sep 26, 2024

Changes proposed in this PR:
Replace all occurrence of data = xr.open_dataset(filename) with with xr.open_dataset(filename) as data: to ensure filestream closing. See #920 on why this can cause problems.

Using data = xr.open_dataset(filename) assumes a data.close() afterwards to correctly close the filestream. This is not done in all function. The pythonic approach is to use a context manager with ... as ... : which handle the closing.

See also: pydata/xarray#2887

This PR fixes #920

PR Author Checklist

PR Reviewer Checklist

@spjuhel
Copy link
Collaborator Author

spjuhel commented Sep 26, 2024

Should we also mention this in the programming guidelines?

@spjuhel spjuhel self-assigned this Sep 26, 2024
@chahank
Copy link
Member

chahank commented Sep 26, 2024

Yes!

Copy link
Collaborator

@emanuel-schmid emanuel-schmid left a comment

Choose a reason for hiding this comment

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

Very valuable contribution. 🙌

I'd just change the changelog

  1. this is a fix not a change
  2. the log should tell what has been done, but not how. If somebody is interested they can click on the PR link.

CHANGELOG.md Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
spjuhel and others added 4 commits September 27, 2024 16:25
Co-authored-by: Emanuel Schmid <51439563+emanuel-schmid@users.noreply.github.com>
Co-authored-by: Emanuel Schmid <51439563+emanuel-schmid@users.noreply.github.com>
@emanuel-schmid emanuel-schmid merged commit 4ae9abd into develop Oct 4, 2024
18 checks passed
@emanuel-schmid emanuel-schmid deleted the feature/xr_dataset_context_manager branch October 4, 2024 08:43
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.

3 participants