Skip to content

Commit a765ae0

Browse files
Upgrade ruff to 0.8.0 (#9816)
1 parent 552a74b commit a765ae0

File tree

14 files changed

+70
-83
lines changed

14 files changed

+70
-83
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ repos:
2525
- id: text-unicode-replacement-char
2626
- repo: https://github.com/astral-sh/ruff-pre-commit
2727
# Ruff version.
28-
rev: v0.7.2
28+
rev: v0.8.0
2929
hooks:
3030
- id: ruff-format
3131
- id: ruff

asv_bench/benchmarks/dataset_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def make_ds(self, nfiles=10):
305305
ds.attrs = {"history": "created for xarray benchmarking"}
306306

307307
self.ds_list.append(ds)
308-
self.filenames_list.append("test_netcdf_%i.nc" % i)
308+
self.filenames_list.append(f"test_netcdf_{i}.nc")
309309

310310

311311
class IOWriteMultipleNetCDF3(IOMultipleNetCDF):

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ dev = [
4545
"pytest-env",
4646
"pytest-xdist",
4747
"pytest-timeout",
48-
"ruff",
48+
"ruff>=0.8.0",
4949
"sphinx",
5050
"sphinx_autosummary_accessors",
5151
"xarray[complete]",
@@ -256,7 +256,6 @@ ignore = [
256256
"E501", # line too long - let the formatter worry about that
257257
"E731", # do not assign a lambda expression, use a def
258258
"UP007", # use X | Y for type annotations
259-
"UP027", # deprecated
260259
"C40", # unnecessary generator, comprehension, or literal
261260
"PIE790", # unnecessary pass statement
262261
"PERF203", # try-except within a loop incurs performance overhead

xarray/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
# A hardcoded __all__ variable is necessary to appease
6666
# `mypy --strict` running in projects that import xarray.
67-
__all__ = (
67+
__all__ = ( # noqa: RUF022
6868
# Sub-packages
6969
"groupers",
7070
"testing",
@@ -117,8 +117,8 @@
117117
"Context",
118118
"Coordinates",
119119
"DataArray",
120-
"Dataset",
121120
"DataTree",
121+
"Dataset",
122122
"Index",
123123
"IndexSelResult",
124124
"IndexVariable",
@@ -131,6 +131,6 @@
131131
"SerializationWarning",
132132
"TreeIsomorphismError",
133133
# Constants
134-
"__version__",
135134
"ALL_DIMS",
135+
"__version__",
136136
)

xarray/coding/cftime_offsets.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,8 +1451,7 @@ def date_range_like(source, calendar, use_cftime=None):
14511451
from xarray.core.dataarray import DataArray
14521452

14531453
if not isinstance(source, pd.DatetimeIndex | CFTimeIndex) and (
1454-
isinstance(source, DataArray)
1455-
and (source.ndim != 1)
1454+
(isinstance(source, DataArray) and (source.ndim != 1))
14561455
or not _contains_datetime_like_objects(source.variable)
14571456
):
14581457
raise ValueError(

xarray/conventions.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -726,11 +726,8 @@ def _encode_coordinates(
726726
)
727727

728728
# if coordinates set to None, don't write coordinates attribute
729-
if (
730-
"coordinates" in attrs
731-
and attrs.get("coordinates") is None
732-
or "coordinates" in encoding
733-
and encoding.get("coordinates") is None
729+
if ("coordinates" in attrs and attrs.get("coordinates") is None) or (
730+
"coordinates" in encoding and encoding.get("coordinates") is None
734731
):
735732
# make sure "coordinates" is removed from attrs/encoding
736733
attrs.pop("coordinates", None)

xarray/core/dataset.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5401,11 +5401,9 @@ def _get_stack_index(
54015401
and var.dims[0] == dim
54025402
and (
54035403
# stack: must be a single coordinate index
5404-
not multi
5405-
and not self.xindexes.is_multi(name)
5404+
(not multi and not self.xindexes.is_multi(name))
54065405
# unstack: must be an index that implements .unstack
5407-
or multi
5408-
and type(index).unstack is not Index.unstack
5406+
or (multi and type(index).unstack is not Index.unstack)
54095407
)
54105408
):
54115409
if stack_index is not None and index is not stack_index:
@@ -7617,7 +7615,7 @@ def from_dataframe(cls, dataframe: pd.DataFrame, sparse: bool = False) -> Self:
76177615

76187616
if isinstance(idx, pd.MultiIndex):
76197617
dims = tuple(
7620-
name if name is not None else "level_%i" % n # type: ignore[redundant-expr]
7618+
name if name is not None else f"level_{n}" # type: ignore[redundant-expr]
76217619
for n, name in enumerate(idx.names)
76227620
)
76237621
for dim, lev in zip(dims, idx.levels, strict=True):

xarray/plot/utils.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,11 +869,11 @@ def _infer_interval_breaks(coord, axis=0, scale=None, check_monotonic=False):
869869
if check_monotonic and not _is_monotonic(coord, axis=axis):
870870
raise ValueError(
871871
"The input coordinate is not sorted in increasing "
872-
"order along axis %d. This can lead to unexpected "
872+
f"order along axis {axis}. This can lead to unexpected "
873873
"results. Consider calling the `sortby` method on "
874874
"the input DataArray. To plot data with categorical "
875875
"axes, consider using the `heatmap` function from "
876-
"the `seaborn` statistical plotting library." % axis
876+
"the `seaborn` statistical plotting library."
877877
)
878878

879879
# If logscale, compute the intervals in the logarithmic space
@@ -1708,8 +1708,7 @@ def _determine_guide(
17081708
if (
17091709
not add_colorbar
17101710
and (hueplt_norm.data is not None and hueplt_norm.data_is_numeric is False)
1711-
or sizeplt_norm.data is not None
1712-
):
1711+
) or sizeplt_norm.data is not None:
17131712
add_legend = True
17141713
else:
17151714
add_legend = False

xarray/testing/assertions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ def assert_equal(a, b, check_dim_order: bool = True):
124124
numpy.testing.assert_array_equal
125125
"""
126126
__tracebackhide__ = True
127-
assert (
128-
type(a) is type(b) or isinstance(a, Coordinates) and isinstance(b, Coordinates)
127+
assert type(a) is type(b) or (
128+
isinstance(a, Coordinates) and isinstance(b, Coordinates)
129129
)
130130
b = maybe_transpose_dims(a, b, check_dim_order)
131131
if isinstance(a, Variable | DataArray):
@@ -163,8 +163,8 @@ def assert_identical(a, b):
163163
assert_equal, assert_allclose, Dataset.equals, DataArray.equals
164164
"""
165165
__tracebackhide__ = True
166-
assert (
167-
type(a) is type(b) or isinstance(a, Coordinates) and isinstance(b, Coordinates)
166+
assert type(a) is type(b) or (
167+
isinstance(a, Coordinates) and isinstance(b, Coordinates)
168168
)
169169
if isinstance(a, Variable):
170170
assert a.identical(b), formatting.diff_array_repr(a, b, "identical")

xarray/tests/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ def __call__(self, dsk, keys, **kwargs):
210210
self.total_computes += 1
211211
if self.total_computes > self.max_computes:
212212
raise RuntimeError(
213-
"Too many computes. Total: %d > max: %d."
214-
% (self.total_computes, self.max_computes)
213+
f"Too many computes. Total: {self.total_computes} > max: {self.max_computes}."
215214
)
216215
return dask.get(dsk, keys, **kwargs)
217216

xarray/tests/test_backends.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,7 @@ def test_roundtrip_mask_and_scale(self, decoded_fn, encoded_fn, dtype) -> None:
963963
decoded = decoded_fn(dtype)
964964
encoded = encoded_fn(dtype)
965965
if decoded["x"].encoding["dtype"] == "u1" and not (
966-
self.engine == "netcdf4"
967-
and self.file_format is None
966+
(self.engine == "netcdf4" and self.file_format is None)
968967
or self.file_format == "NETCDF4"
969968
):
970969
pytest.skip("uint8 data can't be written to non-NetCDF4 data")

xarray/tests/test_dataset.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,16 +388,15 @@ def test_unicode_data(self) -> None:
388388

389389
byteorder = "<" if sys.byteorder == "little" else ">"
390390
expected = dedent(
391-
"""\
391+
f"""\
392392
<xarray.Dataset> Size: 12B
393393
Dimensions: (foø: 1)
394394
Coordinates:
395-
* foø (foø) %cU3 12B %r
395+
* foø (foø) {byteorder}U3 12B {'ba®'!r}
396396
Data variables:
397397
*empty*
398398
Attributes:
399399
å: ∑"""
400-
% (byteorder, "ba®")
401400
)
402401
actual = str(data)
403402
assert expected == actual

xarray/tests/test_formatting.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def test_diff_array_repr(self) -> None:
295295

296296
byteorder = "<" if sys.byteorder == "little" else ">"
297297
expected = dedent(
298-
"""\
298+
f"""\
299299
Left and right DataArray objects are not identical
300300
Differing dimensions:
301301
(x: 2, y: 3) != (x: 2)
@@ -306,8 +306,8 @@ def test_diff_array_repr(self) -> None:
306306
R
307307
array([1, 2], dtype=int64)
308308
Differing coordinates:
309-
L * x (x) %cU1 8B 'a' 'b'
310-
R * x (x) %cU1 8B 'a' 'c'
309+
L * x (x) {byteorder}U1 8B 'a' 'b'
310+
R * x (x) {byteorder}U1 8B 'a' 'c'
311311
Coordinates only on the left object:
312312
* y (y) int64 24B 1 2 3
313313
Coordinates only on the right object:
@@ -317,7 +317,6 @@ def test_diff_array_repr(self) -> None:
317317
R units: kg
318318
Attributes only on the left object:
319319
description: desc"""
320-
% (byteorder, byteorder)
321320
)
322321

323322
actual = formatting.diff_array_repr(da_a, da_b, "identical")
@@ -496,15 +495,15 @@ def test_diff_dataset_repr(self) -> None:
496495

497496
byteorder = "<" if sys.byteorder == "little" else ">"
498497
expected = dedent(
499-
"""\
498+
f"""\
500499
Left and right Dataset objects are not identical
501500
Differing dimensions:
502501
(x: 2, y: 3) != (x: 2)
503502
Differing coordinates:
504-
L * x (x) %cU1 8B 'a' 'b'
503+
L * x (x) {byteorder}U1 8B 'a' 'b'
505504
Differing variable attributes:
506505
foo: bar
507-
R * x (x) %cU1 8B 'a' 'c'
506+
R * x (x) {byteorder}U1 8B 'a' 'c'
508507
Differing variable attributes:
509508
source: 0
510509
foo: baz
@@ -522,7 +521,6 @@ def test_diff_dataset_repr(self) -> None:
522521
R title: newtitle
523522
Attributes only on the left object:
524523
description: desc"""
525-
% (byteorder, byteorder)
526524
)
527525

528526
actual = formatting.diff_dataset_repr(ds_a, ds_b, "identical")

xarray/ufuncs.py

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -247,70 +247,45 @@ def _dedent(doc):
247247
"absolute",
248248
"acos",
249249
"acosh",
250+
"add",
251+
"angle",
250252
"arccos",
251253
"arccosh",
252254
"arcsin",
253255
"arcsinh",
254256
"arctan",
257+
"arctan2",
255258
"arctanh",
256259
"asin",
257260
"asinh",
258261
"atan",
262+
"atan2",
259263
"atanh",
264+
"bitwise_and",
260265
"bitwise_count",
261266
"bitwise_invert",
267+
"bitwise_left_shift",
262268
"bitwise_not",
269+
"bitwise_or",
270+
"bitwise_right_shift",
271+
"bitwise_xor",
263272
"cbrt",
264273
"ceil",
265274
"conj",
266275
"conjugate",
276+
"copysign",
267277
"cos",
268278
"cosh",
269279
"deg2rad",
270280
"degrees",
281+
"divide",
282+
"equal",
271283
"exp",
272284
"exp2",
273285
"expm1",
274286
"fabs",
275-
"floor",
276-
"invert",
277-
"isfinite",
278-
"isinf",
279-
"isnan",
280-
"isnat",
281-
"log",
282-
"log10",
283-
"log1p",
284-
"log2",
285-
"logical_not",
286-
"negative",
287-
"positive",
288-
"rad2deg",
289-
"radians",
290-
"reciprocal",
291-
"rint",
292-
"sign",
293-
"signbit",
294-
"sin",
295-
"sinh",
296-
"spacing",
297-
"sqrt",
298-
"square",
299-
"tan",
300-
"tanh",
301-
"trunc",
302-
"add",
303-
"arctan2",
304-
"atan2",
305-
"bitwise_and",
306-
"bitwise_left_shift",
307-
"bitwise_or",
308-
"bitwise_right_shift",
309-
"bitwise_xor",
310-
"copysign",
311-
"divide",
312-
"equal",
313287
"float_power",
288+
"floor",
314289
"floor_divide",
315290
"fmax",
316291
"fmin",
@@ -320,29 +295,54 @@ def _dedent(doc):
320295
"greater_equal",
321296
"heaviside",
322297
"hypot",
298+
"invert",
299+
"iscomplex",
300+
"isfinite",
301+
"isinf",
302+
"isnan",
303+
"isnat",
304+
"isreal",
323305
"lcm",
324306
"ldexp",
325307
"left_shift",
326308
"less",
327309
"less_equal",
310+
"log",
311+
"log1p",
312+
"log2",
313+
"log10",
328314
"logaddexp",
329315
"logaddexp2",
330316
"logical_and",
317+
"logical_not",
331318
"logical_or",
332319
"logical_xor",
333320
"maximum",
334321
"minimum",
335322
"mod",
336323
"multiply",
324+
"negative",
337325
"nextafter",
338326
"not_equal",
327+
"positive",
339328
"pow",
340329
"power",
330+
"rad2deg",
331+
"radians",
332+
"reciprocal",
341333
"remainder",
342334
"right_shift",
335+
"rint",
336+
"sign",
337+
"signbit",
338+
"sin",
339+
"sinh",
340+
"spacing",
341+
"sqrt",
342+
"square",
343343
"subtract",
344+
"tan",
345+
"tanh",
344346
"true_divide",
345-
"angle",
346-
"isreal",
347-
"iscomplex",
347+
"trunc",
348348
]

0 commit comments

Comments
 (0)