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

'DataTree' object does not support the context manager protocol #9607

Closed
kmuehlbauer opened this issue Oct 11, 2024 · 4 comments
Closed

'DataTree' object does not support the context manager protocol #9607

kmuehlbauer opened this issue Oct 11, 2024 · 4 comments
Labels
contrib-good-first-issue enhancement topic-DataTree Related to the implementation of a DataTree class

Comments

@kmuehlbauer
Copy link
Contributor

What is your issue?

ds = xr.Dataset(
    {"foo": (("x", "y"), np.random.rand(4, 5))},
    coords={
        "x": [10, 20, 30, 40],
        "y": pd.date_range("2000-01-01", periods=5),
        "z": ("x", list("abcd")),
    },
)
ds.to_netcdf("saved_on_disk.nc")

# works as intended
with xr.open_dataset("saved_on_disk.nc") as ds:
    print(ds)

# doesn't work
with xr.open_datatree("saved_on_disk.nc") as dtree:
    print(dtree)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 with xr.open_datatree("saved_on_disk.nc") as dtree:
      2     print(dtree)

TypeError: 'DataTree' object does not support the context manager protocol
@kmuehlbauer kmuehlbauer added the needs triage Issue that has not been reviewed by xarray team member label Oct 11, 2024
@kmuehlbauer
Copy link
Contributor Author

@TomNicholas I've first found that, after I've moved the whole tests of xradar to use contextmanager 😬 Is it just missing?

@headtr1ck
Copy link
Collaborator

That is defined in DataWithCoords: https://github.com/pydata/xarray/blob/main/xarray%2Fcore%2Fcommon.py#L1467-L1471

Should be easy to add this to DataTree as well.

@headtr1ck headtr1ck added enhancement contrib-good-first-issue topic-DataTree Related to the implementation of a DataTree class and removed needs triage Issue that has not been reviewed by xarray team member labels Oct 11, 2024
@TomNicholas
Copy link
Member

Thanks for raising this - I agree this should be addressed. It's actually a duplicate of #9337

@kmuehlbauer
Copy link
Contributor Author

Great, closing as dupe of #9337.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contrib-good-first-issue enhancement topic-DataTree Related to the implementation of a DataTree class
Projects
None yet
Development

No branches or pull requests

3 participants