Skip to content

Commit

Permalink
MAINT: Lint remaining files
Browse files Browse the repository at this point in the history
  • Loading branch information
Abel Aoun committed Dec 21, 2023
1 parent 34c65dc commit 6ca2191
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
41 changes: 21 additions & 20 deletions src/icclim/generic_indices/generic_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,7 @@ def preprocess(
coef: float | None,
sampling_method: str,
) -> list[ClimateVariable]:
if not _same_freq_for_all(climate_vars):
msg = (
"All variables must have the same time frequency (for example daily) to"
" be compared with each others, but this was not the case."
)
raise InvalidIcclimArgumentError(
msg,
)
if self.check_vars is not None:
self.check_vars(climate_vars, self)
if sampling_method not in self.sampling_methods:
msg = (
f"{self.name} can only be computed with the following"
f" sampling_method(s): {self.sampling_methods}"
)
raise InvalidIcclimArgumentError(
msg,
)
self._check_for_invalid_setup(climate_vars, sampling_method)
if output_unit is not None:
if _is_amount_unit(output_unit):
climate_vars = _convert_rates_to_amounts(
Expand All @@ -263,8 +246,6 @@ def preprocess(
climate_var.studied_data,
target=output_unit,
)
else:
pass # nothing to do
if coef is not None:
for climate_var in climate_vars:
climate_var.studied_data = coef * climate_var.studied_data
Expand Down Expand Up @@ -343,6 +324,26 @@ def __eq__(self, other) -> bool:
and self.process == other.process
)

def _check_for_invalid_setup(
self,
climate_vars: list[ClimateVariable],
sampling_method: str,
):
if not _same_freq_for_all(climate_vars):
msg = (
"All variables must have the same time frequency (for example daily) to"
" be compared with each others, but this was not the case."
)
raise InvalidIcclimArgumentError(msg)
if sampling_method not in self.sampling_methods:
msg = (
f"{self.name} can only be computed with the following"
f" sampling_method(s): {self.sampling_methods}"
)
raise InvalidIcclimArgumentError(msg)
if self.check_vars is not None:
self.check_vars(climate_vars, self)


def count_occurrences(
climate_vars: list[ClimateVariable],
Expand Down
9 changes: 5 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,7 @@ def test_indices_all_ignore_error(self):
).compute()
for i in EcadIndexRegistry.values():
# No variable in input to compute snow indices
if i.group == IndexGroupRegistry.SNOW:
assert res.data_vars.get(i.short_name, None) is None
elif "spi" in i.short_name.lower():
if i.group == IndexGroupRegistry.SNOW or "spi" in i.short_name.lower():
assert res.data_vars.get(i.short_name, None) is None
else:
assert res[i.short_name] is not None
Expand All @@ -518,7 +516,10 @@ def test_indices_all__error(self):
ds["tasmin"] = self.data
ds["pr"] = self.data.copy(deep=True)
ds["pr"].attrs[UNITS_KEY] = "kg m-2 d-1"
with pytest.raises(Exception):
with pytest.raises(
InvalidIcclimArgumentError,
match=r"Index .* needs the following .*",
):
icclim.indices(
index_group="all",
in_files=ds,
Expand Down
14 changes: 6 additions & 8 deletions tests/test_rechunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ def test_create_optimized_zarr_store_error():
},
).chunk({"time": 2})
# Then
with pytest.raises(InvalidIcclimArgumentError):
# When
with create_optimized_zarr_store(
in_files=ds,
var_names="TATAYOYO!",
target_zarr_store_name="yolo.zarr",
):
pass
with pytest.raises(InvalidIcclimArgumentError), create_optimized_zarr_store(
in_files=ds,
var_names="TATAYOYO!",
target_zarr_store_name="yolo.zarr",
):
pass


@patch("rechunker.rechunk")
Expand Down
6 changes: 3 additions & 3 deletions tests/test_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_per_threshold_min_value__operand_error():

def test_per_threshold_min_value__type_error():
with pytest.raises(NotImplementedError):
build_threshold(">10doy_per", threshold_min_value=dict(random="stuff")) # noqa
build_threshold(">10doy_per", threshold_min_value={"random": "stuff"})


def test_per_threshold_min_value__string():
Expand Down Expand Up @@ -203,7 +203,7 @@ def test_build_per_threshold__from_query():
assert res.is_ready is False
assert isinstance(res.prepare, Callable)
with pytest.raises(RuntimeError): # not computed yet
res.value # noqa
_ = res.value


def test_build_basic_threshold__from_dataarray():
Expand Down Expand Up @@ -269,7 +269,7 @@ class Test_FileBased:
)

@pytest.fixture(autouse=True)
def cleanup(self):
def _cleanup(self):
# setup
# ...
yield
Expand Down

0 comments on commit 6ca2191

Please sign in to comment.