Skip to content

Commit 0e8e716

Browse files
committed
Manual fixes and ignores
1 parent 0258311 commit 0e8e716

63 files changed

Lines changed: 230 additions & 203 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

esmvalcore/_main.py

100755100644
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ def _clean_preproc(session):
521521

522522
if (
523523
not session["save_intermediary_cubes"]
524-
and session._fixed_file_dir.exists()
524+
and session._fixed_file_dir.exists() # noqa: SLF001
525525
):
526526
logger.debug(
527527
"Removing `preproc/fixed_files` directory containing fixed "
@@ -532,7 +532,7 @@ def _clean_preproc(session):
532532
"`save_intermediary_cubes` to `true` and `remove_preproc_dir` "
533533
"to `false` in your configuration",
534534
)
535-
shutil.rmtree(session._fixed_file_dir)
535+
shutil.rmtree(session._fixed_file_dir) # noqa: SLF001
536536

537537
if session["remove_preproc_dir"] and session.preproc_dir.exists():
538538
logger.info(
@@ -634,7 +634,6 @@ def display(lines, out):
634634
logger.exception(
635635
"Program terminated abnormally, see stack trace "
636636
"below for more information:",
637-
exc_info=True,
638637
)
639638
logger.info(
640639
"\n"

esmvalcore/_recipe/check.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def variable(
167167
def _log_data_availability_errors(dataset):
168168
"""Check if the required input data is available."""
169169
input_files = dataset.files
170-
patterns = dataset._file_globs
170+
patterns = dataset._file_globs # noqa: SLF001
171171
if not input_files:
172172
logger.error("No input files found for %s", dataset)
173173
if patterns:
@@ -436,22 +436,20 @@ def _check_duration_periods(timerange):
436436
try:
437437
isodate.parse_duration(timerange[0])
438438
except isodate.isoerror.ISO8601Error as exc:
439-
raise RecipeError(
440-
"Invalid value encountered for `timerange`. "
441-
f"{timerange[0]} is not valid duration according to ISO 8601."
442-
+ "\n"
443-
+ str(exc),
444-
) from exc
439+
msg = (
440+
f"Invalid value encountered for `timerange`. {timerange[0]} is "
441+
f"not valid duration according to ISO 8601.\n{exc}"
442+
)
443+
raise RecipeError(msg) from exc
445444
elif timerange[1].startswith("P"):
446445
try:
447446
isodate.parse_duration(timerange[1])
448447
except isodate.isoerror.ISO8601Error as exc:
449-
raise RecipeError(
450-
"Invalid value encountered for `timerange`. "
451-
f"{timerange[1]} is not valid duration according to ISO 8601."
452-
+ "\n"
453-
+ str(exc),
454-
) from exc
448+
msg = (
449+
f"Invalid value encountered for `timerange`. {timerange[1]} is "
450+
f"not valid duration according to ISO 8601.\n{exc}"
451+
)
452+
raise RecipeError(msg) from exc
455453

456454

457455
def _check_format_years(date):
@@ -475,13 +473,14 @@ def _check_timerange_values(date, timerange):
475473
else:
476474
isodate.parse_date(date)
477475
except isodate.isoerror.ISO8601Error as exc:
478-
raise RecipeError(
476+
msg = (
479477
"Invalid value encountered for `timerange`. "
480478
"Valid value must follow ISO 8601 standard "
481479
"for dates and duration periods, or be "
482480
"set to '*' to load available years. "
483-
f"Got {timerange} instead." + "\n" + str(exc),
484-
) from exc
481+
f"Got {timerange} instead.\n{exc}"
482+
)
483+
raise RecipeError(msg) from exc
485484

486485

487486
def valid_time_selection(timerange):

esmvalcore/_recipe/to_datasets.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ def datasets_from_recipe(
425425
return datasets
426426

427427

428-
def _dataset_from_files(dataset: Dataset) -> list[Dataset]:
428+
def _dataset_from_files( # noqa: C901
429+
dataset: Dataset,
430+
) -> list[Dataset]:
429431
"""Replace facet values of '*' based on available files."""
430432
result: list[Dataset] = []
431433
errors: list[str] = []
@@ -519,7 +521,7 @@ def _report_unexpanded_globs(
519521
)
520522
else:
521523
timerange = expanded_ds.facets.get("timerange")
522-
patterns = expanded_ds._file_globs
524+
patterns = expanded_ds._file_globs # noqa: SLF001
523525
msg = (
524526
f"{msg}\nNo files found matching:\n"
525527
+ "\n".join(str(p) for p in patterns) # type: ignore[union-attr]

esmvalcore/cmor/_fixes/cesm/cesm2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
import logging
19+
from typing import ClassVar
1920

2021
from iris.cube import CubeList
2122

@@ -28,7 +29,7 @@ class AllVars(NativeDatasetFix):
2829
"""Fixes for all variables."""
2930

3031
# Dictionary to map invalid units in the data to valid entries
31-
INVALID_UNITS = {
32+
INVALID_UNITS: ClassVar[dict[str, str]] = {
3233
"fraction": "1",
3334
}
3435

esmvalcore/cmor/_fixes/cmip6/mcm_ua_1_0.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ def strip_cube_metadata(cube):
2626
class AllVars(Fix):
2727
"""Fixes for all variables."""
2828

29-
def fix_metadata(self, cubes):
29+
def fix_metadata( # noqa: C901
30+
self,
31+
cubes,
32+
):
3033
"""Fix metadata.
3134
3235
Remove unnecessary spaces in metadata and rename ``var_name`` of

esmvalcore/cmor/_fixes/emac/emac.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import logging
1515
import warnings
1616
from shutil import copyfile
17+
from typing import ClassVar
1718

1819
import dask.array as da
1920
import iris.analysis
@@ -36,7 +37,7 @@ class AllVars(EmacFix):
3637
"""Fixes for all variables."""
3738

3839
# Dictionary to map invalid units in the data to valid entries
39-
INVALID_UNITS = {
40+
INVALID_UNITS: ClassVar[dict[str, str]] = {
4041
"kg/m**2s": "kg m-2 s-1",
4142
}
4243

esmvalcore/cmor/_fixes/fix.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -844,25 +844,21 @@ def _fix_time_units(self, cube: Cube, cube_coord: Coord) -> None:
844844

845845
branch_parent = "branch_time_in_parent"
846846
if branch_parent in attrs:
847-
try:
847+
# Overflow happens when the time is very large.
848+
with contextlib.suppress(OverflowError):
848849
attrs[branch_parent] = parent_units.convert(
849850
attrs[branch_parent],
850851
cube_coord.units,
851852
)
852-
except OverflowError:
853-
# This happens when the time is very large.
854-
pass
855853

856854
branch_child = "branch_time_in_child"
857855
if branch_child in attrs:
858-
try:
856+
# Overflow happens when the time is very large.
857+
with contextlib.suppress(OverflowError):
859858
attrs[branch_child] = old_units.convert(
860859
attrs[branch_child],
861860
cube_coord.units,
862861
)
863-
except OverflowError:
864-
# This happens when the time is very large.
865-
pass
866862

867863
def _fix_time_bounds(self, cube: Cube, cube_coord: Coord) -> None:
868864
"""Fix time bounds."""

esmvalcore/cmor/_fixes/native6/era5.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@ def fix_metadata(self, cubes):
497497
class AllVars(Fix):
498498
"""Fixes for all variables."""
499499

500-
def _fix_coordinates(self, cube):
500+
def _fix_coordinates( # noqa: C901
501+
self,
502+
cube,
503+
):
501504
"""Fix coordinates."""
502505
# Add scalar height coordinates
503506
if "height2m" in self.vardef.dimensions:

esmvalcore/cmor/_fixes/native_datasets.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from __future__ import annotations
44

55
import logging
6-
from typing import TYPE_CHECKING
6+
from contextlib import suppress
7+
from typing import TYPE_CHECKING, ClassVar
78

89
from iris import NameConstraint
910

@@ -27,7 +28,7 @@ class NativeDatasetFix(Fix):
2728
"""Common fix operations for native datasets."""
2829

2930
# Dictionary to map invalid units in the data to valid entries
30-
INVALID_UNITS: dict[str, str] = {}
31+
INVALID_UNITS: ClassVar[dict[str, str]] = {}
3132

3233
def fix_scalar_coords(self, cube: Cube) -> None:
3334
"""Add missing scalar coordinate to cube (in-place).
@@ -240,10 +241,8 @@ def guess_coord_bounds(cube: Cube, coord: Coord) -> Coord:
240241
if isinstance(coord, str):
241242
coord = cube.coord(coord)
242243
if not coord.has_bounds():
243-
try:
244+
with suppress(ValueError):
244245
coord.guess_bounds()
245-
except ValueError: # Coord has only 1 point
246-
pass
247246
return coord
248247

249248
@staticmethod

esmvalcore/cmor/check.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from __future__ import annotations
44

55
import logging
6-
from collections import namedtuple
76
from enum import IntEnum
87
from functools import cached_property
9-
from typing import TYPE_CHECKING
8+
from typing import TYPE_CHECKING, NamedTuple
109

1110
import cf_units
1211
import dask
@@ -247,7 +246,9 @@ def _check_fill_value(self):
247246
#
248247
# => Very difficult to check!
249248

250-
def _check_var_metadata(self):
249+
def _check_var_metadata( # noqa: C901
250+
self,
251+
):
251252
"""Check metadata of variable."""
252253
# Check standard_name
253254
if self._cmor_var.standard_name:
@@ -534,7 +535,12 @@ def _check_coords(self):
534535

535536
def _check_coord_ranges(self, coords: list[tuple[CoordinateInfo, Coord]]):
536537
"""Check coordinate value are inside valid ranges."""
537-
Limit = namedtuple("Limit", ["name", "type", "limit", "value"])
538+
539+
class Limit(NamedTuple):
540+
name: str
541+
type: str
542+
limit: float
543+
value: float
538544

539545
limits = []
540546
for coord_info, coord in coords:
@@ -621,7 +627,12 @@ def _check_time_bounds(self, time):
621627
self._cmor_var.short_name,
622628
)
623629

624-
def _check_coord_monotonicity_and_direction(self, cmor, coord, var_name):
630+
def _check_coord_monotonicity_and_direction( # noqa: C901
631+
self,
632+
cmor,
633+
coord,
634+
var_name,
635+
):
625636
"""Check monotonicity and direction of coordinate."""
626637
if coord.ndim > 1:
627638
return
@@ -687,7 +698,9 @@ def _check_requested_values(self, coord, coord_info, var_name):
687698
str(coord.units),
688699
)
689700

690-
def _check_time_coord(self):
701+
def _check_time_coord( # noqa: C901,PLR0912,PLR0915
702+
self,
703+
):
691704
"""Check time coordinate."""
692705
try:
693706
coord = self._cube.coord("time", dim_coords=True)

0 commit comments

Comments
 (0)