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

Type check sentinel values #3472

Merged
merged 5 commits into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 6 additions & 10 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from .formatting import format_item
from .indexes import Indexes, default_indexes
from .options import OPTIONS
from .utils import ReprObject, _check_inplace, either_dict_or_kwargs
from .utils import Default, ReprObject, __default, _check_inplace, either_dict_or_kwargs
from .variable import (
IndexVariable,
Variable,
Expand Down Expand Up @@ -270,8 +270,6 @@ class DataArray(AbstractArray, DataWithCoords):
_coarsen_cls = rolling.DataArrayCoarsen
_resample_cls = resample.DataArrayResample

__default = ReprObject("<default>")

dt = property(DatetimeAccessor)

def __init__(
Expand Down Expand Up @@ -387,18 +385,18 @@ def _replace(
self,
variable: Variable = None,
coords=None,
name: Optional[Hashable] = __default,
name: Union[Optional[Hashable], Default] = __default,
) -> "DataArray":
if variable is None:
variable = self.variable
if coords is None:
coords = self._coords
if name is self.__default:
if name is __default:
name = self.name
return type(self)(variable, coords, name=name, fastpath=True)

def _replace_maybe_drop_dims(
self, variable: Variable, name: Optional[Hashable] = __default
self, variable: Variable, name: Union[Optional[Hashable], Default] = __default
) -> "DataArray":
if variable.dims == self.dims and variable.shape == self.shape:
coords = self._coords.copy()
Expand Down Expand Up @@ -2450,13 +2448,11 @@ def identical(self, other: "DataArray") -> bool:
except (TypeError, AttributeError):
return False

__default_name = object()

def _result_name(self, other: Any = None) -> Optional[Hashable]:
# use the same naming heuristics as pandas:
# https://github.com/ContinuumIO/blaze/issues/458#issuecomment-51936356
other_name = getattr(other, "name", self.__default_name)
if other_name is self.__default_name or other_name == self.name:
other_name = getattr(other, "name", __default)
if other_name is __default or other_name == self.name:
return self.name
else:
return None
Expand Down
39 changes: 18 additions & 21 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@
from .options import OPTIONS, _get_keep_attrs
from .pycompat import dask_array_type
from .utils import (
Default,
Frozen,
SortedKeysDict,
__default,
_check_inplace,
decode_numpy_dict_values,
either_dict_or_kwargs,
Expand Down Expand Up @@ -410,7 +412,7 @@ class Dataset(Mapping, ImplementsDatasetReduce, DataWithCoords):
"""

_accessors: Optional[Dict[str, Any]]
_attrs: Optional[Dict[Hashable, Any]]
_attrs: Union[Dict[Hashable, Any], None]
_coord_names: Set[Hashable]
_dims: Dict[Hashable, int]
_encoding: Optional[Dict[Hashable, Any]]
Expand Down Expand Up @@ -856,23 +858,18 @@ def _construct_direct(
obj._accessors = None
return obj

__default = object()

@classmethod
def _from_vars_and_coord_names(cls, variables, coord_names, attrs=None):
return cls._construct_direct(variables, coord_names, attrs=attrs)

# TODO(shoyer): renable type checking on this signature when pytype has a
# good way to handle defaulting arguments to a sentinel value:
# https://github.com/python/mypy/issues/1803
def _replace( # type: ignore
def _replace(
self,
variables: Dict[Hashable, Variable] = None,
coord_names: Set[Hashable] = None,
dims: Dict[Any, int] = None,
attrs: Optional[Dict[Hashable, Any]] = __default,
indexes: Optional[Dict[Any, pd.Index]] = __default,
encoding: Optional[dict] = __default,
attrs: Union[None, Dict[Hashable, Any], Default] = __default,
indexes: Union[Optional[Dict[Any, pd.Index]], Default] = __default,
encoding: Union[Optional[dict], Default] = __default,
inplace: bool = False,
) -> "Dataset":
"""Fastpath constructor for internal use.
Expand All @@ -890,11 +887,11 @@ def _replace( # type: ignore
self._coord_names = coord_names
if dims is not None:
self._dims = dims
if attrs is not self.__default:
if attrs is not __default:
self._attrs = attrs
if indexes is not self.__default:
if indexes is not __default:
self._indexes = indexes
if encoding is not self.__default:
if encoding is not __default:
self._encoding = encoding
obj = self
else:
Expand All @@ -904,23 +901,23 @@ def _replace( # type: ignore
coord_names = self._coord_names.copy()
if dims is None:
dims = self._dims.copy()
if attrs is self.__default:
if attrs is __default:
attrs = copy.copy(self._attrs)
if indexes is self.__default:
if indexes is __default:
indexes = copy.copy(self._indexes)
if encoding is self.__default:
if encoding is __default:
encoding = copy.copy(self._encoding)
obj = self._construct_direct(
variables, coord_names, dims, attrs, indexes, encoding
)
return obj

def _replace_with_new_dims( # type: ignore
def _replace_with_new_dims(
self,
variables: Dict[Hashable, Variable],
coord_names: set = None,
attrs: Optional[Dict[Hashable, Any]] = __default,
indexes: Dict[Hashable, pd.Index] = __default,
attrs: Union[Optional[Dict[Hashable, Any]], Default] = __default,
indexes: Union[Dict[Hashable, pd.Index], Default] = __default,
inplace: bool = False,
) -> "Dataset":
"""Replace variables with recalculated dimensions."""
Expand All @@ -929,12 +926,12 @@ def _replace_with_new_dims( # type: ignore
variables, coord_names, dims, attrs, indexes, inplace=inplace
)

def _replace_vars_and_dims( # type: ignore
def _replace_vars_and_dims(
self,
variables: Dict[Hashable, Variable],
coord_names: set = None,
dims: Dict[Hashable, int] = None,
attrs: Dict[Hashable, Any] = __default,
attrs: Union[Dict[Hashable, Any], Default] = __default,
inplace: bool = False,
) -> "Dataset":
"""Deprecated version of _replace_with_new_dims().
Expand Down
8 changes: 8 additions & 0 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os.path
import re
import warnings
from enum import Enum
from typing import (
AbstractSet,
Any,
Expand Down Expand Up @@ -701,3 +702,10 @@ def get_temp_dimname(dims: Container[Hashable], new_dim: Hashable) -> Hashable:
while new_dim in dims:
new_dim = "_" + str(new_dim)
return new_dim


class Default(Enum):
sentinel = 0


__default = Default.sentinel
crusaderky marked this conversation as resolved.
Show resolved Hide resolved