diff --git a/lib/iris/common/mixin.py b/lib/iris/common/mixin.py index bcdc7f5f01..ce8e7a520c 100644 --- a/lib/iris/common/mixin.py +++ b/lib/iris/common/mixin.py @@ -179,6 +179,7 @@ class CfUnit(cf_units.Unit): @classmethod def from_unit(cls, unit: cf_units.Unit): """Cast a :class:`cf_units.Unit` to an :class:`Unit`.""" + unit = cf_units.as_unit(unit) if isinstance(unit, CfUnit): result = unit elif isinstance(unit, cf_units.Unit): @@ -234,6 +235,12 @@ def _round(date): return result + def __repr__(self): + # Adjust repr to look like the parent class, to avoid many CML errors. + string = super().__repr__() + string = string.replace(self.__class__.__name__ + "(", "Unit(", 1) + return string + if cfpint is not None: @@ -521,7 +528,7 @@ def is_vertical(self): # FOR NOW: insist on pint units -_DEFAULT_UNITCLASS: type = CfpintUnit +_DEFAULT_UNITCLASS: type = CfUnit # And force pint too. # TODO: since we may have seen problems with doing this dynamically, this could affect @@ -629,8 +636,8 @@ def units(self) -> cf_units.Unit | cfpint.Unit: @units.setter def units(self, unit: cf_units.Unit | cfpint.Unit | str | None) -> None: - # unit = cf_units.as_unit(unit) - self._metadata_manager.units = default_units_class().from_unit(unit) + unit = default_units_class().from_unit(unit) + self._metadata_manager.units = unit @property def attributes(self) -> LimitedAttributeDict: diff --git a/lib/iris/tests/unit/common/mixin/test_pintunits.py b/lib/iris/tests/unit/common/mixin/test_pintunits.py index 359c4c175e..c7cdaaed3a 100644 --- a/lib/iris/tests/unit/common/mixin/test_pintunits.py +++ b/lib/iris/tests/unit/common/mixin/test_pintunits.py @@ -1,3 +1,12 @@ +# Copyright Iris contributors +# +# This file is part of Iris and is released under the BSD license. +# See LICENSE in the root of the repository for full licensing details. +"""Stopgap testing for pint units. + +So far, only a few specific things are tested. +""" + from datetime import datetime import cf_units @@ -29,7 +38,9 @@ def test_nounit_eq(): def test_calendar(): unit = CfpintUnit("days since 1970-01-01", calendar="360_day") - assert repr(unit) == "" + # NOTE: no <>, due to "backwards compatibility" for assert_CDL + # TODO: remove the CfpintUnit._REPR_NO_LTGT + assert repr(unit) == "Unit('days since 1970-01-01', calendar='360_day')" # TODO: should really add the calendar to the string format # I think this is a bit horrible, # .. but it is cf_units behaviour + currently required for correct netcdf saving diff --git a/lib/iris/tests/unit/test_Future.py b/lib/iris/tests/unit/test_Future.py index e9ce4af222..445e8bf40d 100644 --- a/lib/iris/tests/unit/test_Future.py +++ b/lib/iris/tests/unit/test_Future.py @@ -122,7 +122,8 @@ class Test__str_repr: def _check_content(self, future, text): assert text == ( "Future(datum_support=False, pandas_ndim=False, save_split_attrs=False, " - "date_microseconds=False, derived_bounds=False, lam_pole_offset=False)" + "date_microseconds=False, derived_bounds=False, lam_pole_offset=False, " + "use_cfpint_units=False)" ) # Also just check that all the property elements are included for propname in future.__dict__.keys(): diff --git a/noxfile.py b/noxfile.py index 4d733fa2d4..705446cb1e 100644 --- a/noxfile.py +++ b/noxfile.py @@ -6,6 +6,7 @@ import hashlib import os +import pathlib from pathlib import Path import nox @@ -181,6 +182,31 @@ def tests(session: nox.sessions.Session): prepare_venv(session) session.install("--no-deps", "--editable", ".") session.env.update(ENV) + + # HACK EXTRAS: install pint; download cfpint + make it importable with a pth + session.conda_install("pint") + + def expth(pth): + return str(pathlib.Path(pth).expanduser().absolute()) + + session.run("mkdir", "-p", expth("~/extra_installs"), external=True) + session.run( + "git", + "clone", + "https://github.com/SciTools/cfpint.git", + expth("~/extra_installs/cfpint"), + external=True, + ) + session.run( + "mkdir", + "-p", + expth(f"~/.local/lib/python{session.python}/site-packages"), + external=True, + ) + path = expth(f"~/.local/lib/python{session.python}/site-packages/cfpint.pth") + with open(path, "w") as f_out: + f_out.write(expth("~/extra_installs/cfpint/src")) + run_args = [ "pytest", "-n",