Skip to content

Commit f7d004d

Browse files
authored
Enable annotations ruff rule (#2880)
1 parent 89ab5a3 commit f7d004d

Some content is hidden

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

82 files changed

+677
-343
lines changed

esmvalcore/_main.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
import sys
3636
from importlib.metadata import entry_points
3737
from pathlib import Path
38+
from typing import TYPE_CHECKING
3839

3940
import fire
4041

4142
from esmvalcore.config._config import warn_if_old_extra_facets_exist
4243

44+
if TYPE_CHECKING:
45+
from esmvalcore.config import Session
46+
4347
# set up logging
4448
logger = logging.getLogger(__name__)
4549

@@ -80,7 +84,7 @@ def parse_resume(resume, recipe):
8084
return resume
8185

8286

83-
def process_recipe(recipe_file: Path, session):
87+
def process_recipe(recipe_file: Path, session: Session) -> None:
8488
"""Process recipe."""
8589
import datetime
8690
import shutil
@@ -318,7 +322,7 @@ def _copy_config_file(
318322
in_file: Path,
319323
out_file: Path,
320324
overwrite: bool,
321-
):
325+
) -> None:
322326
"""Copy a configuration file."""
323327
import shutil
324328

@@ -634,7 +638,7 @@ def _create_session_dir(session):
634638
def _run(
635639
self,
636640
recipe: Path,
637-
session,
641+
session: Session,
638642
cli_config_dir: Path | None,
639643
) -> None:
640644
"""Run `recipe` using `session`."""
@@ -697,7 +701,7 @@ def _clean_preproc(session):
697701
shutil.rmtree(session.preproc_dir)
698702

699703
@staticmethod
700-
def _get_recipe(recipe) -> Path:
704+
def _get_recipe(recipe: str) -> Path:
701705
from esmvalcore.config._diagnostics import DIAGNOSTICS
702706

703707
if not os.path.isfile(recipe):

esmvalcore/_provenance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __init__(
139139
attributes: dict[str, Any] | None = None,
140140
ancestors: Iterable[TrackedFile] | None = None,
141141
prov_filename: str | None = None,
142-
):
142+
) -> None:
143143
"""Create an instance of a file with provenance tracking.
144144
145145
Arguments
@@ -182,7 +182,7 @@ def attributes(self) -> dict[str, Any]:
182182
return self._attributes
183183

184184
@attributes.setter
185-
def attributes(self, value: dict[str, Any] | None):
185+
def attributes(self, value: dict[str, Any] | None) -> None:
186186
"""Set attributes describing the file."""
187187
self._attributes = value
188188

@@ -194,11 +194,11 @@ def __repr__(self) -> str:
194194
"""Return representation string (e.g., used by ``pformat``)."""
195195
return f"{self.__class__.__name__}: {self.filename}"
196196

197-
def __eq__(self, other) -> bool:
197+
def __eq__(self, other: object) -> bool:
198198
"""Check if `other` equals `self`."""
199199
return hasattr(other, "filename") and self.filename == other.filename
200200

201-
def __lt__(self, other) -> bool:
201+
def __lt__(self, other: object) -> bool:
202202
"""Check if `other` should be sorted before `self`."""
203203
return hasattr(other, "filename") and self.filename < other.filename
204204

esmvalcore/_recipe/check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def _verify_span_value(span: str) -> None:
364364
raise RecipeError(msg)
365365

366366

367-
def _verify_groupby(groupby: Any) -> None:
367+
def _verify_groupby(groupby: list[str]) -> None:
368368
"""Raise error if groupby arguments cannot be verified."""
369369
if not isinstance(groupby, list):
370370
msg = (
@@ -375,7 +375,7 @@ def _verify_groupby(groupby: Any) -> None:
375375
raise RecipeError(msg)
376376

377377

378-
def _verify_keep_input_datasets(keep_input_datasets: Any) -> None:
378+
def _verify_keep_input_datasets(keep_input_datasets: bool) -> None:
379379
if not isinstance(keep_input_datasets, bool):
380380
msg = (
381381
f"Invalid value encountered for `keep_input_datasets`."
@@ -385,7 +385,7 @@ def _verify_keep_input_datasets(keep_input_datasets: Any) -> None:
385385
raise RecipeError(msg)
386386

387387

388-
def _verify_ignore_scalar_coords(ignore_scalar_coords: Any) -> None:
388+
def _verify_ignore_scalar_coords(ignore_scalar_coords: bool) -> None:
389389
if not isinstance(ignore_scalar_coords, bool):
390390
msg = (
391391
f"Invalid value encountered for `ignore_scalar_coords`."

esmvalcore/_recipe/from_datasets.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import itertools
66
import logging
77
import re
8-
from collections.abc import Iterable, Mapping, Sequence
8+
from collections.abc import Iterable, Mapping
99
from functools import partial
1010
from typing import TYPE_CHECKING, Any
1111

@@ -16,6 +16,7 @@
1616
from ._io import _load_recipe
1717

1818
if TYPE_CHECKING:
19+
from collections.abc import Sequence
1920
from pathlib import Path
2021

2122
from esmvalcore.dataset import Dataset
@@ -114,7 +115,7 @@ def _to_frozen(item):
114115
return item
115116

116117

117-
def _move_one_level_up(base: dict, level: str, target: str):
118+
def _move_one_level_up(base: dict, level: str, target: str) -> None:
118119
"""Move datasets one level up in the recipe."""
119120
groups = base[level]
120121
if not groups:

esmvalcore/_recipe/recipe.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from esmvalcore import __version__, esgf
1818
from esmvalcore._provenance import get_recipe_provenance
19-
from esmvalcore._task import BaseTask, DiagnosticTask, ResumeTask, TaskSet
19+
from esmvalcore._task import DiagnosticTask, ResumeTask, TaskSet
2020
from esmvalcore.config._config import TASKSEP
2121
from esmvalcore.config._dask import validate_dask_config
2222
from esmvalcore.config._diagnostics import TAGS
@@ -59,6 +59,9 @@
5959
if TYPE_CHECKING:
6060
from collections.abc import Iterable, Sequence
6161

62+
from prov.model import ProvEntity
63+
64+
from esmvalcore._task import BaseTask
6265
from esmvalcore.config import Session
6366
from esmvalcore.io.protocol import DataElement
6467
from esmvalcore.typing import Facets
@@ -964,7 +967,10 @@ def _need_ncl(raw_diagnostics: dict[str, Diagnostic]) -> bool:
964967
return True
965968
return False
966969

967-
def _initialize_provenance(self, raw_documentation: dict[str, Any]):
970+
def _initialize_provenance(
971+
self,
972+
raw_documentation: dict[str, Any],
973+
) -> ProvEntity:
968974
"""Initialize the recipe provenance."""
969975
doc = deepcopy(raw_documentation)
970976

@@ -1380,10 +1386,10 @@ def write_html_summary(self) -> None:
13801386
RecipeOutput,
13811387
)
13821388

1383-
output = self.get_output()
1384-
13851389
try:
1386-
output = RecipeOutput.from_core_recipe_output(output)
1390+
output = RecipeOutput.from_core_recipe_output(
1391+
self.get_output(),
1392+
)
13871393
except LookupError as error:
13881394
# See https://github.com/ESMValGroup/ESMValCore/issues/28
13891395
logger.warning("Could not write HTML report: %s", error)

esmvalcore/_recipe/to_datasets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import logging
6-
from collections.abc import Iterable, Iterator, Sequence
6+
from collections.abc import Iterable
77
from copy import deepcopy
88
from typing import TYPE_CHECKING, Any
99

@@ -23,6 +23,7 @@
2323
from ._io import _load_recipe
2424

2525
if TYPE_CHECKING:
26+
from collections.abc import Iterator, Sequence
2627
from pathlib import Path
2728

2829
from esmvalcore.config import Session

esmvalcore/_task.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""ESMValtool task definition."""
22

3+
from __future__ import annotations
4+
35
import abc
46
import contextlib
57
import datetime
@@ -283,7 +285,7 @@ def flatten(self):
283285
tasks.add(self)
284286
return tasks
285287

286-
def run(self, input_files=None):
288+
def run(self, input_files: list[str] | None = None) -> None:
287289
"""Run task."""
288290
if not self.output_files:
289291
if input_files is None:
@@ -308,7 +310,7 @@ def run(self, input_files=None):
308310
return self.output_files
309311

310312
@abc.abstractmethod
311-
def _run(self, input_files):
313+
def _run(self, input_files: list[str]) -> list[str]:
312314
"""Run task."""
313315

314316
def get_product_attributes(self) -> dict:
@@ -362,7 +364,7 @@ def __init__(self, prev_preproc_dir, preproc_dir, name):
362364

363365
super().__init__(ancestors=None, name=name, products=products)
364366

365-
def _run(self, _):
367+
def _run(self, _: list[str]) -> list[str]:
366368
"""Return the result of a previous run."""
367369
metadata = self.get_product_attributes()
368370

@@ -810,11 +812,11 @@ def available_cpu_count() -> int:
810812
class TaskSet(set):
811813
"""Container for tasks."""
812814

813-
def flatten(self) -> "TaskSet":
815+
def flatten(self) -> TaskSet:
814816
"""Flatten the list of tasks."""
815817
return TaskSet(t for task in self for t in task.flatten())
816818

817-
def get_independent(self) -> "TaskSet":
819+
def get_independent(self) -> TaskSet:
818820
"""Return a set of independent tasks."""
819821
independent_tasks = TaskSet()
820822
all_tasks = self.flatten()

esmvalcore/cmor/_fixes/cmip5/ec_earth.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Fixes for EC-Earth model."""
22

3-
from collections.abc import Iterable
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
46

57
import iris
68
import numpy as np
@@ -12,6 +14,9 @@
1214
cube_to_aux_coord,
1315
)
1416

17+
if TYPE_CHECKING:
18+
from collections.abc import Iterable
19+
1520

1621
class Sic(Fix):
1722
"""Fixes for sic."""

esmvalcore/cmor/_fixes/cmip6/cesm2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Fixes for CESM2 model."""
22

3-
from pathlib import Path
3+
from __future__ import annotations
4+
45
from shutil import copyfile
6+
from typing import TYPE_CHECKING
57

68
import iris
79
import iris.coords
@@ -18,6 +20,9 @@
1820
fix_ocean_depth_coord,
1921
)
2022

23+
if TYPE_CHECKING:
24+
from pathlib import Path
25+
2126

2227
class Cl(Fix):
2328
"""Fixes for ``cl``."""

esmvalcore/cmor/_fixes/cmip6/e3sm_1_1.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"""Fixes for E3SM-1-1 model."""
22

3-
from iris.cube import Cube
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
46

57
from esmvalcore.cmor.fix import Fix
68
from esmvalcore.preprocessor._shared import get_array_module
79

10+
if TYPE_CHECKING:
11+
from iris.cube import Cube
12+
813

914
def _mask_greater(cube: Cube, value: float) -> Cube:
1015
"""Mask all data of cube which is greater than ``value``."""

0 commit comments

Comments
 (0)