From c01de3997cf886b91c8134f17fa086401dbb22a7 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Mon, 6 May 2024 12:17:22 -0700 Subject: [PATCH] Fix for ruff 0.4.3 (#9007) * Fix for ruff 0.4.3 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- ci/min_deps_check.py | 2 +- xarray/datatree_/datatree/__init__.py | 1 - xarray/datatree_/datatree/common.py | 9 ++++----- xarray/tests/test_dataset.py | 7 ++++--- xarray/tests/test_groupby.py | 6 +++--- xarray/tests/test_rolling.py | 6 +++--- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/ci/min_deps_check.py b/ci/min_deps_check.py index 48ea323ed81..5ec7bff0a30 100755 --- a/ci/min_deps_check.py +++ b/ci/min_deps_check.py @@ -133,7 +133,7 @@ def process_pkg( - publication date of version suggested by policy (YYYY-MM-DD) - status ("<", "=", "> (!)") """ - print("Analyzing %s..." % pkg) + print(f"Analyzing {pkg}...") versions = query_conda(pkg) try: diff --git a/xarray/datatree_/datatree/__init__.py b/xarray/datatree_/datatree/__init__.py index 3159d612913..51c5f1b3073 100644 --- a/xarray/datatree_/datatree/__init__.py +++ b/xarray/datatree_/datatree/__init__.py @@ -1,7 +1,6 @@ # import public API from xarray.core.treenode import InvalidTreeError, NotFoundInTreeError - __all__ = ( "InvalidTreeError", "NotFoundInTreeError", diff --git a/xarray/datatree_/datatree/common.py b/xarray/datatree_/datatree/common.py index e4d52925ede..f4f74337c50 100644 --- a/xarray/datatree_/datatree/common.py +++ b/xarray/datatree_/datatree/common.py @@ -6,8 +6,9 @@ """ import warnings +from collections.abc import Hashable, Iterable, Mapping from contextlib import suppress -from typing import Any, Hashable, Iterable, List, Mapping +from typing import Any class TreeAttrAccessMixin: @@ -83,16 +84,14 @@ def __setattr__(self, name: str, value: Any) -> None: except AttributeError as e: # Don't accidentally shadow custom AttributeErrors, e.g. # DataArray.dims.setter - if str(e) != "{!r} object has no attribute {!r}".format( - type(self).__name__, name - ): + if str(e) != f"{type(self).__name__!r} object has no attribute {name!r}": raise raise AttributeError( f"cannot set attribute {name!r} on a {type(self).__name__!r} object. Use __setitem__ style" "assignment (e.g., `ds['name'] = ...`) instead of assigning variables." ) from e - def __dir__(self) -> List[str]: + def __dir__(self) -> list[str]: """Provide method name lookup and completion. Only provide 'public' methods. """ diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 301596e032f..59b5b2b9b71 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -284,7 +284,7 @@ def test_repr(self) -> None: Dimensions: (dim2: 9, dim3: 10, time: 20, dim1: 8) Coordinates: * dim2 (dim2) float64 72B 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 - * dim3 (dim3) %s 40B 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' + * dim3 (dim3) {} 40B 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' * time (time) datetime64[ns] 160B 2000-01-01 2000-01-02 ... 2000-01-20 numbers (dim3) int64 80B 0 1 2 0 0 1 1 2 2 3 Dimensions without coordinates: dim1 @@ -293,8 +293,9 @@ def test_repr(self) -> None: var2 (dim1, dim2) float64 576B 1.162 -1.097 -2.123 ... 1.267 0.3328 var3 (dim3, dim1) float64 640B 0.5565 -0.2121 0.4563 ... -0.2452 -0.3616 Attributes: - foo: bar""" - % data["dim3"].dtype + foo: bar""".format( + data["dim3"].dtype + ) ) actual = "\n".join(x.rstrip() for x in repr(data).split("\n")) print(actual) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index e9e4eb1364c..7134fe96d01 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -577,8 +577,8 @@ def test_da_groupby_assign_coords() -> None: def test_groupby_repr(obj, dim) -> None: actual = repr(obj.groupby(dim)) expected = f"{obj.__class__.__name__}GroupBy" - expected += ", grouped over %r" % dim - expected += "\n%r groups with labels " % (len(np.unique(obj[dim]))) + expected += f", grouped over {dim!r}" + expected += f"\n{len(np.unique(obj[dim]))!r} groups with labels " if dim == "x": expected += "1, 2, 3, 4, 5." elif dim == "y": @@ -595,7 +595,7 @@ def test_groupby_repr_datetime(obj) -> None: actual = repr(obj.groupby("t.month")) expected = f"{obj.__class__.__name__}GroupBy" expected += ", grouped over 'month'" - expected += "\n%r groups with labels " % (len(np.unique(obj.t.dt.month))) + expected += f"\n{len(np.unique(obj.t.dt.month))!r} groups with labels " expected += "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12." assert actual == expected diff --git a/xarray/tests/test_rolling.py b/xarray/tests/test_rolling.py index 79a5ba0a667..89f6ebba2c3 100644 --- a/xarray/tests/test_rolling.py +++ b/xarray/tests/test_rolling.py @@ -254,7 +254,7 @@ def test_rolling_reduce( rolling_obj = da.rolling(time=window, center=center, min_periods=min_periods) # add nan prefix to numpy methods to get similar # behavior as bottleneck - actual = rolling_obj.reduce(getattr(np, "nan%s" % name)) + actual = rolling_obj.reduce(getattr(np, f"nan{name}")) expected = getattr(rolling_obj, name)() assert_allclose(actual, expected) assert actual.sizes == expected.sizes @@ -276,7 +276,7 @@ def test_rolling_reduce_nonnumeric( rolling_obj = da.rolling(time=window, center=center, min_periods=min_periods) # add nan prefix to numpy methods to get similar behavior as bottleneck - actual = rolling_obj.reduce(getattr(np, "nan%s" % name)) + actual = rolling_obj.reduce(getattr(np, f"nan{name}")) expected = getattr(rolling_obj, name)() assert_allclose(actual, expected) assert actual.sizes == expected.sizes @@ -741,7 +741,7 @@ def test_rolling_reduce(self, ds, center, min_periods, window, name) -> None: rolling_obj = ds.rolling(time=window, center=center, min_periods=min_periods) # add nan prefix to numpy methods to get similar behavior as bottleneck - actual = rolling_obj.reduce(getattr(np, "nan%s" % name)) + actual = rolling_obj.reduce(getattr(np, f"nan{name}")) expected = getattr(rolling_obj, name)() assert_allclose(actual, expected) assert ds.sizes == actual.sizes