From 4822bd8cb203863d8d4ac31ba35e1e7eab18e077 Mon Sep 17 00:00:00 2001 From: Justus Magin Date: Mon, 10 Jun 2024 17:39:17 +0200 Subject: [PATCH] fixes for the `pint` tests (#8983) * use the `Quantity` constructor instead of multiplying * manually install `pint` deps into the upstream-dev CI * stop explicitly installing `pint` dependencies --- ci/install-upstream-wheels.sh | 2 ++ xarray/tests/test_units.py | 13 +++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/ci/install-upstream-wheels.sh b/ci/install-upstream-wheels.sh index b43c6a61ec1..d728768439a 100755 --- a/ci/install-upstream-wheels.sh +++ b/ci/install-upstream-wheels.sh @@ -48,6 +48,8 @@ python -m pip install \ --pre \ --upgrade \ pyarrow +# manually install `pint` to pull in new dependencies +python -m pip install --upgrade pint python -m pip install \ --no-deps \ --upgrade \ diff --git a/xarray/tests/test_units.py b/xarray/tests/test_units.py index 2f11fe688b7..0e8fbe9198b 100644 --- a/xarray/tests/test_units.py +++ b/xarray/tests/test_units.py @@ -34,6 +34,7 @@ # always be treated like ndarrays unit_registry = pint.UnitRegistry(force_ndarray_like=True) Quantity = unit_registry.Quantity +no_unit_values = ("none", None) pytestmark = [ @@ -91,17 +92,13 @@ def array_strip_units(array): def array_attach_units(data, unit): - if isinstance(data, Quantity): + if isinstance(data, Quantity) and data.units != unit: raise ValueError(f"cannot attach unit {unit} to quantity {data}") - try: - quantity = data * unit - except np.core._exceptions.UFuncTypeError: - if isinstance(unit, unit_registry.Unit): - raise - - quantity = data + if unit in no_unit_values or (isinstance(unit, int) and unit == 1): + return data + quantity = unit_registry.Quantity(data, unit) return quantity