From 88169c7cb6dbea5262bb58e46549fb1020cbc96f Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos Orfanos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Thu, 7 Nov 2024 16:38:25 +0100 Subject: [PATCH] Apply ruff rule RUF007 (#9739) RUF007 Prefer `itertools.pairwise()` over `zip()` when iterating over successive pairs --- pyproject.toml | 1 - xarray/groupers.py | 4 ++-- xarray/tests/test_groupby.py | 3 ++- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9ba2bf33c2b..abf949945bb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -249,7 +249,6 @@ ignore = [ "RUF002", "RUF003", "RUF005", - "RUF007", "RUF012", ] extend-select = [ diff --git a/xarray/groupers.py b/xarray/groupers.py index 0adfcce9483..89b189e582e 100644 --- a/xarray/groupers.py +++ b/xarray/groupers.py @@ -9,6 +9,7 @@ import datetime from abc import ABC, abstractmethod from dataclasses import dataclass, field +from itertools import pairwise from typing import TYPE_CHECKING, Any, Literal, cast import numpy as np @@ -496,8 +497,7 @@ def factorize(self, group: T_Group) -> EncodedGroups: full_index, first_items, codes_ = self._get_index_and_items() sbins = first_items.values.astype(np.int64) group_indices: GroupIndices = tuple( - [slice(i, j) for i, j in zip(sbins[:-1], sbins[1:], strict=True)] - + [slice(sbins[-1], None)] + [slice(i, j) for i, j in pairwise(sbins)] + [slice(sbins[-1], None)] ) unique_coord = Variable( diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index c0eeace71af..475a988b694 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -3,6 +3,7 @@ import datetime import operator import warnings +from itertools import pairwise from unittest import mock import numpy as np @@ -1732,7 +1733,7 @@ def test_groupby_bins_multidim(self) -> None: bincoord = np.array( [ pd.Interval(left, right, closed="right") - for left, right in zip(bins[:-1], bins[1:], strict=True) + for left, right in pairwise(bins) ], dtype=object, )