From 9368da731ecadb0c6a62de20babe9a7d68b0815f Mon Sep 17 00:00:00 2001 From: Liam DeVoe Date: Tue, 14 May 2024 14:38:14 -0400 Subject: [PATCH] simplify implementation --- .../src/hypothesis/internal/compat.py | 15 ------ .../internal/conjecture/junkdrawer.py | 46 ++++++------------- 2 files changed, 14 insertions(+), 47 deletions(-) diff --git a/hypothesis-python/src/hypothesis/internal/compat.py b/hypothesis-python/src/hypothesis/internal/compat.py index 40255224fa..4a3a94a084 100644 --- a/hypothesis-python/src/hypothesis/internal/compat.py +++ b/hypothesis-python/src/hypothesis/internal/compat.py @@ -12,7 +12,6 @@ import copy import dataclasses import inspect -import itertools import platform import sys import sysconfig @@ -252,20 +251,6 @@ def bad_django_TestCase(runner): return not isinstance(runner, HypothesisTestCase) -if sys.version_info[:2] < (3, 10): - - # copied straight from - # https://docs.python.org/3/library/itertools.html#itertools.pairwise - def pairwise(iterable): - iterator = iter(iterable) - a = next(iterator, None) - for b in iterator: - yield a, b - a = b - -else: - pairwise = itertools.pairwise - # see issue #3812 if sys.version_info[:2] < (3, 12): diff --git a/hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py b/hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py index 71e2546336..716b5d3d82 100644 --- a/hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py +++ b/hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py @@ -15,7 +15,6 @@ import array import sys import warnings -from itertools import chain from random import Random from typing import ( Callable, @@ -35,7 +34,6 @@ from sortedcontainers import SortedList from hypothesis.errors import HypothesisWarning -from hypothesis.internal.compat import pairwise ARRAY_CODES = ["B", "H", "I", "L", "Q", "O"] @@ -254,38 +252,22 @@ def __underlying_index(self, i: int) -> int: assert 0 <= i < n if self.__popped_indices is not None: - i = self.__underlying_index_for_pops(i) - + # given an index i in the popped representation of the list, compute + # its corresponding index in the underlying list. given + # l = [1, 4, 2, 10, 188] + # l.pop(3) + # l.pop(1) + # assert l == [1, 2, 188] + # + # we want l[i] == self.__values[f(i)], where f is this function. + assert len(self.__popped_indices) <= len(self.__values) + + for idx in self.__popped_indices: + if idx > i: + break + i += 1 return i - def __underlying_index_for_pops(self, i): - # given an index i in the popped representation of the list, compute - # its corresponding index in the underlying list. given - # l = [1, 4, 2, 10, 188] - # l.pop(3) - # l.pop(1) - # assert l == [1, 2, 188] - # - # we want l[i] == self.__values[f(i)], where f is this function. - assert len(self.__popped_indices) <= len(self.__values) - - # we know what indices have been popped. count the total gap (number of - # "free indices") between these these, which is the elements remaining. - # stop whenever the gap is bigger than we need it to be and compute - # exactly where in the gap the expected index would be. - # - # This relies on popped_indices being sorted. - gap = 0 - for n1, n2 in pairwise( - chain([-1], self.__popped_indices, [len(self.__values)]) - ): - prev_gap = gap - gap += n2 - n1 - 1 - if gap > i: - return n1 + (i - prev_gap) + 1 - - raise NotImplementedError("unreachable") - def clamp(lower: float, value: float, upper: float) -> float: """Given a value and lower/upper bounds, 'clamp' the value so that