Skip to content

Commit

Permalink
Convenience - get_latest_load_problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
trexfeathers committed Mar 3, 2025
1 parent c7ea621 commit 6a15ca1
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 21 deletions.
13 changes: 13 additions & 0 deletions lib/iris/tests/_shared_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,19 @@ def env_bin_path(exe_name: Optional[str] = None):
return exe_path


def get_latest_load_problem():
"""Return the latest :class:`iris.loading.LoadProblemsEntry` instance.
Simplifies checking for new additions to LOAD_PROBLEMS, without
worrying about clearing LOAD_PROBLEMS during setup, or using some
sort of context manager.
"""
from iris.loading import LOAD_PROBLEMS

load_problems = list(LOAD_PROBLEMS.values())[-1]
return load_problems[-1]


class GraphicsTest:
"""All inheriting classes automatically have access to ``self.check_graphic()``."""

Expand Down
5 changes: 2 additions & 3 deletions lib/iris/tests/integration/netcdf/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
# Get the netCDF4 module, but in a sneaky way that avoids triggering the "do not import
# netCDF4" check in "iris.tests.test_coding_standards.test_netcdf4_import()".
import iris.fileformats.netcdf._thread_safe_nc as threadsafe_nc
from iris.loading import LOAD_PROBLEMS
import iris.warnings

nc = threadsafe_nc.netCDF4

from iris.tests._shared_utils import get_latest_load_problem
from iris.tests.stock.netcdf import ncgen_from_cdl


Expand Down Expand Up @@ -361,8 +361,7 @@ def test_lat_not_loaded(self):
cube = iris.load_cube(self.nc_path)
with pytest.raises(iris.exceptions.CoordinateNotFoundError):
_ = cube.coord("lat")
load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
assert isinstance(load_problem.loaded, iris.coords.DimCoord)
assert load_problem.loaded.name() == "latitude"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import iris.coord_systems as ics
import iris.fileformats._nc_load_rules.helpers as hh
from iris.loading import LOAD_PROBLEMS
from iris.tests._shared_utils import get_latest_load_problem
from iris.tests.unit.fileformats.nc_load_rules.actions import Mixin__nc_load_actions


Expand Down Expand Up @@ -337,8 +337,7 @@ def check_result(
self.assertEqual(yco_cs, cube_cs)

if load_problems_regex is not None:
load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
self.assertRegex(str(load_problem.stack_trace), load_problems_regex)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from iris.common import LimitedAttributeDict
from iris.coord_systems import GeogCS, RotatedGeogCS
from iris.loading import LOAD_PROBLEMS
from iris.tests._shared_utils import get_latest_load_problem
from iris.tests.unit.fileformats.nc_load_rules.actions import Mixin__nc_load_actions


Expand Down Expand Up @@ -160,8 +160,7 @@ def check_result(
def check_load_problem(self, setup_kwargs, expected_msg):
# Check that the expected load problem is stored.
_ = self.run_testcase(**setup_kwargs)
load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
attributes = load_problem.loaded.attributes[LimitedAttributeDict.IRIS_RAW]
self.assertEqual(attributes["standard_name"], setup_kwargs["standard_name"])
self.assertRegex("".join(load_problem.stack_trace.format()), expected_msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import iris.tests as tests # isort: skip

from iris.coords import AuxCoord, DimCoord
from iris.loading import LOAD_PROBLEMS
from iris.tests._shared_utils import get_latest_load_problem
from iris.tests.unit.fileformats.nc_load_rules.actions import Mixin__nc_load_actions


Expand Down Expand Up @@ -218,8 +218,7 @@ def check_result(
self.assertIsInstance(period_auxcos[0], AuxCoord)

if load_problems_regex is not None:
load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
self.assertRegex(str(load_problem.stack_trace), load_problems_regex)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from iris.coords import AuxCoord, DimCoord
from iris.exceptions import CannotAddError
from iris.fileformats._nc_load_rules.helpers import build_and_add_dimension_coordinate
from iris.loading import LOAD_PROBLEMS
from iris.tests._shared_utils import get_latest_load_problem


def _make_bounds_var(bounds, dimensions, units):
Expand Down Expand Up @@ -238,8 +238,7 @@ def test_aux_coord_construction(self):

# Test that expected coord is built and added to cube.
self.engine.cube.add_aux_coord.assert_called_with(expected_coord, [0])
load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
self.assertIn(
"creating wibble auxiliary coordinate instead",
"".join(load_problem.stack_trace.format()),
Expand All @@ -259,8 +258,7 @@ def mock_add_dim_coord(_, __):
with self.deferred_load_patch, self.get_cf_bounds_var_patch:
build_and_add_dimension_coordinate(self.engine, self.cf_coord_var)

load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
assert load_problem.stack_trace.exc_type is CannotAddError
assert self.engine.cube_parts["coordinates"] == []

Expand All @@ -278,8 +276,7 @@ def mock_add_aux_coord(_, __):
with self.deferred_load_patch, self.get_cf_bounds_var_patch:
build_and_add_dimension_coordinate(self.engine, self.cf_coord_var)

load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
assert load_problem.stack_trace.exc_type is CannotAddError
assert self.engine.cube_parts["coordinates"] == []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from iris.fileformats._nc_load_rules.helpers import build_and_add_names
from iris.loading import LOAD_PROBLEMS
from iris.tests._shared_utils import get_latest_load_problem

from .test_build_cube_metadata import _make_engine

Expand All @@ -35,8 +36,7 @@ def check_load_problems(self, invalid_standard_name=None):
if invalid_standard_name is None:
self.assertEqual(LOAD_PROBLEMS, {})
else:
load_problems = list(LOAD_PROBLEMS.values())[-1]
load_problem = load_problems[-1]
load_problem = get_latest_load_problem()
self.assertEqual(
load_problem.loaded, {"standard_name": invalid_standard_name}
)
Expand Down

0 comments on commit 6a15ca1

Please sign in to comment.