Skip to content

Commit cf0f21e

Browse files
committed
Configure numpy printoptions legacy
1 parent 2822889 commit cf0f21e

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/iris/cube.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from functools import partial, reduce
2020
import itertools
2121
import operator
22+
from packaging.version import Version
2223
from typing import TYPE_CHECKING, Any, Optional, TypeGuard
2324
import warnings
2425
from xml.dom.minidom import Document
@@ -60,6 +61,11 @@
6061
__all__ = ["Cube", "CubeAttrsDict", "CubeList"]
6162

6263

64+
# Determine the numpy printoptions legacy setting.
65+
_NP_VERSION = Version(np.__version__)
66+
_NP_LEGACY_VERSION = Version("2.2")
67+
NP_PRINTOPTIONS_LEGACY = str(_NP_LEGACY_VERSION) if Version(f"{_NP_VERSION.major}.{_NP_VERSION.minor}") > _NP_LEGACY_VERSION else False
68+
6369
# The XML namespace to use for CubeML documents
6470
XML_NAMESPACE_URI = "urn:x-iris:cubeml-0.2"
6571

@@ -162,7 +168,7 @@ def insert(self, index, cube):
162168

163169
def xml(self, checksum=False, order=True, byteorder=True):
164170
"""Return a string of the XML that this list of cubes represents."""
165-
with np.printoptions(legacy="2.2"):
171+
with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY):
166172
doc = Document()
167173
cubes_xml_element = doc.createElement("cubes")
168174
cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
@@ -3865,7 +3871,7 @@ def xml(
38653871
byteorder: bool = True,
38663872
) -> str:
38673873
"""Return a fully valid CubeML string representation of the Cube."""
3868-
with np.printoptions(legacy="2.2"):
3874+
with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY):
38693875
doc = Document()
38703876

38713877
cube_xml_element = self._xml_element(

lib/iris/tests/test_pp_module.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from numpy.testing import assert_array_equal
1313
import pytest
1414

15+
from iris.cube import NP_PRINTOPTIONS_LEGACY
1516
import iris.fileformats.pp as pp
1617
from iris.tests import _shared_utils
1718

@@ -64,7 +65,7 @@ def check_pp(self, pp_fields, reference_filename):
6465
for pp_field in pp_fields:
6566
pp_field.data
6667

67-
with np.printoptions(legacy="2.2"):
68+
with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY):
6869
test_string = str(pp_fields)
6970
reference_path = _shared_utils.get_result_path(reference_filename)
7071
if os.path.isfile(reference_path):

0 commit comments

Comments
 (0)