Skip to content

Commit 7e41997

Browse files
committed
Unsafe fixes
1 parent 8027304 commit 7e41997

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

esmvalcore/cmor/_fixes/fix.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from __future__ import annotations
44

5+
import contextlib
56
import importlib
67
import inspect
78
import logging
89
import tempfile
9-
from collections.abc import Sequence
1010
from pathlib import Path
1111
from typing import TYPE_CHECKING
1212

@@ -30,6 +30,8 @@
3030
from esmvalcore.iris_helpers import has_unstructured_grid, safe_convert_units
3131

3232
if TYPE_CHECKING:
33+
from collections.abc import Sequence
34+
3335
from esmvalcore.cmor.table import CoordinateInfo, VariableInfo
3436
from esmvalcore.config import Session
3537

@@ -165,7 +167,8 @@ def get_cube_from_list(
165167
for cube in cubes:
166168
if cube.var_name == short_name:
167169
return cube
168-
raise ValueError(f'Cube for variable "{short_name}" not found')
170+
msg = f'Cube for variable "{short_name}" not found'
171+
raise ValueError(msg)
169172

170173
def fix_data(self, cube: Cube) -> Cube:
171174
"""Apply fixes to the data of the cube.
@@ -259,37 +262,33 @@ def get_fixes(
259262
if project == "cordex":
260263
driver = extra_facets["driver"].replace("-", "_").lower()
261264
extra_facets["dataset"] = dataset
262-
try:
265+
with contextlib.suppress(ImportError):
263266
fixes_modules.append(
264267
importlib.import_module(
265268
f"esmvalcore.cmor._fixes.{project}.{driver}.{dataset}",
266269
),
267270
)
268-
except ImportError:
269-
pass
270271
fixes_modules.append(
271272
importlib.import_module(
272273
"esmvalcore.cmor._fixes.cordex.cordex_fixes",
273274
),
274275
)
275276
else:
276-
try:
277+
with contextlib.suppress(ImportError):
277278
fixes_modules.append(
278279
importlib.import_module(
279280
f"esmvalcore.cmor._fixes.{project}.{dataset}",
280281
),
281282
)
282-
except ImportError:
283-
pass
284283

285284
for fixes_module in fixes_modules:
286-
classes = dict(
287-
(name.lower(), value)
285+
classes = {
286+
name.lower(): value
288287
for (name, value) in inspect.getmembers(
289288
fixes_module,
290289
inspect.isclass,
291290
)
292-
)
291+
}
293292
for fix_name in (short_name, mip.lower(), "allvars"):
294293
if fix_name in classes:
295294
fixes.append(
@@ -711,7 +710,7 @@ def _fix_longitude_0_360(
711710
cube_coord: Coord,
712711
) -> tuple[Cube, Coord]:
713712
"""Fix longitude coordinate to be in [0, 360]."""
714-
if not cube_coord.standard_name == "longitude":
713+
if cube_coord.standard_name != "longitude":
715714
return (cube, cube_coord)
716715

717716
points = cube_coord.core_points()

esmvalcore/cmor/_fixes/native6/era5.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ def get_frequency(cube):
3333
"Percentage of the Grid Cell Occupied by Land (Including Lakes)",
3434
)
3535
if cube.long_name not in acceptable_long_names:
36-
raise ValueError(
36+
msg = (
3737
"Unable to infer frequency of cube "
38-
f"with length 1 time dimension: {cube}",
38+
f"with length 1 time dimension: {cube}"
39+
)
40+
raise ValueError(
41+
msg,
3942
)
4043
return "fx"
4144

@@ -63,9 +66,12 @@ def fix_accumulated_units(cube):
6366
elif get_frequency(cube) == "hourly":
6467
cube.units = cube.units * "h-1"
6568
elif get_frequency(cube) == "daily":
66-
raise NotImplementedError(
69+
msg = (
6770
f"Fixing of accumulated units of cube "
68-
f"{cube.summary(shorten=True)} is not implemented for daily data",
71+
f"{cube.summary(shorten=True)} is not implemented for daily data"
72+
)
73+
raise NotImplementedError(
74+
msg,
6975
)
7076
return cube
7177

esmvalcore/cmor/fix.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
import logging
1111
from collections import defaultdict
12-
from collections.abc import Iterable, Sequence
13-
from pathlib import Path
1412
from typing import TYPE_CHECKING, Any
1513

1614
from iris.cube import Cube, CubeList
@@ -19,7 +17,10 @@
1917
from esmvalcore.local import _get_start_end_date
2018

2119
if TYPE_CHECKING:
22-
from ..config import Session
20+
from collections.abc import Iterable, Sequence
21+
from pathlib import Path
22+
23+
from esmvalcore.config import Session
2324

2425
logger = logging.getLogger(__name__)
2526

0 commit comments

Comments
 (0)