From 89731fe80b8a3c997c5064c46a1f4ba3b91230a4 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 26 Aug 2024 15:46:34 -0400 Subject: [PATCH] Use itertools.pairwise() --- src/rapids_pre_commit_hooks/lint.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/rapids_pre_commit_hooks/lint.py b/src/rapids_pre_commit_hooks/lint.py index 3a3b033..5a6f3f1 100644 --- a/src/rapids_pre_commit_hooks/lint.py +++ b/src/rapids_pre_commit_hooks/lint.py @@ -19,24 +19,12 @@ import functools import re import warnings -from typing import Callable, Generator, Iterable, Optional +from itertools import pairwise +from typing import Callable, Optional from rich.console import Console from rich.markup import escape - -# Taken from Python docs -# (https://docs.python.org/3.12/library/itertools.html#itertools.pairwise) -# Replace with itertools.pairwise after dropping Python 3.9 support -def _pairwise(iterable: Iterable) -> Generator: - # pairwise('ABCDEFG') → AB BC CD DE EF FG - iterator = iter(iterable) - a = next(iterator, None) - for b in iterator: - yield a, b - a = b - - _PosType = tuple[int, int] @@ -101,7 +89,7 @@ def fix(self) -> str: key=lambda replacement: replacement.pos, ) - for r1, r2 in _pairwise(sorted_replacements): + for r1, r2 in pairwise(sorted_replacements): if r1.pos[1] > r2.pos[0]: raise OverlappingReplacementsError(f"{r1} overlaps with {r2}")