From 4b83974fffb2571aa7ea5e10d52bda9a4e508952 Mon Sep 17 00:00:00 2001 From: Zeitsperre <10819524+Zeitsperre@users.noreply.github.com> Date: Wed, 24 Apr 2024 15:55:05 -0400 Subject: [PATCH] enable testing for PEP8 rule N802 --- pyproject.toml | 1 + tests/test_analog.py | 4 ++-- tests/test_cffwis.py | 2 +- tests/test_indicators.py | 18 ++++++++++-------- tests/test_indices.py | 12 ++++++------ tests/test_sdba/test_adjustment.py | 14 ++++++++++---- tests/test_temperature.py | 8 ++++---- tests/test_wind.py | 12 ++++++------ xclim/indices/_simple.py | 24 ++++++++++++++++++------ xclim/indices/stats.py | 2 +- xclim/sdba/adjustment.py | 2 +- xclim/sdba/utils.py | 2 +- 12 files changed, 61 insertions(+), 40 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f66d665c4..902176b65 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -270,6 +270,7 @@ select = [ "D", "E", "F", + "N802", "W" ] diff --git a/tests/test_analog.py b/tests/test_analog.py index 6037d0b6a..a1524add6 100644 --- a/tests/test_analog.py +++ b/tests/test_analog.py @@ -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) @@ -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 diff --git a/tests/test_cffwis.py b/tests/test_cffwis.py index d17ae4b8e..d6785c605 100644 --- a/tests/test_cffwis.py +++ b/tests/test_cffwis.py @@ -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]) diff --git a/tests/test_indicators.py b/tests/test_indicators.py index 490b69941..69f8623bc 100644 --- a/tests/test_indicators.py +++ b/tests/test_indicators.py @@ -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): @@ -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): @@ -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"], @@ -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") @@ -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", diff --git a/tests/test_indices.py b/tests/test_indices.py index 9fb3896cf..974e5d0e7 100644 --- a/tests/test_indices.py +++ b/tests/test_indices.py @@ -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]) diff --git a/tests/test_sdba/test_adjustment.py b/tests/test_sdba/test_adjustment.py index 3cdc86ff9..653a9a953 100644 --- a/tests/test_sdba/test_adjustment.py +++ b/tests/test_sdba/test_adjustment.py @@ -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) @@ -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 @@ -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 ): """ @@ -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 diff --git a/tests/test_temperature.py b/tests/test_temperature.py index 5b069fd34..5d2b299e7 100644 --- a/tests/test_temperature.py +++ b/tests/test_temperature.py @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/tests/test_wind.py b/tests/test_wind.py index 5e352be13..f3427bedf 100644 --- a/tests/test_wind.py +++ b/tests/test_wind.py @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/xclim/indices/_simple.py b/xclim/indices/_simple.py index e59db0f22..c97d714a4 100644 --- a/xclim/indices/_simple.py +++ b/xclim/indices/_simple.py @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/xclim/indices/stats.py b/xclim/indices/stats.py index 9b72e34e6..598932f11 100644 --- a/xclim/indices/stats.py +++ b/xclim/indices/stats.py @@ -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. diff --git a/xclim/sdba/adjustment.py b/xclim/sdba/adjustment.py index 956b6a5b6..bee16451c 100644 --- a/xclim/sdba/adjustment.py +++ b/xclim/sdba/adjustment.py @@ -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) diff --git a/xclim/sdba/utils.py b/xclim/sdba/utils.py index 485c9a1d2..2815f5f42 100644 --- a/xclim/sdba/utils.py +++ b/xclim/sdba/utils.py @@ -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}")