Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/iris/common/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion lib/iris/tests/unit/common/mixin/test_pintunits.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -29,7 +38,9 @@ def test_nounit_eq():

def test_calendar():
unit = CfpintUnit("days since 1970-01-01", calendar="360_day")
assert repr(unit) == "<Unit('days since 1970-01-01', calendar='360_day')>"
# 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
Expand Down
3 changes: 2 additions & 1 deletion lib/iris/tests/unit/test_Future.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
26 changes: 26 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import hashlib
import os
import pathlib
from pathlib import Path

import nox
Expand Down Expand Up @@ -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",
Expand Down
Loading