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

Allow weakref #3318

Merged
merged 4 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ What's New
v0.13.1 (unreleased)
--------------------

Bug fixes
~~~~~~~~~
- Reintroduce support for :mod:`weakref` (broken in v0.13.0). Support has been
reinstated for :class:`DataArray` and :class:`Dataset` objects only. Internal xarray
objects remain unaddressable by weakref in order to save RAM.
crusaderky marked this conversation as resolved.
Show resolved Hide resolved
(:issue:`3317`) by `Guido Imperiale <https://github.com/crusaderky>`_.


.. _whats-new.0.13.0:

v0.13.0 (17 Sep 2019)
Expand Down
10 changes: 9 additions & 1 deletion xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ class DataArray(AbstractArray, DataWithCoords):
Dictionary for holding arbitrary metadata.
"""

__slots__ = ("_accessors", "_coords", "_file_obj", "_name", "_indexes", "_variable")
__slots__ = (
"_accessors",
"_coords",
"_file_obj",
"_name",
"_indexes",
"_variable",
"__weakref__",
)

_groupby_cls = groupby.DataArrayGroupBy
_rolling_cls = rolling.DataArrayRolling
Expand Down
1 change: 1 addition & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords):
"_file_obj",
"_indexes",
"_variables",
"__weakref__",
)

_groupby_cls = groupby.DatasetGroupBy
Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4672,3 +4672,14 @@ class MyArray(DataArray):
pass

assert str(e.value) == "MyArray must explicitly define __slots__"


def test_weakref():
"""Classes with __slots__ are incompatible with the weakref module unless they
explicitly state __weakref__ among their slots
"""
from weakref import ref

a = DataArray(1)
r = ref(a)
assert r() is a
11 changes: 11 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5786,3 +5786,14 @@ class MyDS(Dataset):
pass

assert str(e.value) == "MyDS must explicitly define __slots__"


def test_weakref():
"""Classes with __slots__ are incompatible with the weakref module unless they
explicitly state __weakref__ among their slots
"""
from weakref import ref

ds = Dataset()
r = ref(ds)
assert r() is ds