Skip to content

Commit

Permalink
Use itertools.pairwise()
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromNVIDIA committed Aug 26, 2024
1 parent 10a766e commit 89731fe
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/rapids_pre_commit_hooks/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down Expand Up @@ -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}")

Expand Down

0 comments on commit 89731fe

Please sign in to comment.