Skip to content

Commit 51b8c0d

Browse files
authored
Improve code quality and test coverage (#3029)
1 parent 653ee9c commit 51b8c0d

38 files changed

+380
-241
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,6 @@ doc/_sidebar.rst.inc
111111
*.ESMF_LogFile
112112

113113

114-
#Ignore vscode AI rules
114+
# AI
115115
.github/instructions/codacy.instructions.md
116+
CLAUDE.md

esmvalcore/_recipe/from_datasets.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ def _datasets_to_recipe(datasets: Iterable[Dataset]) -> Recipe:
6767
f"'diagnostic' facet missing from {dataset},"
6868
"unable to convert to recipe."
6969
)
70-
raise RecipeError(
71-
msg,
72-
)
70+
raise RecipeError(msg)
7371

7472
recipe = _datasets_to_raw_recipe(datasets)
7573
diagnostics = recipe["diagnostics"].values()

esmvalcore/_task.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,12 @@ def _py2ncl(value, var_name=""):
177177
type_ = type(value[0])
178178
if any(not isinstance(v, type_) for v in value):
179179
msg = f"NCL array cannot be mixed type: {value}"
180-
raise ValueError(
181-
msg,
182-
)
180+
raise ValueError(msg)
183181
txt += "(/{}/)".format(", ".join(_py2ncl(v) for v in value))
184182
elif isinstance(value, dict):
185183
if not var_name:
186184
msg = f"NCL does not support nested dicts: {value}"
187-
raise ValueError(
188-
msg,
189-
)
185+
raise ValueError(msg)
190186
txt += "True\n"
191187
for key in value:
192188
txt += f"{var_name}@{key} = {_py2ncl(value[key])}\n"
@@ -272,9 +268,7 @@ def initialize_provenance(self, recipe_entity):
272268
"""Initialize task provenance activity."""
273269
if self.activity is not None:
274270
msg = f"Provenance of {self} already initialized"
275-
raise ValueError(
276-
msg,
277-
)
271+
raise ValueError(msg)
278272
self.activity = get_task_provenance(self, recipe_entity)
279273

280274
def flatten(self):
@@ -445,18 +439,14 @@ def _initialize_cmd(self):
445439
msg = (
446440
f"{err_msg}: program '{interpreters[ext]}' not installed."
447441
)
448-
raise DiagnosticError(
449-
msg,
450-
)
442+
raise DiagnosticError(msg)
451443
cmd.append(interpreter)
452444
elif not os.access(script_file, os.X_OK):
453445
msg = (
454446
f"{err_msg}: non-executable file with unknown extension "
455447
f"'{script_file.suffix}'."
456448
)
457-
raise DiagnosticError(
458-
msg,
459-
)
449+
raise DiagnosticError(msg)
460450

461451
cmd.extend(args.get(ext, []))
462452
cmd.append(str(script_file))
@@ -674,9 +664,7 @@ def _run(self, input_files):
674664
f"Diagnostic script {self.script} failed with return code {returncode}. See the log "
675665
f"in {self.log}"
676666
)
677-
raise DiagnosticError(
678-
msg,
679-
)
667+
raise DiagnosticError(msg)
680668

681669
def _collect_provenance(self) -> None:
682670
"""Process provenance information provided by the diagnostic script."""

esmvalcore/_version.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@
99
"ESMValCore package not found, please run `pip install -e .` before "
1010
"importing the package."
1111
)
12-
raise PackageNotFoundError(
13-
msg,
14-
) from exc
12+
raise PackageNotFoundError(msg) from exc

esmvalcore/cmor/_fixes/access/_base_fix.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ def _get_path_from_facet(self, facet):
7575
path = Path(self.extra_facets[facet])
7676
if not path.is_file():
7777
msg = f"'{path}' given by facet '{facet}' does not exist"
78-
raise FileNotFoundError(
79-
msg,
80-
)
78+
raise FileNotFoundError(msg)
8179
return path
8280

8381
def load_ocean_grid_data(self, facet):

esmvalcore/cmor/_fixes/cordex/cordex_fixes.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ def _check_grid_differences(self, old_coord, new_coord):
131131
"Differences between the original grid and the "
132132
f"standardised grid are above 10e-4 {new_coord.units}."
133133
)
134-
raise RecipeError(
135-
msg,
136-
)
134+
raise RecipeError(msg)
137135

138136
def _fix_rotated_coords(self, cube, domain, domain_info):
139137
"""Fix rotated coordinates."""
@@ -221,8 +219,6 @@ def fix_metadata(self, cubes):
221219
"not supported in CORDEX datasets. Must be "
222220
"rotated_latitude_longitude or lambert_conformal_conic."
223221
)
224-
raise RecipeError(
225-
msg,
226-
)
222+
raise RecipeError(msg)
227223

228224
return cubes

esmvalcore/cmor/_fixes/emac/_base_fixes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def get_cube(self, cubes, var_name=None):
4040
f"available in the input file. Please specify a valid `raw_name` "
4141
f"in the recipe or as extra facets."
4242
)
43-
raise ValueError(
44-
msg,
45-
)
43+
raise ValueError(msg)
4644

4745

4846
class NegateData(EmacFix):

esmvalcore/cmor/_fixes/emac/emac.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ def _fix_plev(self, cube):
117117
f"'{self.vardef.short_name}', searched for Z-coordinates with "
118118
f"units that are convertible to Pa"
119119
)
120-
raise ValueError(
121-
msg,
122-
)
120+
raise ValueError(msg)
123121

124122
@staticmethod
125123
def _fix_alevel(cube, cubes):

esmvalcore/cmor/_fixes/icon/_base_fixes.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ def _get_grid_url(self, cube: Cube) -> tuple[str, str]:
162162
f"necessary to download the ICON horizontal grid file:\n"
163163
f"{cube}"
164164
)
165-
raise ValueError(
166-
msg,
167-
)
165+
raise ValueError(msg)
168166
grid_url = cube.attributes[self.GRID_FILE_ATTR]
169167
parsed_url = urlparse(grid_url)
170168
grid_name = Path(parsed_url.path).name
@@ -223,9 +221,7 @@ def _get_path_from_facet(
223221
f"relative to the auxiliary_data_dir "
224222
f"'{self.session['auxiliary_data_dir']}')"
225223
)
226-
raise FileNotFoundError(
227-
msg,
228-
)
224+
raise FileNotFoundError(msg)
229225
path = new_path
230226
return path
231227

@@ -650,9 +646,7 @@ def _add_coord_from_grid_file(self, cube: Cube, coord_name: str) -> None:
650646
f"'{coord_name}', cube does not contain a single unnamed "
651647
f"dimension:\n{cube}"
652648
)
653-
raise ValueError(
654-
msg,
655-
)
649+
raise ValueError(msg)
656650
coord_dims: tuple[()] | tuple[int] = ()
657651
for idx in range(cube.ndim):
658652
if not cube.coords(dimensions=idx, dim_coords=True):
@@ -678,9 +672,7 @@ def _add_time(self, cube: Cube, cubes: CubeList) -> Cube:
678672
f"'{self.vardef.short_name}', cube and other cubes in file do not "
679673
f"contain it"
680674
)
681-
raise ValueError(
682-
msg,
683-
)
675+
raise ValueError(msg)
684676

685677
def _get_z_coord(
686678
self,
@@ -987,9 +979,7 @@ def _get_previous_timestep(self, datetime_point: datetime) -> datetime:
987979
f"got {datetime_point}. Use `shift_time=false` in the "
988980
f"recipe to disable this feature"
989981
)
990-
raise ValueError(
991-
msg,
992-
)
982+
raise ValueError(msg)
993983

994984
# Decadal data
995985
if "dec" in freq:
@@ -1082,9 +1072,7 @@ def _fix_invalid_time_units(time_coord: Coord) -> None:
10821072
f"Expected time units '{time_format}' in input file, got "
10831073
f"'{t_unit}'"
10841074
)
1085-
raise ValueError(
1086-
msg,
1087-
)
1075+
raise ValueError(msg)
10881076
new_t_units = Unit(
10891077
"days since 1850-01-01",
10901078
calendar="proleptic_gregorian",

esmvalcore/cmor/_fixes/native_datasets.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ def fix_var_metadata(self, cube: Cube) -> None:
8686
f"Failed to fix invalid units '{invalid_units}' for "
8787
f"variable '{self.vardef.short_name}'"
8888
)
89-
raise ValueError(
90-
msg,
91-
) from exc
89+
raise ValueError(msg) from exc
9290
safe_convert_units(cube, self.vardef.units)
9391

9492
# Fix attributes
@@ -132,9 +130,7 @@ def get_cube(
132130
f"Variable '{var_name}' used to extract "
133131
f"'{self.vardef.short_name}' is not available in input file"
134132
)
135-
raise ValueError(
136-
msg,
137-
)
133+
raise ValueError(msg)
138134
return cubes.extract_cube(NameConstraint(var_name=var_name))
139135

140136
def fix_regular_time(

0 commit comments

Comments
 (0)