Skip to content

Commit

Permalink
more pylint adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Aug 15, 2024
1 parent f141072 commit 9397d1a
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .pylintrc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
# run arbitrary code.
# extension-pkg-allow-list =
extension-pkg-allow-list = ["cftime"]

# A comma-separated list of package or module names from where C extensions may
# be loaded. Extensions are loading into the active Python interpreter and may
Expand All @@ -36,7 +36,7 @@ fail-under = 10
# from-stdin =

# Files or directories to be skipped. They should be base names, not paths.
ignore = ["CVS"]
ignore = ["CVS", "conftest.py"]

# Add files or directories matching the regular expressions patterns to the
# ignore-list. The regex matches against paths and can be in Posix or Windows
Expand Down
2 changes: 1 addition & 1 deletion xclim/analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def pivot(x, y):

return np.max(np.abs(cx - cy))

return max(pivot(x, y), pivot(y, x))
return max(pivot(x, y), pivot(y, x)) # pylint: disable=arguments-out-of-order


@metric
Expand Down
5 changes: 3 additions & 2 deletions xclim/core/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,8 @@ def stack_periods(
If ``stride`` is a divisor of ``window``, the correct timeseries can be reconstructed with :py:func:`unstack_periods`.
The coordinate of `period` is the first timestep of each window.
"""
from xclim.core.units import ( # Import in function to avoid cyclical imports
# Import in function to avoid cyclical imports
from xclim.core.units import ( # pylint: disable=import-outside-toplevel
ensure_cf_units,
infer_sampling_units,
)
Expand Down Expand Up @@ -1575,7 +1576,7 @@ def unstack_periods(da: xr.DataArray | xr.Dataset, dim: str = "period"):
0 o o o x x
=== === === === === === === ===
"""
from xclim.core.units import infer_sampling_units
from xclim.core.units import infer_sampling_units # pylint: disable=import-outside-toplevel

try:
starts = da[dim]
Expand Down
2 changes: 1 addition & 1 deletion xclim/core/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ def build_indicator_module(
ModuleType
A indicator module built from a mapping of Indicators.
"""
from xclim import indicators
from xclim import indicators # pylint: disable=import-outside-toplevel

out: ModuleType
if hasattr(indicators, name):
Expand Down
20 changes: 9 additions & 11 deletions xclim/core/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,15 @@ def _valid_locales(locales):
if isinstance(locales, str):
return True
return all(
[
# A locale is valid if it is a string from the list
(isinstance(locale, str) and locale in _LOCALES)
or (
# Or if it is a tuple of a string and either a file or a dict.
not isinstance(locale, str)
and isinstance(locale[0], str)
and (isinstance(locale[1], dict) or Path(locale[1]).is_file())
)
for locale in locales
]
# A locale is valid if it is a string from the list
(isinstance(locale, str) and locale in _LOCALES)
or (
# Or if it is a tuple of a string and either a file or a dict.
not isinstance(locale, str)
and isinstance(locale[0], str)
and (isinstance(locale[1], dict) or Path(locale[1]).is_file())
)
for locale in locales
)


Expand Down
10 changes: 3 additions & 7 deletions xclim/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,8 @@ def convert_units_to( # noqa: C901
for direction, sign in [("to", 1), ("from", -1)]:
# If the dimensionality diff is compatible with this conversion
compatible = all(
[
dimdiff == (sign * dim_order_diff.get(f"[{dim}]"))
for dim, dimdiff in convconf["dimensionality"].items()
]
dimdiff == sign * dim_order_diff.get(f"[{dim}]")
for dim, dimdiff in convconf["dimensionality"].items()
)
# Does the input cf standard name have an equivalent after conversion
valid = cf_conversion(standard_name, convname, direction)
Expand Down Expand Up @@ -372,9 +370,7 @@ def convert_units_to( # noqa: C901
raise NotImplementedError(f"Source of type `{type(source)}` is not supported.")


def cf_conversion(
standard_name: str, conversion: str, direction: Literal["to", "from"]
) -> str | None:
def cf_conversion(standard_name: str, conversion: str, direction: str) -> str | None:
"""Get the standard name of the specific conversion for the given standard name.
Parameters
Expand Down
1 change: 1 addition & 0 deletions xclim/indicators/land/_streamflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Streamflow(ResamplingIndicator):
src_freq = "D"
keywords = "streamflow hydrology"

# TODO: TJS: The signature of this method seems wrong. Should it be `def cfcheck(cls, q):` or something else? Is it a static method?
@staticmethod
def cfcheck(q):
check_valid(q, "standard_name", "water_volume_transport_in_river_channel")
Expand Down
4 changes: 2 additions & 2 deletions xclim/indices/_agro.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,12 @@ def dryness_index(
adjustment_array_north = xarray.DataArray(
[0, 0, 0, 0.1, 0.3, 0.5, 0.5, 0.5, 0.5, 0, 0, 0],
dims="month",
coords=dict(month=np.arange(1, 13)),
coords={"month": np.arange(1, 13)},
)
adjustment_array_south = xarray.DataArray(
[0.5, 0.5, 0.5, 0, 0, 0, 0, 0, 0, 0.1, 0.3, 0.5],
dims="month",
coords=dict(month=np.arange(1, 13)),
coords={"month": np.arange(1, 13)},
)

has_north, has_south = False, False
Expand Down
9 changes: 2 additions & 7 deletions xclim/indices/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np
import xarray as xr
from numba import float32, float64, vectorize # noqa
from numba import vectorize

from xclim.core.units import (
amount2rate,
Expand Down Expand Up @@ -1584,12 +1584,7 @@ def potential_evapotranspiration(
return out


@vectorize(
# [
# float64(float64, float64, float64, float64),
# float32(float32, float32, float32, float32),
# ],
)
@vectorize
def _utci(tas, sfcWind, dt, wvp):
"""Return the empirical polynomial function for UTCI. See :py:func:`universal_thermal_climate_index`."""
# Taken directly from the original Fortran code by Peter Bröde.
Expand Down
26 changes: 13 additions & 13 deletions xclim/indices/fire/_cffwis.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,9 @@ def _drought_code( # pragma: no cover
"""
fl = _day_length_factor(lat, mth) # type: ignore

if t < -2.8:
t = -2.8 # type: ignore
t = max(t, -2.8) # type: ignore
pe = (0.36 * (t + 2.8) + fl) / 2 # *Eq.22*#
pe = max(pe, 0.0)
pe = max(pe, 0.0) # type: ignore

if p > 2.8:
ra = p
Expand Down Expand Up @@ -1074,7 +1073,7 @@ def fire_weather_ufunc( # noqa: C901

# Verification of all arguments
for i, (arg, name, usedby, has_time_dim) in enumerate(needed_args):
if any([ind in indexes + [season_method] for ind in usedby]):
if any(ind in indexes + [season_method] for ind in usedby):
if arg is None:
raise TypeError(
f"Missing input argument {name} for index combination {indexes} "
Expand Down Expand Up @@ -1172,7 +1171,8 @@ def fire_weather_ufunc( # noqa: C901

if len(outputs) == 1:
return {outputs[0]: das}
return {name: da for name, da in zip(outputs, das)}

return dict(zip(outputs, das))


@declare_units(last_dc="[]", winter_pr="[length]")
Expand Down Expand Up @@ -1644,14 +1644,14 @@ def fire_season(
):
raise ValueError("Thresholds must be scalar.")

kwargs = dict(
method=method,
temp_start_thresh=convert_units_to(temp_start_thresh, "degC"),
temp_end_thresh=convert_units_to(temp_end_thresh, "degC"),
temp_condition_days=temp_condition_days,
snow_condition_days=snow_condition_days,
snow_thresh=convert_units_to(snow_thresh, "m"),
)
kwargs = {
"method": method,
"temp_start_thresh": convert_units_to(temp_start_thresh, "degC"),
"temp_end_thresh": convert_units_to(temp_end_thresh, "degC"),
"temp_condition_days": temp_condition_days,
"snow_condition_days": snow_condition_days,
"snow_thresh": convert_units_to(snow_thresh, "m"),
}

def _apply_fire_season(ds, **kwargs):
season_mask = ds.tas.copy(
Expand Down
3 changes: 1 addition & 2 deletions xclim/indices/fire/_ffdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ def _griffiths_drought_factor(p, smd, lim, df): # pragma: no cover
dflim = 9.0
else:
dflim = 10.0
if dfw > dflim:
dfw = dflim
dfw = min(dfw, dflim)

dfw = min(dfw, 10.0)

Expand Down
3 changes: 1 addition & 2 deletions xclim/indices/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ def select_resample_op(
"""
da = select_time(da, **indexer)
r = da.resample(time=freq)
if op in _xclim_ops:
op = _xclim_ops[op]
if isinstance(op, str):
op = _xclim_ops.get(op, op)
out = getattr(r, op.replace("integral", "sum"))(dim="time", keep_attrs=True)
else:
with xr.set_options(keep_attrs=True):
Expand Down
12 changes: 8 additions & 4 deletions xclim/indices/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ def time_correction_for_solar_angle(time: xr.DataArray) -> xr.DataArray:
return _wrap_radians(convert_units_to(tc, "rad"))


def eccentricity_correction_factor(time: xr.DataArray, method="spencer"):
def eccentricity_correction_factor(
time: xr.DataArray, method: str = "spencer"
) -> xr.DataArray:
"""Eccentricity correction factor of the Earth's orbit.
The squared ratio of the mean distance Earth-Sun to the distance at a specific moment.
Expand All @@ -163,9 +165,10 @@ def eccentricity_correction_factor(time: xr.DataArray, method="spencer"):
----------
time: xr.DataArray
Time coordinate
method : str
Which approximation to use. The default ("spencer") uses the first five terms of the fourier series of the
eccentricity, while "simple" approximates with only the first two.
method : {'spencer', 'simple'}
Which approximation to use.
The default ("spencer") uses the first five terms of the fourier series of the eccentricity.
The "simple" method approximates with only the first two.
Returns
-------
Expand All @@ -191,6 +194,7 @@ def eccentricity_correction_factor(time: xr.DataArray, method="spencer"):
+ 0.000719 * np.cos(2 * da)
+ 0.000077 * np.sin(2 * da)
)
raise NotImplementedError("Method must be one of 'simple' or 'spencer'.")


def cosine_of_solar_zenith_angle(
Expand Down
10 changes: 5 additions & 5 deletions xclim/indices/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ def fit(
dask="parallelized",
output_dtypes=[float],
keep_attrs=True,
kwargs=dict(
kwargs={
# Don't know how APP should be included, this works for now
dist=dist,
nparams=len(dist_params),
method=method,
"dist": dist,
"nparams": len(dist_params),
"method": method,
**fitkwargs,
),
},
dask_gufunc_kwargs={"output_sizes": {"dparams": len(dist_params)}},
)

Expand Down
4 changes: 3 additions & 1 deletion xclim/sdba/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ def group(
They are broadcast, merged to the grouping dataset and regrouped in the output.
"""
if das:
from .utils import broadcast # pylint: disable=cyclic-import
from .utils import ( # pylint: disable=cyclic-import,import-outside-toplevel
broadcast,
)

if da is not None:
das[da.name] = da
Expand Down
8 changes: 5 additions & 3 deletions xclim/sdba/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def _postprocess(self, outs, das, params):

def get_measure(self):
"""Get the statistical measure indicator that is best used with this statistical property."""
from xclim.core.indicator import registry
from xclim.core.indicator import ( # pylint: disable=import-outside-toplevel
registry,
)

return registry[self.measure].get_instance()

Expand Down Expand Up @@ -354,7 +356,7 @@ def _spell_stats(
stat_resample,
):
# PB: This prevents an import error in the distributed dask scheduler, but I don't know why.
import xarray.core.resample_cftime # noqa: F401, pylint: disable=unused-import
import xarray.core.resample_cftime # noqa: F401, pylint: disable=unused-import,import-outside-toplevel

da = ds.data
mask = ~(da.isel({dim: 0}).isnull()).drop_vars(
Expand Down Expand Up @@ -906,7 +908,7 @@ def _bivariate_spell_stats(
stat_resample,
):
# PB: This prevents an import error in the distributed dask scheduler, but I don't know why.
import xarray.core.resample_cftime # noqa: F401, pylint: disable=unused-import
import xarray.core.resample_cftime # noqa: F401, pylint: disable=unused-import,import-outside-toplevel

conds = []
masks = []
Expand Down
10 changes: 5 additions & 5 deletions xclim/sdba/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,12 +1027,12 @@ def optimal_transport(gridX, gridY, muX, muY, numItermax, transform):
:cite:cts:`sdba-robin_2021`
"""
try:
from ot import emd # noqa
except ImportError:
from ot import emd # pylint: disable=import-outside-toplevel
except ImportError as e:
raise ImportError(
"The optional dependency `ot` is required for optimal_transport. "
"You can install it with `pip install pot` or `pip install 'xclim[extras]'`."
)
"The optional dependency `POT` is required for optimal_transport. "
"You can install it with `pip install POT` or `pip install 'xclim[extras]'`."
) from e

if transform == "standardize":
gridX = (gridX - gridX.mean()) / gridX.std()
Expand Down

0 comments on commit 9397d1a

Please sign in to comment.