diff --git a/hypothesis-python/src/hypothesis/control.py b/hypothesis-python/src/hypothesis/control.py index 6a9523a463..a2cebd1bd8 100644 --- a/hypothesis-python/src/hypothesis/control.py +++ b/hypothesis-python/src/hypothesis/control.py @@ -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, ) @@ -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 diff --git a/hypothesis-python/src/hypothesis/strategies/_internal/core.py b/hypothesis-python/src/hypothesis/strategies/_internal/core.py index b8d4aeb010..8ab02d0b39 100644 --- a/hypothesis-python/src/hypothesis/strategies/_internal/core.py +++ b/hypothesis-python/src/hypothesis/strategies/_internal/core.py @@ -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)