Skip to content

Commit

Permalink
Leave empty slot when not using accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
crusaderky authored Nov 15, 2019
1 parent ee9da17 commit aa876cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
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 @@ -419,17 +419,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 @@ -535,7 +535,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 @@ -870,7 +869,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

0 comments on commit aa876cf

Please sign in to comment.