|
1 | 1 | import builtins
|
2 |
| -from typing import Any |
| 2 | +import functools |
| 3 | +from collections.abc import Callable |
| 4 | +from typing import Any, TypeVar, cast |
3 | 5 |
|
4 | 6 | import hypothesis.extra.numpy as npst
|
5 | 7 | import hypothesis.strategies as st
|
|
34 | 36 |
|
35 | 37 | MAX_BINARY_SIZE = 100
|
36 | 38 |
|
| 39 | +F = TypeVar("F", bound=Callable[..., Any]) |
| 40 | + |
| 41 | + |
| 42 | +def with_frequency(frequency: float) -> Callable[[F], F]: |
| 43 | + """This needs to be deterministic for hypothesis replaying""" |
| 44 | + |
| 45 | + def decorator(func: F) -> F: |
| 46 | + counter_attr = f"__{func.__name__}_counter" |
| 47 | + |
| 48 | + @functools.wraps(func) |
| 49 | + def wrapper(*args: Any, **kwargs: Any) -> Any: |
| 50 | + return func(*args, **kwargs) |
| 51 | + |
| 52 | + @precondition |
| 53 | + def frequency_check(f: Any) -> Any: |
| 54 | + if not hasattr(f, counter_attr): |
| 55 | + setattr(f, counter_attr, 0) |
| 56 | + |
| 57 | + current_count = getattr(f, counter_attr) + 1 |
| 58 | + setattr(f, counter_attr, current_count) |
| 59 | + |
| 60 | + return (current_count * frequency) % 1.0 >= (1.0 - frequency) |
| 61 | + |
| 62 | + return cast(F, frequency_check(wrapper)) |
| 63 | + |
| 64 | + return decorator |
| 65 | + |
37 | 66 |
|
38 | 67 | def split_prefix_name(path: str) -> tuple[str, str]:
|
39 | 68 | split = path.rsplit("/", maxsplit=1)
|
@@ -129,6 +158,7 @@ def add_array(
|
129 | 158 | self.all_arrays.add(path)
|
130 | 159 |
|
131 | 160 | @rule()
|
| 161 | + @with_frequency(0.25) |
132 | 162 | def clear(self) -> None:
|
133 | 163 | note("clearing")
|
134 | 164 | import zarr
|
|
0 commit comments