Skip to content

Commit

Permalink
defer reprs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Feb 4, 2024
1 parent e769f67 commit 1e2991c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions hypothesis-python/src/hypothesis/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ def __call__(self, x):


@contextmanager
def deprecate_random_in_strategy(where, stacklevel=0):
def deprecate_random_in_strategy(fmt, *args):
_global_rand_state = random.getstate()
yield (checker := _Checker())
if _global_rand_state != random.getstate() and not checker.saw_global_random:
# raise InvalidDefinition
note_deprecation(
"Do not use the `random` module inside strategies; instead "
"consider `st.randoms()`, `st.sampled_from()`, etc. " + where,
"consider `st.randoms()`, `st.sampled_from()`, etc. " + fmt.format(*args),
since="RELEASEDAY",
has_codemod=False,
stacklevel=stacklevel + 1,
stacklevel=1,
)


Expand Down Expand Up @@ -153,7 +153,7 @@ def prep_args_kwargs_from_strategies(self, kwarg_strategies):
kwargs = {}
for k, s in kwarg_strategies.items():
start_idx = self.data.index
with deprecate_random_in_strategy(f"from {k}={s!r}", stacklevel=1) as check:
with deprecate_random_in_strategy("from {}={!r}", k, s) as check:
obj = check(self.data.draw(s, observe_as=f"generate:{k}"))
end_idx = self.data.index
kwargs[k] = obj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2134,7 +2134,7 @@ def draw(self, strategy: SearchStrategy[Ex], label: Any = None) -> Ex:
self.count += 1
printer = RepresentationPrinter(context=current_build_context())
desc = f"Draw {self.count}{'' if label is None else f' ({label})'}: "
with deprecate_random_in_strategy(f"{desc}from {strategy!r}", stacklevel=1):
with deprecate_random_in_strategy("{}from {!r}", desc, strategy):
result = self.conjecture_data.draw(strategy, observe_as=f"generate:{desc}")
if TESTCASE_CALLBACKS:
self.conjecture_data._observability_args[desc] = to_jsonable(result)
Expand Down

0 comments on commit 1e2991c

Please sign in to comment.