From 9b949fe850a28d043a4aeeecc4ef878d4893cd32 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Mon, 2 Nov 2020 11:33:07 +0000 Subject: [PATCH 01/11] unpack list comprehension --- pint_xarray/accessors.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index bb1888e9..ea10aae7 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -498,19 +498,19 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs): unit_attrs = conversion.extract_unit_attributes(self.ds) new_obj = conversion.strip_unit_attributes(self.ds) - units = { - name: _decide_units(unit, registry, attr) - for name, (unit, attr) in zip_mappings(units, unit_attrs).items() - if unit is not None or attr is not None - } + possible_new_units = zip_mappings(units, unit_attrs) + new_units = {} + for name, (unit, attr) in possible_new_units.items(): + if unit is not None or attr is not None: + new_units[name] = _decide_units(unit, registry, attr) # TODO: remove once indexes support units - dim_units = {name: unit for name, unit in units.items() if name in new_obj.dims} + dim_units = {name: unit for name, unit in new_units.items() if name in new_obj.dims} for name in dim_units.keys(): - units.pop(name) + new_units.pop(name) new_obj = conversion.attach_unit_attributes(new_obj, dim_units) - return conversion.attach_units(new_obj, units) + return conversion.attach_units(new_obj, new_units) def dequantify(self): """ From 05243548ec51fa8cbdb05333a55c2f7f0a13feff Mon Sep 17 00:00:00 2001 From: Tom Nicholas Date: Mon, 2 Nov 2020 13:07:44 +0100 Subject: [PATCH 02/11] raise from to give context --- pint_xarray/accessors.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index ea10aae7..465ca1b8 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -502,7 +502,11 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs): new_units = {} for name, (unit, attr) in possible_new_units.items(): if unit is not None or attr is not None: - new_units[name] = _decide_units(unit, registry, attr) + try: + new_units[name] = _decide_units(unit, registry, attr) + except Exception as e: + raise ValueError( + f"Failed to assign units to variable {name}") from e # TODO: remove once indexes support units dim_units = {name: unit for name, unit in new_units.items() if name in new_obj.dims} From c07333c1efab779375d47399c5395ca1c27a94e0 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Mon, 2 Nov 2020 13:49:48 +0000 Subject: [PATCH 03/11] re-raise same type of error --- pint_xarray/accessors.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 465ca1b8..d78a0526 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -505,8 +505,7 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs): try: new_units[name] = _decide_units(unit, registry, attr) except Exception as e: - raise ValueError( - f"Failed to assign units to variable {name}") from e + raise type(e)(f"Failed to assign units to variable {name}") from e # TODO: remove once indexes support units dim_units = {name: unit for name, unit in new_units.items() if name in new_obj.dims} From e76de0c98a7cb84c98d8028314d0b353d71cbb67 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Mon, 2 Nov 2020 17:50:11 +0000 Subject: [PATCH 04/11] added test --- pint_xarray/tests/test_accessors.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pint_xarray/tests/test_accessors.py b/pint_xarray/tests/test_accessors.py index e7026884..13823a6f 100644 --- a/pint_xarray/tests/test_accessors.py +++ b/pint_xarray/tests/test_accessors.py @@ -222,6 +222,11 @@ def test_error_on_nonsense_units(self, example_unitless_ds): with pytest.raises(UndefinedUnitError): ds.pint.quantify(units={"users": "aecjhbav"}) + def test_error_indicates_problematic_variable(self, example_unitless_ds): + ds = example_unitless_ds + with raises_regex(UndefinedUnitError, "users"): + ds.pint.quantify(units={"users": "aecjhbav"}) + class TestDequantifyDataSet: def test_strip_units(self, example_quantity_ds): From 968a952db809e1c93389b735a4b2c8d5d34a8cf1 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Mon, 2 Nov 2020 17:52:15 +0000 Subject: [PATCH 05/11] black formatting --- pint_xarray/accessors.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index d78a0526..26b12387 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -508,7 +508,9 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs): raise type(e)(f"Failed to assign units to variable {name}") from e # TODO: remove once indexes support units - dim_units = {name: unit for name, unit in new_units.items() if name in new_obj.dims} + dim_units = { + name: unit for name, unit in new_units.items() if name in new_obj.dims + } for name in dim_units.keys(): new_units.pop(name) new_obj = conversion.attach_unit_attributes(new_obj, dim_units) From fd75967b29ac4db659917d783de5294ed7cdc9ce Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Mon, 2 Nov 2020 17:55:05 +0000 Subject: [PATCH 06/11] updated what's new --- docs/whats-new.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/whats-new.rst b/docs/whats-new.rst index 41f3abc7..c23ef7d6 100644 --- a/docs/whats-new.rst +++ b/docs/whats-new.rst @@ -5,6 +5,8 @@ What's new ------------------ - rewrite :py:meth:`Dataset.pint.quantify` and :py:meth:`DataArray.pint.quantify`, to use pint's `parse_units` instead of `parse_expression` (:pull:`40`) +- ensure the variable which causes the error is explicit if an error occurs in + :py:meth:`Dataset.pint.quantify` (:pull:`43`) v0.1 (October 26 2020) ---------------------- From 06d109c345a3e0fccc6cf0815124c1ef8a1ddbea Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 14 Apr 2021 11:24:07 -0400 Subject: [PATCH 07/11] Raised more general ValueError instead --- pint_xarray/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index 20aee2e0..c10d2d18 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -1152,7 +1152,7 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs): try: new_units[name] = _decide_units(unit, registry, attr) except Exception as e: - raise type(e)(f"Failed to assign units to variable {name}") from e + raise ValueError(f"Failed to assign units to variable {name}") from e return self.ds.pipe(conversion.strip_unit_attributes).pipe( conversion.attach_units, new_units ) From 2babe7c84e7cc750c8929d38afc03ca16270a54e Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 14 Apr 2021 11:25:02 -0400 Subject: [PATCH 08/11] update tests to match new error types --- pint_xarray/tests/test_accessors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pint_xarray/tests/test_accessors.py b/pint_xarray/tests/test_accessors.py index 364b1933..39389eca 100644 --- a/pint_xarray/tests/test_accessors.py +++ b/pint_xarray/tests/test_accessors.py @@ -247,12 +247,12 @@ def test_error_when_already_units(self, example_quantity_ds): def test_error_on_nonsense_units(self, example_unitless_ds): ds = example_unitless_ds - with pytest.raises(UndefinedUnitError): + with pytest.raises(ValueError): ds.pint.quantify(units={"users": "aecjhbav"}) def test_error_indicates_problematic_variable(self, example_unitless_ds): ds = example_unitless_ds - with raises_regex(UndefinedUnitError, "users"): + with raises_regex(ValueError, "users"): ds.pint.quantify(units={"users": "aecjhbav"}) From a71112235ae084f0aea7571c57c04d5596396194 Mon Sep 17 00:00:00 2001 From: Thomas Nicholas Date: Wed, 14 Apr 2021 11:29:28 -0400 Subject: [PATCH 09/11] linting --- pint_xarray/accessors.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pint_xarray/accessors.py b/pint_xarray/accessors.py index c10d2d18..d68833b4 100644 --- a/pint_xarray/accessors.py +++ b/pint_xarray/accessors.py @@ -1152,12 +1152,13 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs): try: new_units[name] = _decide_units(unit, registry, attr) except Exception as e: - raise ValueError(f"Failed to assign units to variable {name}") from e + raise ValueError( + f"Failed to assign units to variable {name}" + ) from e return self.ds.pipe(conversion.strip_unit_attributes).pipe( conversion.attach_units, new_units ) - def dequantify(self, format=None): """ Convert units from the Dataset to string attributes. From 51285d1e5db167c08195e8b9b8b9fe05cdf262b0 Mon Sep 17 00:00:00 2001 From: Tom Nicholas <35968931+TomNicholas@users.noreply.github.com> Date: Wed, 14 Apr 2021 12:39:46 -0400 Subject: [PATCH 10/11] Update docs/whats-new.rst Co-authored-by: keewis --- docs/whats-new.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/whats-new.rst b/docs/whats-new.rst index 10b319fc..71c06918 100644 --- a/docs/whats-new.rst +++ b/docs/whats-new.rst @@ -7,6 +7,7 @@ What's new ------------------ - rewrite :py:meth:`Dataset.pint.quantify` and :py:meth:`DataArray.pint.quantify`, to use pint's `parse_units` instead of `parse_expression` (:pull:`40`) + By `Tom Nicholas `_. - ensure the variable which causes the error is explicit if an error occurs in :py:meth:`Dataset.pint.quantify` (:pull:`43`) By `Tom Nicholas `_. From 0d4bf5bcd5bce0c48cd2015359ab451363ca3a9a Mon Sep 17 00:00:00 2001 From: Tom Nicholas <35968931+TomNicholas@users.noreply.github.com> Date: Wed, 14 Apr 2021 12:40:03 -0400 Subject: [PATCH 11/11] use pytest raises with match Co-authored-by: keewis --- pint_xarray/tests/test_accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pint_xarray/tests/test_accessors.py b/pint_xarray/tests/test_accessors.py index 39389eca..fc3d32d2 100644 --- a/pint_xarray/tests/test_accessors.py +++ b/pint_xarray/tests/test_accessors.py @@ -252,7 +252,7 @@ def test_error_on_nonsense_units(self, example_unitless_ds): def test_error_indicates_problematic_variable(self, example_unitless_ds): ds = example_unitless_ds - with raises_regex(ValueError, "users"): + with pytest.raises(ValueError, match="users"): ds.pint.quantify(units={"users": "aecjhbav"})