Skip to content

Commit fad2773

Browse files
Apply ruff/flake8-simplify rules (SIM) (#9727)
* Apply ruff/flake8-simplify rule SIM101 SIM101 Multiple `isinstance` calls for expression, merge into a single call * Apply ruff/flake8-simplify rule SIM110 SIM110 Use `return any()` instead of `for` loop * Apply ruff/flake8-simplify rule SIM910 SIM910 Use `.get(...)` instead of `.get(..., None)` --------- Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
1 parent 3a4ff9b commit fad2773

File tree

11 files changed

+20
-31
lines changed

11 files changed

+20
-31
lines changed

xarray/backends/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _find_absolute_paths(
104104
['common.py']
105105
"""
106106
if isinstance(paths, str):
107-
if is_remote_uri(paths) and kwargs.get("engine", None) == "zarr":
107+
if is_remote_uri(paths) and kwargs.get("engine") == "zarr":
108108
try:
109109
from fsspec.core import get_fs_token_paths
110110
except ImportError as e:

xarray/backends/plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def sort_backends(
9393
) -> dict[str, type[BackendEntrypoint]]:
9494
ordered_backends_entrypoints = {}
9595
for be_name in STANDARD_BACKENDS_ORDER:
96-
if backend_entrypoints.get(be_name, None) is not None:
96+
if backend_entrypoints.get(be_name) is not None:
9797
ordered_backends_entrypoints[be_name] = backend_entrypoints.pop(be_name)
9898
ordered_backends_entrypoints.update(
9999
{name: backend_entrypoints[name] for name in sorted(backend_entrypoints)}

xarray/core/coordinates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ def create_coords_with_default_indexes(
11081108

11091109
# extract and merge coordinates and indexes from input DataArrays
11101110
if dataarray_coords:
1111-
prioritized = {k: (v, indexes.get(k, None)) for k, v in variables.items()}
1111+
prioritized = {k: (v, indexes.get(k)) for k, v in variables.items()}
11121112
variables, indexes = merge_coordinates_without_align(
11131113
dataarray_coords + [new_coords],
11141114
prioritized=prioritized,

xarray/core/dataset.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,7 @@ def _setitem_check(self, key, value):
17271727
new_value[name] = duck_array_ops.astype(val, dtype=var_k.dtype, copy=False)
17281728

17291729
# check consistency of dimension sizes and dimension coordinates
1730-
if isinstance(value, DataArray) or isinstance(value, Dataset):
1730+
if isinstance(value, DataArray | Dataset):
17311731
align(self[key], value, join="exact", copy=False)
17321732

17331733
return new_value
@@ -7000,7 +7000,7 @@ def reduce(
70007000
math_scores (student) float64 24B 91.0 82.5 96.5
70017001
english_scores (student) float64 24B 91.0 80.5 94.5
70027002
"""
7003-
if kwargs.get("axis", None) is not None:
7003+
if kwargs.get("axis") is not None:
70047004
raise ValueError(
70057005
"passing 'axis' to Dataset reduce methods is ambiguous."
70067006
" Please use 'dim' instead."
@@ -10034,11 +10034,7 @@ def curvefit(
1003410034
else:
1003510035
reduce_dims_ = list(reduce_dims)
1003610036

10037-
if (
10038-
isinstance(coords, str)
10039-
or isinstance(coords, DataArray)
10040-
or not isinstance(coords, Iterable)
10041-
):
10037+
if isinstance(coords, str | DataArray) or not isinstance(coords, Iterable):
1004210038
coords = [coords]
1004310039
coords_: Sequence[DataArray] = [
1004410040
self[coord] if isinstance(coord, str) else coord for coord in coords

xarray/core/missing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def _get_interpolator(
485485
# take higher dimensional data but scipy.interp1d can.
486486
if (
487487
method == "linear"
488-
and not kwargs.get("fill_value", None) == "extrapolate"
488+
and not kwargs.get("fill_value") == "extrapolate"
489489
and not vectorizeable_only
490490
):
491491
kwargs.update(method=method)

xarray/core/nputils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def __setitem__(self, key, value):
176176

177177
def _create_method(name, npmodule=np) -> Callable:
178178
def f(values, axis=None, **kwargs):
179-
dtype = kwargs.get("dtype", None)
179+
dtype = kwargs.get("dtype")
180180
bn_func = getattr(bn, name, None)
181181

182182
if (

xarray/core/utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ def equivalent(first: T, second: T) -> bool:
187187
def list_equiv(first: Sequence[T], second: Sequence[T]) -> bool:
188188
if len(first) != len(second):
189189
return False
190-
for f, s in zip(first, second, strict=True):
191-
if not equivalent(f, s):
192-
return False
193-
return True
190+
return all(equivalent(f, s) for f, s in zip(first, second, strict=True))
194191

195192

196193
def peek_at(iterable: Iterable[T]) -> tuple[T, Iterator[T]]:
@@ -1073,8 +1070,7 @@ def contains_only_chunked_or_numpy(obj) -> bool:
10731070

10741071
return all(
10751072
[
1076-
isinstance(var._data, ExplicitlyIndexed)
1077-
or isinstance(var._data, np.ndarray)
1073+
isinstance(var._data, ExplicitlyIndexed | np.ndarray)
10781074
or is_chunked_array(var._data)
10791075
for var in obj._variables.values()
10801076
]

xarray/plot/dataarray_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ def newplotfunc(
10301030
cbar_kwargs["label"] = label_from_attrs(hueplt_norm.data)
10311031

10321032
_add_colorbar(
1033-
primitive, ax, kwargs.get("cbar_ax", None), cbar_kwargs, cmap_params
1033+
primitive, ax, kwargs.get("cbar_ax"), cbar_kwargs, cmap_params
10341034
)
10351035

10361036
if add_legend_:

xarray/plot/facetgrid.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def map_dataarray(
337337
338338
"""
339339

340-
if kwargs.get("cbar_ax", None) is not None:
340+
if kwargs.get("cbar_ax") is not None:
341341
raise ValueError("cbar_ax not supported by FacetGrid.")
342342

343343
cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
@@ -363,7 +363,7 @@ def map_dataarray(
363363
x=x,
364364
y=y,
365365
imshow=func.__name__ == "imshow",
366-
rgb=kwargs.get("rgb", None),
366+
rgb=kwargs.get("rgb"),
367367
)
368368

369369
for d, ax in zip(self.name_dicts.flat, self.axs.flat, strict=True):
@@ -421,7 +421,7 @@ def map_plot1d(
421421
# not sure how much that is used outside these tests.
422422
self.data = self.data.copy()
423423

424-
if kwargs.get("cbar_ax", None) is not None:
424+
if kwargs.get("cbar_ax") is not None:
425425
raise ValueError("cbar_ax not supported by FacetGrid.")
426426

427427
if func.__name__ == "scatter":
@@ -537,8 +537,8 @@ def map_plot1d(
537537
add_colorbar, add_legend = _determine_guide(
538538
hueplt_norm,
539539
sizeplt_norm,
540-
kwargs.get("add_colorbar", None),
541-
kwargs.get("add_legend", None),
540+
kwargs.get("add_colorbar"),
541+
kwargs.get("add_legend"),
542542
# kwargs.get("add_guide", None),
543543
# kwargs.get("hue_style", None),
544544
)
@@ -622,7 +622,7 @@ def map_dataset(
622622

623623
kwargs["add_guide"] = False
624624

625-
if kwargs.get("markersize", None):
625+
if kwargs.get("markersize"):
626626
kwargs["size_mapping"] = _parse_size(
627627
self.data[kwargs["markersize"]], kwargs.pop("size_norm", None)
628628
)

xarray/plot/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ def _process_cmap_cbar_kwargs(
917917
# Leave user to specify cmap settings for surface plots
918918
kwargs["cmap"] = cmap
919919
return {
920-
k: kwargs.get(k, None)
920+
k: kwargs.get(k)
921921
for k in ["vmin", "vmax", "cmap", "extend", "levels", "norm"]
922922
}, {}
923923

@@ -1828,7 +1828,7 @@ def _guess_coords_to_plot(
18281828
default_guess, available_coords, ignore_guess_kwargs, strict=False
18291829
):
18301830
if coords_to_plot.get(k, None) is None and all(
1831-
kwargs.get(ign_kw, None) is None for ign_kw in ign_kws
1831+
kwargs.get(ign_kw) is None for ign_kw in ign_kws
18321832
):
18331833
coords_to_plot[k] = dim
18341834

0 commit comments

Comments
 (0)