diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-02-09 09:20:31 +0300 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-02-09 19:19:06 +0300 |
commit | e670ed618db9d69b1fe0034fdb2835098fcd4588 (patch) | |
tree | a9edc22ce8dd856e1b865e2dbb5b574bd693bcf2 | |
parent | 754f9eeebbdbcbf7a053fdb5a6e9ea5b7b60fb87 (diff) | |
download | ydb-e670ed618db9d69b1fe0034fdb2835098fcd4588.tar.gz |
Intermediate changes
5 files changed, 66 insertions, 19 deletions
diff --git a/contrib/python/hypothesis/py3/.dist-info/METADATA b/contrib/python/hypothesis/py3/.dist-info/METADATA index 86de48abca..135737b731 100644 --- a/contrib/python/hypothesis/py3/.dist-info/METADATA +++ b/contrib/python/hypothesis/py3/.dist-info/METADATA @@ -1,6 +1,6 @@ Metadata-Version: 2.1 Name: hypothesis -Version: 6.96.4 +Version: 6.97.0 Summary: A library for property-based testing Home-page: https://hypothesis.works Author: David R. MacIver and Zac Hatfield-Dodds diff --git a/contrib/python/hypothesis/py3/hypothesis/internal/compat.py b/contrib/python/hypothesis/py3/hypothesis/internal/compat.py index 41e8ce61d1..24e30b112b 100644 --- a/contrib/python/hypothesis/py3/hypothesis/internal/compat.py +++ b/contrib/python/hypothesis/py3/hypothesis/internal/compat.py @@ -16,7 +16,7 @@ import platform import sys import typing from functools import partial -from typing import Any, ForwardRef, get_args +from typing import Any, ForwardRef, List, Optional, get_args try: BaseExceptionGroup = BaseExceptionGroup @@ -180,6 +180,25 @@ def ceil(x): return y +def extract_bits(x: int, /, width: Optional[int] = None) -> List[int]: + assert x >= 0 + result = [] + while x: + result.append(x & 1) + x >>= 1 + if width is not None: + result = (result + [0] * width)[:width] + result.reverse() + return result + + +# int.bit_count was added sometime around python 3.9 +try: + bit_count = int.bit_count +except AttributeError: # pragma: no cover + bit_count = lambda self: sum(extract_bits(abs(self))) + + def bad_django_TestCase(runner): if runner is None or "django.test" not in sys.modules: return False diff --git a/contrib/python/hypothesis/py3/hypothesis/strategies/_internal/core.py b/contrib/python/hypothesis/py3/hypothesis/strategies/_internal/core.py index 028d8405c7..800a8c4e56 100644 --- a/contrib/python/hypothesis/py3/hypothesis/strategies/_internal/core.py +++ b/contrib/python/hypothesis/py3/hypothesis/strategies/_internal/core.py @@ -70,6 +70,7 @@ from hypothesis.internal.charmap import ( from hypothesis.internal.compat import ( Concatenate, ParamSpec, + bit_count, ceil, floor, get_type_hints, @@ -202,6 +203,48 @@ def sampled_from( that behaviour, use ``sampled_from(seq) if seq else nothing()``. """ values = check_sample(elements, "sampled_from") + try: + if isinstance(elements, type) and issubclass(elements, enum.Enum): + repr_ = f"sampled_from({elements.__module__}.{elements.__name__})" + else: + repr_ = f"sampled_from({elements!r})" + except Exception: # pragma: no cover + repr_ = None + if isclass(elements) and issubclass(elements, enum.Flag): + # Combinations of enum.Flag members (including empty) are also members. We generate these + # dynamically, because static allocation takes O(2^n) memory. LazyStrategy is used for the + # ease of force_repr. + # Add all named values, both flag bits (== list(elements)) and aliases. The aliases are + # necessary for full coverage for flags that would fail enum.NAMED_FLAGS check, and they + # are also nice values to shrink to. + flags = sorted( + set(elements.__members__.values()), + key=lambda v: (bit_count(v.value), v.value), + ) + # Finally, try to construct the empty state if it is not named. It's placed at the + # end so that we shrink to named values. + flags_with_empty = flags + if not flags or flags[0].value != 0: + try: + flags_with_empty = [*flags, elements(0)] + except TypeError: # pragma: no cover + # Happens on some python versions (at least 3.12) when there are no named values + pass + inner = [ + # Consider one or no named flags set, with shrink-to-named-flag behaviour. + # Special cases (length zero or one) are handled by the inner sampled_from. + sampled_from(flags_with_empty), + ] + if len(flags) > 1: + inner += [ + # Uniform distribution over number of named flags or combinations set. The overlap + # at r=1 is intentional, it may lead to oversampling but gives consistent shrinking + # behaviour. + integers(min_value=1, max_value=len(flags)) + .flatmap(lambda r: sets(sampled_from(flags), min_size=r, max_size=r)) + .map(lambda s: elements(reduce(operator.or_, s))), + ] + return LazyStrategy(one_of, args=inner, kwargs={}, force_repr=repr_) if not values: if ( isinstance(elements, type) @@ -217,21 +260,6 @@ def sampled_from( raise InvalidArgument("Cannot sample from a length-zero sequence.") if len(values) == 1: return just(values[0]) - try: - if isinstance(elements, type) and issubclass(elements, enum.Enum): - repr_ = f"sampled_from({elements.__module__}.{elements.__name__})" - else: - repr_ = f"sampled_from({elements!r})" - except Exception: # pragma: no cover - repr_ = None - if isclass(elements) and issubclass(elements, enum.Flag): - # Combinations of enum.Flag members are also members. We generate - # these dynamically, because static allocation takes O(2^n) memory. - # LazyStrategy is used for the ease of force_repr. - inner = sets(sampled_from(list(values)), min_size=1).map( - lambda s: reduce(operator.or_, s) - ) - return LazyStrategy(lambda: inner, args=[], kwargs={}, force_repr=repr_) return SampledFromStrategy(values, repr_) diff --git a/contrib/python/hypothesis/py3/hypothesis/version.py b/contrib/python/hypothesis/py3/hypothesis/version.py index 8360110757..41fe61ff7e 100644 --- a/contrib/python/hypothesis/py3/hypothesis/version.py +++ b/contrib/python/hypothesis/py3/hypothesis/version.py @@ -8,5 +8,5 @@ # v. 2.0. If a copy of the MPL was not distributed with this file, You can # obtain one at https://mozilla.org/MPL/2.0/. -__version_info__ = (6, 96, 4) +__version_info__ = (6, 97, 0) __version__ = ".".join(map(str, __version_info__)) diff --git a/contrib/python/hypothesis/py3/ya.make b/contrib/python/hypothesis/py3/ya.make index 73016652bc..8be1df19a1 100644 --- a/contrib/python/hypothesis/py3/ya.make +++ b/contrib/python/hypothesis/py3/ya.make @@ -2,7 +2,7 @@ PY3_LIBRARY() -VERSION(6.96.4) +VERSION(6.97.0) LICENSE(MPL-2.0) |