1# This file is part of Hypothesis, which may be found at
2# https://github.com/HypothesisWorks/hypothesis/
3#
4# Copyright the Hypothesis Authors.
5# Individual contributors are listed in AUTHORS.rst and the git log.
6#
7# This Source Code Form is subject to the terms of the Mozilla Public License,
8# v. 2.0. If a copy of the MPL was not distributed with this file, You can
9# obtain one at https://mozilla.org/MPL/2.0/.
10
11from weakref import WeakKeyDictionary
12
13from hypothesis.control import note, should_note
14from hypothesis.errors import InvalidState
15from hypothesis.internal.reflection import (
16 convert_positional_arguments,
17 nicerepr,
18 proxies,
19 repr_call,
20)
21from hypothesis.strategies._internal.strategies import RecurT, SearchStrategy
22
23
24class FunctionStrategy(SearchStrategy):
25 def __init__(self, like, returns, pure):
26 super().__init__()
27 self.like = like
28 self.returns = returns
29 self.pure = pure
30 # Using wekrefs-to-generated-functions means that the cache can be
31 # garbage-collected at the end of each example, reducing memory use.
32 self._cache = WeakKeyDictionary()
33
34 def calc_is_empty(self, recur: RecurT) -> bool:
35 return recur(self.returns)
36
37 def do_draw(self, data):
38 @proxies(self.like)
39 def inner(*args, **kwargs):
40 if data.frozen:
41 raise InvalidState(
42 f"This generated {nicerepr(self.like)} function can only "
43 "be called within the scope of the @given that created it."
44 )
45 if self.pure:
46 args, kwargs = convert_positional_arguments(self.like, args, kwargs)
47 key = (args, frozenset(kwargs.items()))
48 cache = self._cache.setdefault(inner, {})
49 if key not in cache:
50 cache[key] = data.draw(self.returns)
51 if should_note(): # optimization to avoid needless repr_call
52 rep = repr_call(self.like, args, kwargs, reorder=False)
53 note(f"Called function: {rep} -> {cache[key]!r}")
54 return cache[key]
55 else:
56 val = data.draw(self.returns)
57 if should_note():
58 rep = repr_call(self.like, args, kwargs, reorder=False)
59 note(f"Called function: {rep} -> {val!r}")
60 return val
61
62 return inner