diff --git a/cf/data/data.py b/cf/data/data.py index 97baabafda..402fc7e787 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -336,12 +336,6 @@ def __init__( >>> d = cf.Data(tuple('fly')) """ - if array is None and source is None: # don't create no/empty Data - raise ValueError( - "Can't create empty data: some input data or datum must be " - "provided via the 'source' or 'array' parameters." - ) - if source is None and isinstance(array, self.__class__): source = array @@ -387,6 +381,7 @@ def __init__( self.hardmask = hardmask if array is None: + # No data has been set return try: @@ -8318,13 +8313,16 @@ def to_dask_array(self, apply_mask_hardness=False): dask.array """ - if apply_mask_hardness: + if apply_mask_hardness and "dask" in self._custom: if self.hardmask: self.harden_mask() else: self.soften_mask() - return self._custom["dask"] + try: + return self._custom["dask"] + except KeyError: + raise ValueError(f"{self.__class__.__name__} object has no data") def datum(self, *index): """Return an element of the data array as a standard Python diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 48ade5f9e3..219f5c30de 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -151,15 +151,27 @@ def setUp(self): def test_Data__init__basic(self): """Test basic `__init__` cases for Data.""" - # Most __init__ parameters are covered by the various other tests, - # so this is mainly to check trivial cases and especially the edge - # case of 'default' Data i.e. if no parameters are specified. + # Most __init__ parameters are covered by the various other + # tests, so this is mainly to check trivial cases. cf.Data(0, "s") cf.Data(array=np.arange(5)) cf.Data(source=self.filename) + d = cf.Data() with self.assertRaises(ValueError): - cf.Data() + d.ndim + + with self.assertRaises(ValueError): + d.get_filenames() + + def test_Data__init__no_args(self): + """Test `__init__` with no arg.""" + # Most __init__ parameters are covered by the various other + # tests, so this is mainly to check trivial cases. + cf.Data() + cf.Data(0, "s") + cf.Data(array=np.arange(5)) + cf.Data(source=self.filename) def test_Data_equals(self): """Test the equality-testing Data method."""