Skip to content

Commit 7ea28b1

Browse files
Apply assorted ruff preview rules (#10465)
* Apply ruff/flake8-bugbear preview rule B909 B909 Mutation to loop iterable during iteration * Apply ruff/flake8-pyi preview rule PYI059 PYI059 `Generic[]` should always be the last base class * Apply ruff/Pylint preview rule PLC0207 PLC0207 Accessing only the first or last element of `str.split()` without setting `maxsplit=1` * Apply ruff/Pylint preview rule PLW0108 PLW0108 Lambda may be unnecessary; consider inlining inner function * Apply ruff/refurb preview rule FURB110 FURB110 Replace ternary `if` expression with `or` operator * Apply ruff/refurb preview rule FURB171 FURB171 Membership test against single-item container * Remove useless test The initial intent was probably `value.dtype.kind in "cf"`. In any case, this is a no-op because `value.dtype.kind == "O"`. So remove the test altogether. * Apply ruff preview rule RUF036 RUF036 `None` not at the end of the type annotation.
1 parent 5d5bc5b commit 7ea28b1

27 files changed

+68
-67
lines changed

xarray/backends/chunks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def build_grid_chunks(
141141
if region is None:
142142
region = slice(0, size)
143143

144-
region_start = region.start if region.start else 0
144+
region_start = region.start or 0
145145
# Generate the zarr chunks inside the region of this dim
146146
chunks_on_region = [chunk_size - (region_start % chunk_size)]
147147
chunks_on_region.extend([chunk_size] * ((size - chunks_on_region[0]) // chunk_size))
@@ -224,7 +224,7 @@ def validate_grid_chunks_alignment(
224224
)
225225
)
226226

227-
interval_start = interval.start if interval.start else 0
227+
interval_start = interval.start or 0
228228

229229
if len(var_chunks) > 1:
230230
# The first border size is the amount of data that needs to be updated on the
@@ -247,7 +247,7 @@ def validate_grid_chunks_alignment(
247247
)
248248

249249
if not allow_partial_chunks:
250-
region_stop = interval.stop if interval.stop else size
250+
region_stop = interval.stop or size
251251

252252
error_on_last_chunk = base_error.format(
253253
var_chunk_pos=len(var_chunks) - 1,

xarray/backends/pydap_.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@ def get_variables(self):
158158
except AttributeError:
159159
from pydap.model import GroupType
160160

161-
_vars = list(self.ds.keys())
162-
# check the key is a BaseType or GridType
163-
for var in _vars:
164-
if isinstance(self.ds[var], GroupType):
165-
_vars.remove(var)
161+
_vars = [
162+
var
163+
for var in self.ds.keys()
164+
# check the key is not a BaseType or GridType
165+
if not isinstance(self.ds[var], GroupType)
166+
]
166167
return FrozenDict((k, self.open_store_variable(self.ds[k])) for k in _vars)
167168

168169
def get_attrs(self):

xarray/backends/zarr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ def set_variables(
11841184
# with chunk boundaries, then no synchronization is required."
11851185
# TODO: incorporate synchronizer to allow writes from multiple dask
11861186
# threads
1187-
shape = zarr_shape if zarr_shape else v.shape
1187+
shape = zarr_shape or v.shape
11881188
validate_grid_chunks_alignment(
11891189
nd_var_chunks=v.chunks,
11901190
enc_chunks=encoding["chunks"],
@@ -1545,7 +1545,7 @@ def guess_can_open(
15451545
) -> bool:
15461546
if isinstance(filename_or_obj, str | os.PathLike):
15471547
_, ext = os.path.splitext(filename_or_obj)
1548-
return ext in {".zarr"}
1548+
return ext == ".zarr"
15491549

15501550
return False
15511551

xarray/coding/times.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def build_pattern(
246246
]
247247
pattern_list = []
248248
for sep, name, sub_pattern in pieces:
249-
pattern_list.append((sep if sep else "") + named(name, sub_pattern))
249+
pattern_list.append((sep or "") + named(name, sub_pattern))
250250
# TODO: allow timezone offsets?
251251
return "^" + trailing_optional(pattern_list) + "$"
252252

xarray/computation/nanops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _nanmean_ddof_object(ddof, value, axis=None, dtype=None, **kwargs):
110110
# As dtype inference is impossible for object dtype, we assume float
111111
# https://github.com/dask/dask/issues/3162
112112
if dtype is None and value.dtype.kind == "O":
113-
dtype = value.dtype if value.dtype.kind in ["cf"] else float
113+
dtype = float
114114

115115
data = np.sum(value, axis=axis, dtype=dtype, **kwargs)
116116
data = data / (valid_count - ddof)

xarray/core/accessor_str.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def f(x, iind):
349349
islice = slice(-1, None) if iind == -1 else slice(iind, iind + 1)
350350
item = x[islice]
351351

352-
return item if item else default
352+
return item or default
353353

354354
return self._apply(func=f, func_args=(i,))
355355

xarray/core/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ def _full_like_variable(
17801780
other.shape,
17811781
fill_value,
17821782
dtype=dtype,
1783-
chunks=chunks if chunks else other.data.chunks,
1783+
chunks=chunks or other.data.chunks,
17841784
**from_array_kwargs,
17851785
)
17861786
else:

xarray/core/dataarray.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ def _replace(
498498
self,
499499
variable: Variable | None = None,
500500
coords=None,
501-
name: Hashable | None | Default = _default,
501+
name: Hashable | Default | None = _default,
502502
attrs=_default,
503503
indexes=None,
504504
) -> Self:
@@ -520,7 +520,7 @@ def _replace(
520520
def _replace_maybe_drop_dims(
521521
self,
522522
variable: Variable,
523-
name: Hashable | None | Default = _default,
523+
name: Hashable | Default | None = _default,
524524
) -> Self:
525525
if self.sizes == variable.sizes:
526526
coords = self._coords.copy()
@@ -581,7 +581,7 @@ def _to_temp_dataset(self) -> Dataset:
581581
return self._to_dataset_whole(name=_THIS_ARRAY, shallow_copy=False)
582582

583583
def _from_temp_dataset(
584-
self, dataset: Dataset, name: Hashable | None | Default = _default
584+
self, dataset: Dataset, name: Hashable | Default | None = _default
585585
) -> Self:
586586
variable = dataset._variables.pop(_THIS_ARRAY)
587587
coords = dataset._variables
@@ -2609,8 +2609,8 @@ def swap_dims(
26092609

26102610
def expand_dims(
26112611
self,
2612-
dim: None | Hashable | Sequence[Hashable] | Mapping[Any, Any] = None,
2613-
axis: None | int | Sequence[int] = None,
2612+
dim: Hashable | Sequence[Hashable] | Mapping[Any, Any] | None = None,
2613+
axis: int | Sequence[int] | None = None,
26142614
create_index_for_new_dim: bool = True,
26152615
**dim_kwargs: Any,
26162616
) -> Self:

xarray/core/dataset.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,9 +791,9 @@ def _replace(
791791
variables: dict[Hashable, Variable] | None = None,
792792
coord_names: set[Hashable] | None = None,
793793
dims: dict[Any, int] | None = None,
794-
attrs: dict[Hashable, Any] | None | Default = _default,
794+
attrs: dict[Hashable, Any] | Default | None = _default,
795795
indexes: dict[Hashable, Index] | None = None,
796-
encoding: dict | None | Default = _default,
796+
encoding: dict | Default | None = _default,
797797
inplace: bool = False,
798798
) -> Self:
799799
"""Fastpath constructor for internal use.
@@ -840,7 +840,7 @@ def _replace_with_new_dims(
840840
self,
841841
variables: dict[Hashable, Variable],
842842
coord_names: set | None = None,
843-
attrs: dict[Hashable, Any] | None | Default = _default,
843+
attrs: dict[Hashable, Any] | Default | None = _default,
844844
indexes: dict[Hashable, Index] | None = None,
845845
inplace: bool = False,
846846
) -> Self:
@@ -855,7 +855,7 @@ def _replace_vars_and_dims(
855855
variables: dict[Hashable, Variable],
856856
coord_names: set | None = None,
857857
dims: dict[Hashable, int] | None = None,
858-
attrs: dict[Hashable, Any] | None | Default = _default,
858+
attrs: dict[Hashable, Any] | Default | None = _default,
859859
inplace: bool = False,
860860
) -> Self:
861861
"""Deprecated version of _replace_with_new_dims().
@@ -4347,8 +4347,8 @@ def swap_dims(
43474347

43484348
def expand_dims(
43494349
self,
4350-
dim: None | Hashable | Sequence[Hashable] | Mapping[Any, Any] = None,
4351-
axis: None | int | Sequence[int] = None,
4350+
dim: Hashable | Sequence[Hashable] | Mapping[Any, Any] | None = None,
4351+
axis: int | Sequence[int] | None = None,
43524352
create_index_for_new_dim: bool = True,
43534353
**dim_kwargs: Any,
43544354
) -> Self:

xarray/core/datatree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ def _replace( # type: ignore[override]
347347
variables: dict[Hashable, Variable] | None = None,
348348
coord_names: set[Hashable] | None = None,
349349
dims: dict[Any, int] | None = None,
350-
attrs: dict[Hashable, Any] | None | Default = _default,
350+
attrs: dict[Hashable, Any] | Default | None = _default,
351351
indexes: dict[Hashable, Index] | None = None,
352-
encoding: dict | None | Default = _default,
352+
encoding: dict | Default | None = _default,
353353
inplace: bool = False,
354354
) -> Dataset:
355355
"""

0 commit comments

Comments
 (0)