From 4324729a412c4ca2a48a061e0c2dac14d0127c57 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Thu, 5 Jan 2023 09:49:36 +0000 Subject: [PATCH 1/3] re-allow Data() --- cf/data/data.py | 9 +++------ cf/test/test_Data.py | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index 97baabafda..cb1f779491 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,9 @@ def __init__( self.hardmask = hardmask if array is None: + # Initialise the dask array as `None` to allow basic + # inspection without failing + self._set_dask(array) return try: diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 48ade5f9e3..7a1f6d47dc 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -151,16 +151,13 @@ 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() cf.Data(0, "s") cf.Data(array=np.arange(5)) cf.Data(source=self.filename) - with self.assertRaises(ValueError): - cf.Data() - def test_Data_equals(self): """Test the equality-testing Data method.""" shape = 3, 4 From 59a6e3dddff435c18422e67fa89a77d6c8383fc0 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Thu, 12 Jan 2023 08:52:34 +0000 Subject: [PATCH 2/3] trap no data in to_dask_array --- cf/data/data.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index cb1f779491..402fc7e787 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -381,9 +381,7 @@ def __init__( self.hardmask = hardmask if array is None: - # Initialise the dask array as `None` to allow basic - # inspection without failing - self._set_dask(array) + # No data has been set return try: @@ -8315,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 From 8e343bba8bb4a5b2e71f6e94308b89c725f0fda1 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Thu, 12 Jan 2023 18:13:20 +0000 Subject: [PATCH 3/3] test Data no arg init --- cf/test/test_Data.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 7a1f6d47dc..219f5c30de 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -153,6 +153,21 @@ 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. + cf.Data(0, "s") + cf.Data(array=np.arange(5)) + cf.Data(source=self.filename) + + d = cf.Data() + with self.assertRaises(ValueError): + 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))