Skip to content

Commit

Permalink
enable testing for PEP8 rule N802
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Apr 24, 2024
1 parent f3622cc commit 4b83974
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 40 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ select = [
"D",
"E",
"F",
"N802",
"W"
]

Expand Down
4 changes: 2 additions & 2 deletions tests/test_analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def test_compare_with_matlab(self):


class TestKS:
def test_1D_ks_2samp(self, random):
def test_1D_ks_2samp(self, random): # noqa: N802
# Compare with scipy.stats.ks_2samp
x = random.standard_normal(50) + 1
y = random.standard_normal(50)
Expand All @@ -210,7 +210,7 @@ def test_compare_with_matlab(self):
assert_almost_equal(dm, 0.96667, 4)


def analytical_KLDiv(p, q):
def analytical_KLDiv(p, q): # noqa: N802
"""Return the Kullback-Leibler divergence between two distributions.
Parameters
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cffwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def test_fire_weather_ufunc_errors(
),
],
)
def test_fire_season_R(self, open_dataset, key, kwargs):
def test_fire_season_R(self, open_dataset, key, kwargs): # noqa: N802
expected = self._get_cffdrs_fire_season(key)
in_ds = open_dataset("FWI/cffdrs_test_wDC.nc")
nid = int(key[2])
Expand Down
18 changes: 10 additions & 8 deletions tests/test_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ def test_module():
"""Translations are keyed according to the module where the indicators are defined."""
assert atmos.tg_mean.__module__.split(".")[2] == "atmos"
# Virtual module also are stored under xclim.indicators
assert xclim.indicators.cf.fg.__module__ == "xclim.indicators.cf"
assert xclim.indicators.icclim.GD4.__module__ == "xclim.indicators.icclim"
assert xclim.indicators.cf.fg.__module__ == "xclim.indicators.cf" # noqa: F821
assert (
xclim.indicators.icclim.GD4.__module__ == "xclim.indicators.icclim"
) # noqa: F821


def test_temp_unit_conversion(tas_series):
Expand Down Expand Up @@ -377,7 +379,7 @@ def test_multiindicator(tas_series):
compute=uniindtemp_compute,
)
with pytest.raises(ValueError, match="Indicator minmaxtemp4 was wrongly defined"):
tmin, tmax = ind(tas, freq="YS")
_tmin, _tmax = ind(tas, freq="YS")


def test_missing(tas_series):
Expand Down Expand Up @@ -604,7 +606,7 @@ def test_default_formatter():
assert default_formatter.format("{month}", month="m3") == "march"


def test_AttrFormatter():
def test_AttrFormatter(): # noqa: N802
fmt = AttrFormatter(
mapping={"evil": ["méchant", "méchante"], "nice": ["beau", "belle"]},
modifiers=["m", "f"],
Expand Down Expand Up @@ -655,14 +657,14 @@ def test_input_dataset(open_dataset):
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")

# Use defaults
out = xclim.atmos.daily_temperature_range(freq="YS", ds=ds)
_ = xclim.atmos.daily_temperature_range(freq="YS", ds=ds)

# Use non-defaults (inverted on purpose)
with xclim.set_options(cf_compliance="log"):
out = xclim.atmos.daily_temperature_range("tasmax", "tasmin", freq="YS", ds=ds)
_ = xclim.atmos.daily_temperature_range("tasmax", "tasmin", freq="YS", ds=ds)

# Use a mix
out = xclim.atmos.daily_temperature_range(tasmax=ds.tasmax, freq="YS", ds=ds)
_ = xclim.atmos.daily_temperature_range(tasmax=ds.tasmax, freq="YS", ds=ds)

# Inexistent variable:
dsx = ds.drop_vars("tasmin")
Expand Down Expand Up @@ -817,7 +819,7 @@ def test_indicator_call_errors(tas_series):
uniIndTemp(tas, oups=3)


def test_resamplingIndicator_new_error():
def test_resamplingIndicator_new_error(): # noqa: N802
with pytest.raises(ValueError, match="ResamplingIndicator require a 'freq'"):
Daily(
realm="atmos",
Expand Down
12 changes: 6 additions & 6 deletions tests/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -3690,42 +3690,42 @@ def test_wet_spell_frequency_op(pr_series):


class TestSfcWindMax:
def test_sfcWind_max(self, sfcWind_series):
def test_sfcWind_max(self, sfcWind_series): # noqa: N802
sfcWind = sfcWind_series(np.array([14.11, 15.27, 10.70]))
out = xci.sfcWind_max(sfcWind)
np.testing.assert_allclose(out, [15.27])


class TestSfcWindMean:
def test_sfcWind_mean(self, sfcWind_series):
def test_sfcWind_mean(self, sfcWind_series): # noqa: N802
sfcWind = sfcWind_series(np.array([14.11, 15.27, 10.70]))
out = xci.sfcWind_mean(sfcWind)
np.testing.assert_allclose(out, [13.36])


class TestSfcWindMin:
def test_sfcWind_min(self, sfcWind_series):
def test_sfcWind_min(self, sfcWind_series): # noqa: N802
sfcWind = sfcWind_series(np.array([14.11, 15.27, 10.70]))
out = xci.sfcWind_min(sfcWind)
np.testing.assert_allclose(out, [10.70])


class TestSfcWindmaxMax:
def test_sfcWindmax_max(self, sfcWindmax_series):
def test_sfcWindmax_max(self, sfcWindmax_series): # noqa: N802
sfcWindmax = sfcWindmax_series(np.array([14.11, 15.27, 10.70]))
out = xci.sfcWindmax_max(sfcWindmax)
np.testing.assert_allclose(out, [15.27])


class TestSfcWindmaxMean:
def test_sfcWindmax_mean(self, sfcWindmax_series):
def test_sfcWindmax_mean(self, sfcWindmax_series): # noqa: N802
sfcWindmax = sfcWindmax_series(np.array([14.11, 15.27, 10.70]))
out = xci.sfcWindmax_mean(sfcWindmax)
np.testing.assert_allclose(out, [13.36])


class TestSfcWindmaxMin:
def test_sfcWindmax_min(self, sfcWindmax_series):
def test_sfcWindmax_min(self, sfcWindmax_series): # noqa: N802
sfcWindmax = sfcWindmax_series(np.array([14.11, 15.27, 10.70]))
out = xci.sfcWindmax_min(sfcWindmax)
np.testing.assert_allclose(out, [10.70])
Expand Down
14 changes: 10 additions & 4 deletions tests/test_sdba/test_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def test_time(self, kind, name, series, random):
np.testing.assert_array_almost_equal(p, ref)

@pytest.mark.parametrize("kind,name", [(ADDITIVE, "tas"), (MULTIPLICATIVE, "pr")])
def test_mon_U(self, mon_series, series, mon_triangular, kind, name, random):
def test_mon_U( # noqa: N802
self, mon_series, series, mon_triangular, kind, name, random
):
n = 10000
u = random.random(n)

Expand Down Expand Up @@ -209,7 +211,9 @@ def test_quantiles(self, series, kind, name, random):

@pytest.mark.parametrize("kind,name", [(ADDITIVE, "tas"), (MULTIPLICATIVE, "pr")])
@pytest.mark.parametrize("add_dims", [True, False])
def test_mon_U(self, mon_series, series, kind, name, add_dims, random):
def test_mon_U( # noqa: N802
self, mon_series, series, kind, name, add_dims, random
):
"""
Train on
hist: U
Expand Down Expand Up @@ -324,7 +328,7 @@ def test_quantiles(self, series, kind, name, random):
@pytest.mark.parametrize("use_dask", [True, False])
@pytest.mark.parametrize("kind,name", [(ADDITIVE, "tas"), (MULTIPLICATIVE, "pr")])
@pytest.mark.parametrize("add_dims", [True, False])
def test_mon_U(
def test_mon_U( # noqa: N802
self, mon_series, series, mon_triangular, add_dims, kind, name, use_dask, random
):
"""
Expand Down Expand Up @@ -480,7 +484,9 @@ def test_quantiles(self, series, kind, name, random):
np.testing.assert_array_almost_equal(p[middle], ref[middle], 1)

@pytest.mark.parametrize("kind,name", [(ADDITIVE, "tas"), (MULTIPLICATIVE, "pr")])
def test_mon_U(self, mon_series, series, mon_triangular, kind, name, random):
def test_mon_U( # noqa: N802
self, mon_series, series, mon_triangular, kind, name, random
):
"""
Train on
hist: U
Expand Down
8 changes: 4 additions & 4 deletions tests/test_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class TestDTR:
nc_tasmax = os.path.join("NRCANdaily", "nrcan_canada_daily_tasmax_1990.nc")
nc_tasmin = os.path.join("NRCANdaily", "nrcan_canada_daily_tasmin_1990.nc")

def test_DTR_3d_data_with_nans(self, open_dataset):
def test_DTR_3d_data_with_nans(self, open_dataset): # noqa: N802
tasmax = open_dataset(self.nc_tasmax).tasmax
tasmax_C = open_dataset(self.nc_tasmax).tasmax
tasmax_C -= K2C
Expand Down Expand Up @@ -176,7 +176,7 @@ class TestTmean:
os.path.join("NRCANdaily", "nrcan_canada_daily_tasmin_1990.nc"),
)

def test_Tmean_3d_data(self, open_dataset):
def test_Tmean_3d_data(self, open_dataset): # noqa: N802
ds_tmax = open_dataset(self.nc_files[0])
ds_tmin = open_dataset(self.nc_files[1])
tas = atmos.tg(ds_tmin.tasmin, ds_tmax.tasmax)
Expand Down Expand Up @@ -205,7 +205,7 @@ def test_Tmean_3d_data(self, open_dataset):
class TestTx:
nc_file = os.path.join("NRCANdaily", "nrcan_canada_daily_tasmax_1990.nc")

def test_TX_3d_data(self, open_dataset):
def test_TX_3d_data(self, open_dataset): # noqa: N802
tasmax = open_dataset(self.nc_file).tasmax
tasmax_C = open_dataset(self.nc_file).tasmax
tasmax_C.values -= K2C
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_TX_3d_data(self, open_dataset):
class TestTn:
nc_file = os.path.join("NRCANdaily", "nrcan_canada_daily_tasmin_1990.nc")

def test_TN_3d_data(self, open_dataset):
def test_TN_3d_data(self, open_dataset): # noqa: N802
tasmin = open_dataset(self.nc_file).tasmin
tasmin_C = open_dataset(self.nc_file).tasmin
tasmin_C.values -= K2C
Expand Down
12 changes: 6 additions & 6 deletions tests/test_wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_calm_windy_days(self, open_dataset):


class TestSfcWindMax:
def test_sfcWind_max(self, open_dataset):
def test_sfcWind_max(self, open_dataset): # noqa: N802
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
sfcWind, _ = atmos.wind_speed_from_vector(ds.uas, ds.vas)

Expand All @@ -29,7 +29,7 @@ def test_sfcWind_max(self, open_dataset):


class TestSfcWindMean:
def test_sfcWind_mean(self, open_dataset):
def test_sfcWind_mean(self, open_dataset): # noqa: N802
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
sfcWind, _ = atmos.wind_speed_from_vector(ds.uas, ds.vas)

Expand All @@ -39,7 +39,7 @@ def test_sfcWind_mean(self, open_dataset):


class TestSfcWindMin:
def test_sfcWind_min(self, open_dataset):
def test_sfcWind_min(self, open_dataset): # noqa: N802
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
sfcWind, _ = atmos.wind_speed_from_vector(ds.uas, ds.vas)

Expand All @@ -49,7 +49,7 @@ def test_sfcWind_min(self, open_dataset):


class TestSfcWindmaxMax:
def test_sfcWindmax_max(self, open_dataset):
def test_sfcWindmax_max(self, open_dataset): # noqa: N802
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
sfcWindmax, _ = atmos.wind_speed_from_vector(ds.uas, ds.vas)

Expand All @@ -59,7 +59,7 @@ def test_sfcWindmax_max(self, open_dataset):


class TestSfcWindmaxMean:
def test_sfcWindmax_mean(self, open_dataset):
def test_sfcWindmax_mean(self, open_dataset): # noqa: N802
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
sfcWindmax, _ = atmos.wind_speed_from_vector(ds.uas, ds.vas)

Expand All @@ -69,7 +69,7 @@ def test_sfcWindmax_mean(self, open_dataset):


class TestSfcWindmaxMin:
def test_sfcWindmax_mean(self, open_dataset):
def test_sfcWindmax_mean(self, open_dataset): # noqa: N802
ds = open_dataset("ERA5/daily_surface_cancities_1990-1993.nc")
sfcWindmax, _ = atmos.wind_speed_from_vector(ds.uas, ds.vas)

Expand Down
24 changes: 18 additions & 6 deletions xclim/indices/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,9 @@ def snow_depth(


@declare_units(sfcWind="[speed]")
def sfcWind_max(sfcWind: xarray.DataArray, freq: str = "YS") -> xarray.DataArray:
def sfcWind_max( # noqa: N802
sfcWind: xarray.DataArray, freq: str = "YS"
) -> xarray.DataArray:
r"""Highest daily mean wind speed.
The maximum of daily mean wind speed.
Expand Down Expand Up @@ -574,7 +576,9 @@ def sfcWind_max(sfcWind: xarray.DataArray, freq: str = "YS") -> xarray.DataArray


@declare_units(sfcWind="[speed]")
def sfcWind_mean(sfcWind: xarray.DataArray, freq: str = "YS") -> xarray.DataArray:
def sfcWind_mean( # noqa: N802
sfcWind: xarray.DataArray, freq: str = "YS"
) -> xarray.DataArray:
r"""Mean of daily mean wind speed.
Resample the original daily mean wind speed series by taking the mean over each period.
Expand Down Expand Up @@ -615,7 +619,9 @@ def sfcWind_mean(sfcWind: xarray.DataArray, freq: str = "YS") -> xarray.DataArra


@declare_units(sfcWind="[speed]")
def sfcWind_min(sfcWind: xarray.DataArray, freq: str = "YS") -> xarray.DataArray:
def sfcWind_min( # noqa: N802
sfcWind: xarray.DataArray, freq: str = "YS"
) -> xarray.DataArray:
r"""Lowest daily mean wind speed.
The minimum of daily mean wind speed.
Expand Down Expand Up @@ -654,7 +660,9 @@ def sfcWind_min(sfcWind: xarray.DataArray, freq: str = "YS") -> xarray.DataArray


@declare_units(sfcWindmax="[speed]")
def sfcWindmax_max(sfcWindmax: xarray.DataArray, freq: str = "YS") -> xarray.DataArray:
def sfcWindmax_max( # noqa: N802
sfcWindmax: xarray.DataArray, freq: str = "YS"
) -> xarray.DataArray:
r"""Highest maximum wind speed.
The maximum of daily maximum wind speed.
Expand Down Expand Up @@ -696,7 +704,9 @@ def sfcWindmax_max(sfcWindmax: xarray.DataArray, freq: str = "YS") -> xarray.Dat


@declare_units(sfcWindmax="[speed]")
def sfcWindmax_mean(sfcWindmax: xarray.DataArray, freq: str = "YS") -> xarray.DataArray:
def sfcWindmax_mean( # noqa: N802
sfcWindmax: xarray.DataArray, freq: str = "YS"
) -> xarray.DataArray:
r"""Mean of daily maximum wind speed.
Resample the original daily maximum wind speed series by taking the mean over each period.
Expand Down Expand Up @@ -738,7 +748,9 @@ def sfcWindmax_mean(sfcWindmax: xarray.DataArray, freq: str = "YS") -> xarray.Da


@declare_units(sfcWindmax="[speed]")
def sfcWindmax_min(sfcWindmax: xarray.DataArray, freq: str = "YS") -> xarray.DataArray:
def sfcWindmax_min( # noqa: N802
sfcWindmax: xarray.DataArray, freq: str = "YS"
) -> xarray.DataArray:
r"""Lowest daily maximum wind speed.
The minimum of daily maximum wind speed.
Expand Down
2 changes: 1 addition & 1 deletion xclim/indices/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ def _fit_start(x, dist: str, **fitkwargs: Any) -> tuple[tuple, dict]:
return (), {}


def _dist_method_1D(
def _dist_method_1D( # noqa: N802
*args, dist: str | scipy.stats.rv_continuous, function: str, **kwargs: Any
) -> xr.DataArray:
r"""Statistical function for given argument on given distribution initialized with params.
Expand Down
2 changes: 1 addition & 1 deletion xclim/sdba/adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ def _parse(s):
]
)

def _generate_SBCK_classes():
def _generate_SBCK_classes(): # noqa: N802
classes = []
for clsname in dir(SBCK):
cls = getattr(SBCK, clsname)
Expand Down
2 changes: 1 addition & 1 deletion xclim/sdba/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def add_cyclic_bounds(
return ensure_chunk_size(qmf, **{att: -1})


def _interp_on_quantiles_1D(newx, oldx, oldy, method, extrap):
def _interp_on_quantiles_1D(newx, oldx, oldy, method, extrap): # noqa: N802
mask_new = np.isnan(newx)
mask_old = np.isnan(oldy) | np.isnan(oldx)
out = np.full_like(newx, np.NaN, dtype=f"float{oldy.dtype.itemsize * 8}")
Expand Down

0 comments on commit 4b83974

Please sign in to comment.