|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import contextlib |
5 | 6 | import importlib |
6 | 7 | import inspect |
7 | 8 | import logging |
8 | 9 | import tempfile |
9 | | -from collections.abc import Sequence |
10 | 10 | from pathlib import Path |
11 | 11 | from typing import TYPE_CHECKING |
12 | 12 |
|
|
30 | 30 | from esmvalcore.iris_helpers import has_unstructured_grid, safe_convert_units |
31 | 31 |
|
32 | 32 | if TYPE_CHECKING: |
| 33 | + from collections.abc import Sequence |
| 34 | + |
33 | 35 | from esmvalcore.cmor.table import CoordinateInfo, VariableInfo |
34 | 36 | from esmvalcore.config import Session |
35 | 37 |
|
@@ -165,7 +167,8 @@ def get_cube_from_list( |
165 | 167 | for cube in cubes: |
166 | 168 | if cube.var_name == short_name: |
167 | 169 | 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) |
169 | 172 |
|
170 | 173 | def fix_data(self, cube: Cube) -> Cube: |
171 | 174 | """Apply fixes to the data of the cube. |
@@ -259,37 +262,33 @@ def get_fixes( |
259 | 262 | if project == "cordex": |
260 | 263 | driver = extra_facets["driver"].replace("-", "_").lower() |
261 | 264 | extra_facets["dataset"] = dataset |
262 | | - try: |
| 265 | + with contextlib.suppress(ImportError): |
263 | 266 | fixes_modules.append( |
264 | 267 | importlib.import_module( |
265 | 268 | f"esmvalcore.cmor._fixes.{project}.{driver}.{dataset}", |
266 | 269 | ), |
267 | 270 | ) |
268 | | - except ImportError: |
269 | | - pass |
270 | 271 | fixes_modules.append( |
271 | 272 | importlib.import_module( |
272 | 273 | "esmvalcore.cmor._fixes.cordex.cordex_fixes", |
273 | 274 | ), |
274 | 275 | ) |
275 | 276 | else: |
276 | | - try: |
| 277 | + with contextlib.suppress(ImportError): |
277 | 278 | fixes_modules.append( |
278 | 279 | importlib.import_module( |
279 | 280 | f"esmvalcore.cmor._fixes.{project}.{dataset}", |
280 | 281 | ), |
281 | 282 | ) |
282 | | - except ImportError: |
283 | | - pass |
284 | 283 |
|
285 | 284 | for fixes_module in fixes_modules: |
286 | | - classes = dict( |
287 | | - (name.lower(), value) |
| 285 | + classes = { |
| 286 | + name.lower(): value |
288 | 287 | for (name, value) in inspect.getmembers( |
289 | 288 | fixes_module, |
290 | 289 | inspect.isclass, |
291 | 290 | ) |
292 | | - ) |
| 291 | + } |
293 | 292 | for fix_name in (short_name, mip.lower(), "allvars"): |
294 | 293 | if fix_name in classes: |
295 | 294 | fixes.append( |
@@ -711,7 +710,7 @@ def _fix_longitude_0_360( |
711 | 710 | cube_coord: Coord, |
712 | 711 | ) -> tuple[Cube, Coord]: |
713 | 712 | """Fix longitude coordinate to be in [0, 360].""" |
714 | | - if not cube_coord.standard_name == "longitude": |
| 713 | + if cube_coord.standard_name != "longitude": |
715 | 714 | return (cube, cube_coord) |
716 | 715 |
|
717 | 716 | points = cube_coord.core_points() |
|
0 commit comments