Skip to content

Leave empty slot when not using accessors #3531

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

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ class DataArray(AbstractArray, DataWithCoords):
Dictionary for holding arbitrary metadata.
"""

_accessors: Optional[Dict[str, Any]] # noqa
_cache: Dict[str, Any]
_coords: Dict[Any, Variable]
_indexes: Optional[Dict[Hashable, pd.Index]]
_name: Optional[Hashable]
_variable: Variable

__slots__ = (
"_accessors",
"_cache",
"_coords",
"_file_obj",
"_indexes",
Expand Down Expand Up @@ -373,7 +373,6 @@ def __init__(
assert isinstance(coords, dict)
self._coords = coords
self._name = name
self._accessors = None

# TODO(shoyer): document this argument, once it becomes part of the
# public interface.
Expand Down
6 changes: 2 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,17 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords):
coordinates used for label based indexing.
"""

_accessors: Optional[Dict[str, Any]]
_attrs: Optional[Dict[Hashable, Any]]
_cache: Dict[str, Any]
_coord_names: Set[Hashable]
_dims: Dict[Hashable, int]
_encoding: Optional[Dict[Hashable, Any]]
_indexes: Optional[Dict[Hashable, pd.Index]]
_variables: Dict[Hashable, Variable]

__slots__ = (
"_accessors",
"_attrs",
"_cache",
"_coord_names",
"_dims",
"_encoding",
Expand Down Expand Up @@ -527,7 +527,6 @@ def __init__(
data_vars, coords, compat=compat
)

self._accessors = None
self._attrs = dict(attrs) if attrs is not None else None
self._file_obj = None
self._encoding = None
Expand Down Expand Up @@ -862,7 +861,6 @@ def _construct_direct(
obj._attrs = attrs
obj._file_obj = file_obj
obj._encoding = encoding
obj._accessors = None
return obj

@classmethod
Expand Down
13 changes: 9 additions & 4 deletions xarray/core/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ def __get__(self, obj, cls):
# we're accessing the attribute of the class, i.e., Dataset.geo
return self._accessor

# Use the same dict as @pandas.util.cache_readonly.
# It must be explicitly declared in obj.__slots__.
try:
return obj._accessors[self._name]
except TypeError:
obj._accessors = {}
cache = obj._cache
except AttributeError:
cache = obj._cache = {}

try:
return cache[self._name]
except KeyError:
pass

Expand All @@ -35,7 +40,7 @@ def __get__(self, obj, cls):
# something else (GH933):
raise RuntimeError("error initializing %r accessor." % self._name)

obj._accessors[self._name] = accessor_obj
cache[self._name] = accessor_obj
return accessor_obj


Expand Down