|
15 | 15 |
|
16 | 16 | from hypothesis import strategies as st |
17 | 17 | from hypothesis.control import current_build_context |
18 | | -from hypothesis.errors import InvalidArgument |
| 18 | +from hypothesis.errors import CannotInvert, InvalidArgument |
| 19 | +from hypothesis.internal.compat import add_note |
19 | 20 | from hypothesis.internal.conjecture import utils as cu |
| 21 | +from hypothesis.internal.conjecture.choice import ChoiceT |
20 | 22 | from hypothesis.internal.conjecture.data import ConjectureData |
21 | 23 | from hypothesis.internal.conjecture.engine import BUFFER_SIZE |
22 | | -from hypothesis.internal.conjecture.junkdrawer import LazySequenceCopy |
| 24 | +from hypothesis.internal.conjecture.junkdrawer import LazySequenceCopy, equal_values |
23 | 25 | from hypothesis.internal.conjecture.utils import combine_labels |
24 | 26 | from hypothesis.internal.filtering import get_integer_predicate_bounds |
25 | 27 | from hypothesis.internal.reflection import is_identity_function |
@@ -86,6 +88,22 @@ def do_draw(self, data: ConjectureData) -> tuple[Ex, ...]: |
86 | 88 | ) |
87 | 89 | return result |
88 | 90 |
|
| 91 | + def _invert(self, value: Any) -> tuple[ChoiceT, ...]: |
| 92 | + if not isinstance(value, tuple) or len(value) != len(self.element_strategies): |
| 93 | + raise CannotInvert( |
| 94 | + f"{value!r} is not a tuple of length {len(self.element_strategies)}" |
| 95 | + ) |
| 96 | + choices: list[ChoiceT] = [] |
| 97 | + for i, (strategy, element) in enumerate( |
| 98 | + zip(self.element_strategies, value, strict=True) |
| 99 | + ): |
| 100 | + try: |
| 101 | + choices.extend(strategy._invert(element)) |
| 102 | + except CannotInvert as exc: |
| 103 | + add_note(exc, f"at index {i} of {value!r}, strategy={self!r}") |
| 104 | + raise |
| 105 | + return tuple(choices) |
| 106 | + |
89 | 107 | def calc_is_empty(self, recur: RecurT) -> bool: |
90 | 108 | return any(recur(e) for e in self.element_strategies) |
91 | 109 |
|
@@ -219,6 +237,31 @@ def do_draw(self, data: ConjectureData) -> list[Ex]: |
219 | 237 | result.append(data.draw(self.element_strategy)) |
220 | 238 | return result |
221 | 239 |
|
| 240 | + def _invert(self, value: Any) -> tuple[ChoiceT, ...]: |
| 241 | + if not isinstance(value, list): |
| 242 | + raise CannotInvert(f"{value!r} is not a list") |
| 243 | + if not (self.min_size <= len(value) <= self.max_size): |
| 244 | + raise CannotInvert( |
| 245 | + f"len={len(value)} outside " |
| 246 | + f"[{self.min_size}, {self.max_size!r}] for {self!r}" |
| 247 | + ) |
| 248 | + if self.element_strategy.is_empty: |
| 249 | + # do_draw returns [] without drawing anything |
| 250 | + if value: |
| 251 | + raise CannotInvert(f"elements of {self!r} are empty") |
| 252 | + return () |
| 253 | + elements = cu.invert_many(self.min_size, self.max_size) |
| 254 | + choices: list[ChoiceT] = [] |
| 255 | + for i, element in enumerate(value): |
| 256 | + choices.extend(elements.more()) |
| 257 | + try: |
| 258 | + choices.extend(self.element_strategy._invert(element)) |
| 259 | + except CannotInvert as exc: |
| 260 | + add_note(exc, f"at index {i} of {value!r}, strategy={self!r}") |
| 261 | + raise |
| 262 | + choices.extend(elements.done()) |
| 263 | + return tuple(choices) |
| 264 | + |
222 | 265 | def __repr__(self) -> str: |
223 | 266 | return ( |
224 | 267 | f"{self.__class__.__name__}({self.element_strategy!r}, " |
@@ -327,6 +370,20 @@ def not_yet_in_unique_list(val: Ex) -> bool: # type: ignore # covariant type pa |
327 | 370 | assert self.max_size >= len(result) >= self.min_size |
328 | 371 | return result |
329 | 372 |
|
| 373 | + def _invert(self, value: Any) -> tuple[ChoiceT, ...]: |
| 374 | + if self.tuple_suffixes is not None: |
| 375 | + # each element interleaves an element_strategy draw with a |
| 376 | + # tuple_suffixes draw, which we cannot separate here |
| 377 | + raise CannotInvert(f"cannot invert {self!r} with tuple_suffixes") |
| 378 | + # A valid unique list draws exactly like a ListStrategy, since every |
| 379 | + # element passes the uniqueness filter on its first draw. |
| 380 | + choices = ListStrategy._invert(self, value) |
| 381 | + for keyfunc in self.keys: |
| 382 | + keys = list(map(keyfunc, value)) |
| 383 | + if len(set(keys)) != len(keys): |
| 384 | + raise CannotInvert(f"{value!r} has duplicate keys for {self!r}") |
| 385 | + return choices |
| 386 | + |
330 | 387 |
|
331 | 388 | class UniqueSampledListStrategy(UniqueListStrategy): |
332 | 389 | def do_draw(self, data: ConjectureData) -> list[Ex]: |
@@ -362,6 +419,39 @@ def do_draw(self, data: ConjectureData) -> list[Ex]: |
362 | 419 | assert self.max_size >= len(result) >= self.min_size |
363 | 420 | return result |
364 | 421 |
|
| 422 | + def _invert(self, value: Any) -> tuple[ChoiceT, ...]: |
| 423 | + if self.tuple_suffixes is not None: |
| 424 | + raise CannotInvert(f"cannot invert {self!r} with tuple_suffixes") |
| 425 | + if not isinstance(value, list): |
| 426 | + raise CannotInvert(f"{value!r} is not a list") |
| 427 | + if not (self.min_size <= len(value) <= self.max_size): |
| 428 | + raise CannotInvert( |
| 429 | + f"len={len(value)} outside " |
| 430 | + f"[{self.min_size}, {self.max_size!r}] for {self!r}" |
| 431 | + ) |
| 432 | + # do_draw indexes into the pool of not-yet-drawn elements, so each |
| 433 | + # index is relative to what remains, not to the original elements. |
| 434 | + assert isinstance(self.element_strategy, SampledFromStrategy) |
| 435 | + remaining = list(self.element_strategy.elements) |
| 436 | + elements = cu.invert_many(self.min_size, self.max_size) |
| 437 | + choices: list[ChoiceT] = [] |
| 438 | + for i, target in enumerate(value): |
| 439 | + choices.extend(elements.more()) |
| 440 | + for j, candidate in enumerate(remaining): |
| 441 | + if equal_values(self.element_strategy._transform(candidate), target): |
| 442 | + choices.append(j) |
| 443 | + remaining.pop(j) |
| 444 | + break |
| 445 | + else: |
| 446 | + raise CannotInvert( |
| 447 | + f"at index {i} of {value!r}: {target!r} is not among the " |
| 448 | + f"remaining elements of {self!r}" |
| 449 | + ) |
| 450 | + if remaining: |
| 451 | + # with an exhausted pool, do_draw stops without drawing a boolean |
| 452 | + choices.extend(elements.done()) |
| 453 | + return tuple(choices) |
| 454 | + |
365 | 455 |
|
366 | 456 | class FixedDictStrategy(SearchStrategy[Mapping[Any, Any]]): |
367 | 457 | """A strategy which produces mappings with a fixed set of keys, given a |
|
0 commit comments