Skip to content

Commit 6fa55f0

Browse files
committed
Reduce freqquency of clear
1 parent 416d2c2 commit 6fa55f0

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/zarr/testing/stateful.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import builtins
2-
from typing import Any
2+
import functools
3+
from collections.abc import Callable
4+
from typing import Any, TypeVar, cast
35

46
import hypothesis.extra.numpy as npst
57
import hypothesis.strategies as st
@@ -34,6 +36,33 @@
3436

3537
MAX_BINARY_SIZE = 100
3638

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+
3766

3867
def split_prefix_name(path: str) -> tuple[str, str]:
3968
split = path.rsplit("/", maxsplit=1)
@@ -129,6 +158,7 @@ def add_array(
129158
self.all_arrays.add(path)
130159

131160
@rule()
161+
@with_frequency(0.25)
132162
def clear(self) -> None:
133163
note("clearing")
134164
import zarr

0 commit comments

Comments
 (0)