Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed May 15, 2024
1 parent 89c3602 commit 9368da7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
15 changes: 0 additions & 15 deletions hypothesis-python/src/hypothesis/internal/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import copy
import dataclasses
import inspect
import itertools
import platform
import sys
import sysconfig
Expand Down Expand Up @@ -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):

Expand Down
46 changes: 14 additions & 32 deletions hypothesis-python/src/hypothesis/internal/conjecture/junkdrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import array
import sys
import warnings
from itertools import chain
from random import Random
from typing import (
Callable,
Expand All @@ -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"]

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

0 comments on commit 9368da7

Please sign in to comment.