Skip to content

Commit 74c21f3

Browse files
Configure numpy printoptions legacy (#6557)
* Configure numpy printoptions legacy * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6c84ea4 commit 74c21f3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/iris/cube.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import dask.array as da
2929
import numpy as np
3030
import numpy.ma as ma
31+
from packaging.version import Version
3132

3233
import iris._constraints
3334
from iris._data_manager import DataManager
@@ -60,6 +61,15 @@
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 = (
68+
str(_NP_LEGACY_VERSION)
69+
if Version(f"{_NP_VERSION.major}.{_NP_VERSION.minor}") > _NP_LEGACY_VERSION
70+
else False
71+
)
72+
6373
# The XML namespace to use for CubeML documents
6474
XML_NAMESPACE_URI = "urn:x-iris:cubeml-0.2"
6575

@@ -162,7 +172,7 @@ def insert(self, index, cube):
162172

163173
def xml(self, checksum=False, order=True, byteorder=True):
164174
"""Return a string of the XML that this list of cubes represents."""
165-
with np.printoptions(legacy="2.2"):
175+
with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY):
166176
doc = Document()
167177
cubes_xml_element = doc.createElement("cubes")
168178
cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
@@ -3865,7 +3875,7 @@ def xml(
38653875
byteorder: bool = True,
38663876
) -> str:
38673877
"""Return a fully valid CubeML string representation of the Cube."""
3868-
with np.printoptions(legacy="2.2"):
3878+
with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY):
38693879
doc = Document()
38703880

38713881
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)