Skip to content

Commit

Permalink
Apply new ruff rule RUF021
Browse files Browse the repository at this point in the history
RUF021 Parenthesize `a and b` expressions when chaining `and` and `or` together,
to make the precedence clear
  • Loading branch information
DimitriPapadopoulos committed Nov 24, 2024
1 parent beaa3e8 commit 70d2c7b
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 19 deletions.
3 changes: 1 addition & 2 deletions xarray/coding/cftime_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,8 +1451,7 @@ def date_range_like(source, calendar, use_cftime=None):
from xarray.core.dataarray import DataArray

if not isinstance(source, pd.DatetimeIndex | CFTimeIndex) and (
isinstance(source, DataArray)
and (source.ndim != 1)
(isinstance(source, DataArray) and (source.ndim != 1))
or not _contains_datetime_like_objects(source.variable)
):
raise ValueError(
Expand Down
7 changes: 2 additions & 5 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,11 +726,8 @@ def _encode_coordinates(
)

# if coordinates set to None, don't write coordinates attribute
if (
"coordinates" in attrs
and attrs.get("coordinates") is None
or "coordinates" in encoding
and encoding.get("coordinates") is None
if ("coordinates" in attrs and attrs.get("coordinates") is None) or (
"coordinates" in encoding and encoding.get("coordinates") is None
):
# make sure "coordinates" is removed from attrs/encoding
attrs.pop("coordinates", None)
Expand Down
6 changes: 2 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5401,11 +5401,9 @@ def _get_stack_index(
and var.dims[0] == dim
and (
# stack: must be a single coordinate index
not multi
and not self.xindexes.is_multi(name)
(not multi and not self.xindexes.is_multi(name))
# unstack: must be an index that implements .unstack
or multi
and type(index).unstack is not Index.unstack
or (multi and type(index).unstack is not Index.unstack)
)
):
if stack_index is not None and index is not stack_index:
Expand Down
3 changes: 1 addition & 2 deletions xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,8 +1708,7 @@ def _determine_guide(
if (
not add_colorbar
and (hueplt_norm.data is not None and hueplt_norm.data_is_numeric is False)
or sizeplt_norm.data is not None
):
) or sizeplt_norm.data is not None:
add_legend = True
else:
add_legend = False
Expand Down
8 changes: 4 additions & 4 deletions xarray/testing/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def assert_equal(a, b, check_dim_order: bool = True):
numpy.testing.assert_array_equal
"""
__tracebackhide__ = True
assert (
type(a) is type(b) or isinstance(a, Coordinates) and isinstance(b, Coordinates)
assert type(a) is type(b) or (
isinstance(a, Coordinates) and isinstance(b, Coordinates)
)
b = maybe_transpose_dims(a, b, check_dim_order)
if isinstance(a, Variable | DataArray):
Expand Down Expand Up @@ -163,8 +163,8 @@ def assert_identical(a, b):
assert_equal, assert_allclose, Dataset.equals, DataArray.equals
"""
__tracebackhide__ = True
assert (
type(a) is type(b) or isinstance(a, Coordinates) and isinstance(b, Coordinates)
assert type(a) is type(b) or (
isinstance(a, Coordinates) and isinstance(b, Coordinates)
)
if isinstance(a, Variable):
assert a.identical(b), formatting.diff_array_repr(a, b, "identical")
Expand Down
3 changes: 1 addition & 2 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,7 @@ def test_roundtrip_mask_and_scale(self, decoded_fn, encoded_fn, dtype) -> None:
decoded = decoded_fn(dtype)
encoded = encoded_fn(dtype)
if decoded["x"].encoding["dtype"] == "u1" and not (
self.engine == "netcdf4"
and self.file_format is None
(self.engine == "netcdf4" and self.file_format is None)
or self.file_format == "NETCDF4"
):
pytest.skip("uint8 data can't be written to non-NetCDF4 data")
Expand Down

0 comments on commit 70d2c7b

Please sign in to comment.